applicaion

fengtiejun 2011-12-12 10:19:35
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
<property name="username" value="System"></property>
<property name="password" value="master"></property>
</bean>
<!-- sqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:Ibatis.xml"></property>
</bean>
<!-- SqlMapClientTemplate -->
<bean id="sqlTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
<!-- 实体对象 -->
<bean id="vo" class="com.T40.PoJo.tb_user_test">
</bean>
<!-- dao类 -->
<!-- 利用spring提供的SqlMapClientTemplate实现持久层 -->
<bean id="templateDao" class="com.T40.Dao.imps.testDao">
<property name="test" ref="vo"/>
<property name="sqlTemplate">
<ref bean="sqlTemplate"/>
</property>
</bean>
<bean id="templatebiz" class="com.T40.Server.checkUser">
<property name="db" ref="templateDao"></property>
</bean>
<!-- action -->
<bean name="/check" class="com.T40.struts.action.CheckAction">
<property name="test">
<ref bean="templatebiz"/>
</property>
</bean>
<!-- 定义事物管理器(专门由于处理事物的通知) -->
<bean id="tranct" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 定义事务拦截器(事物切入点) -->
<bean id="trInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="tranct"></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 自动代理 -->
<bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value><!-- bean的id名称 -->
</list>
</property>
<property name="interceptorNames">
<list>
<value>trInterceptor</value>
</list>
</property>
</bean>
</beans>
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengtiejun 2011-12-14
  • 打赏
  • 举报
回复
映射文件内容(User.xml)

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="mbs.com.mapper.IUserMapper">

<resultMap type="User" id="uM">

<id property="id" column="id" />

<result property="address" column="address" />

<result property="birthday" column="birthday" />

<result property="cardId" column="cardId" />

<result property="email" column="email" />

<result property="loginName" column="loginName" />

<result property="passWord" column="passWord" />

<result property="pictureUrl" column="pictureUrl" />

<result property="sex" column="sex" />

<result property="telphone" column="telphone" />

<result property="userName" column="userName" />

</resultMap>



<!--查询:通过用户ID返回一个用户对象 -->

<select id="getUser" resultMap="uM" parameterType="Integer">

select * from users

<where>#{id}</where>

</select>

</mapper>



mybatis_config.xml内容



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<typeAliases>

<typeAlias alias="user" type="mbs.com.pojo.User" />

</typeAliases>

<mappers>

<mapper resource="mapper_sql/User.xml" />

</mappers>

</configuration>



IUserService.java 接口内容

public interface IUserService

{

User getUser(Integer id);

}



IUserMapper.java 映射器内容



@Mapper("userMapper")

public interface IUserMapper

{

User getUser(Integer id);

}



FooService.java 实现类内容

@Service("fooService")

public class FooService implements IUserService

{

@Autowired

private IUserMapper userMapper;



public User getUser(Integer id)

{

return userMapper.getUser(id);

}



}







spring.xm内容

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd"

default-init-method="init">

<!-- Annotation Config -->

<context:annotation-config/>



<!--注册数据库的连接信息 -->

<context:property-placeholder location="classpath:config/jdbc.properties"/>



<!-- 扫描物理路径及注册 -->

<context:component-scan base-package="mbs.com"/>



<!-- Data Source -->

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">

<property name="driver" value="${dirver}"/>

<property name="driverUrl" value="${url}"/>

<property name="user" value="${username}"/>

<property name="password" value="${password}"/>

<property name="minimumConnectionCount" value="${min_conn_count}"/>

<property name="maximumConnectionCount" value="${max_conn_count}"/>

</bean>



<!-- 配置mybatis的sqlsessionFactory -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="configLocation" value="classpath:mybatis/mybatis_config.xml"/>

</bean>



<!-- Transaction Manager -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>



<!--配置 ibatis的映射器 -->

<bean id="userMapper" class="org.mybatis.spring.MapperFactoryBean">

<property name="sqlSessionFactory" ref="sqlSessionFactory"/>

<property name="mapperInterface" value="mbs.com.mapper.IUserMapper"/>

</bean>

</beans>



main 函数测试

public static void main(String[] args)

{

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

UserService service =(IUserService)context.getBean("fooService");

User user = service.getUser(1);

System.out.println("用户名:"+user.getUserName());

System.out.println("密 码:"+user.getPassWord());

}
hllfl 2011-12-14
  • 打赏
  • 举报
回复
擦 还没解贴~
fengtiejun 2011-12-14
  • 打赏
  • 举报
回复
请求的Servlet,也可以写入main方法中进行测试
package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class IOServlet extends HttpServlet
{
/** *//**
* 对于主动请求其它接口的参数流写入(POST方式)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("begin send");
String inputParam = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><page><username>爱心天使</usernaem><age>26</age></page>";

URL url = null;
HttpURLConnection httpConn = null;
OutputStream output = null;
OutputStreamWriter outr = null;

url = new URL("http://127.0.0.1:8888/iotest/ReadServlet");
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "text/xml");
httpConn.connect();
output = httpConn.getOutputStream();
outr = new OutputStreamWriter(output);
// 写入请求参数
outr.write(inputParam.toString().toCharArray(), 0, inputParam
.toString().length());
outr.flush();
outr.close();
System.out.println("send ok");
int code = httpConn.getResponseCode();
System.out.println("code " + code);
System.out.println(httpConn.getResponseMessage());

//读取响应内容
String sCurrentLine = "";
String sTotalString = "";
if (code == 200)
{
java.io.InputStream is = httpConn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((sCurrentLine = reader.readLine()) != null)
if (sCurrentLine.length() > 0)
sTotalString = sTotalString + sCurrentLine.trim();
} else
{
sTotalString = "远程服务器连接失败,错误代码:" + code;

}
System.out.println("response:" + sTotalString);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}

}

ReadServlet(相当于被请求的服务器):

package com;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ReadServlet extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
System.out.println("begin read");
ServletInputStream inStream = request.getInputStream(); // 取HTTP请求流
int size = request.getContentLength(); // 取HTTP请求流长度
byte[] buffer = new byte[size]; // 用于缓存每次读取的数据
byte[] in_b = new byte[size]; // 用于存放结果的数组
int count = 0;
int rbyte = 0;
// 循环读取
while (count < size)
{
rbyte = inStream.read(buffer); // 每次实际读取长度存于rbyte中 sflj
for (int i = 0; i < rbyte; i++)
{
in_b[count + i] = buffer[i];
}
count += rbyte;
}
System.out.println("result:" + new String(in_b,0,in_b.length));

response.setContentType("text/html");
//注意响应中文数据时要设置
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
//回与响应数据
out.write("您已经请求成功,这是响应数据!");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}

}

打开页面:http://localhost:8888/iotest/IOServlet

你会在后台看到如下输出说明你已经成功了:
begin send
send ok
begin read
result:<?xml version="1.0" encoding="UTF-8"?><page><username>爱心天使</usernaem><age>26</age></page>
code 200
OK
response:您已经请求成功,这是响应数据!
fengtiejun 2011-12-14
  • 打赏
  • 举报
回复
package com.yixun.wap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/** *//**
*
* @descripte use java.net.HttpURLConnection to request resource form web.
* @author Gavin.lee
* @date 2009-5-23 12:21:00
* @version 1.0
*/
public class TransactionCenter {
private URL url;
private HttpURLConnection urlconn;

String inencoding;
String outencoding;

public TransactionCenter(String inencoding, String outencoding) {
this.inencoding = inencoding;
this.outencoding = outencoding;
}

public String connect(String params, String postUrl) {
BufferedReader br = null;
String response = "", brLine = "";
try {
//params=URLEncoder.encode(params,"GB2312"); //use URLEncoder.encode for encode the params

url = new URL(postUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("user-agent","mozilla/4.7 [en] (win98; i)"); //set request header
urlconn.setRequestProperty("X-Forwarded-For", "127.0.0.1");
urlconn.setConnectTimeout(30000);
urlconn.setReadTimeout(30000);
urlconn.setRequestMethod("POST"); // request method, default GET
urlconn.setUseCaches(false); //Post can not user cache
urlconn.setDoOutput(true); //set output from urlconn
urlconn.setDoInput(true); //set input from urlconn
OutputStream out = urlconn.getOutputStream();
out.write(params.getBytes(outencoding));
out.flush();
out.close(); // output stream close,That's means need not to post data to this outputstream

br = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), inencoding));
while((brLine = br.readLine())!=null)
response =(new StringBuilder(String.valueOf(response))).append(brLine).toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch (IOException e) {
System.out.println("input stream close fail");
}
urlconn.disconnect();
}
return response;
}

public static void main(String[] args) {
TransactionCenter tc = new TransactionCenter("GBK", "GBK");
String response = tc.connect("c_id=10041&cpid=5&c_type=1&lotid=1&expect=08059","http://inter.boss.com/interface/client/requestwap.php?");
System.out.println(response);
}
}


fengtiejun 2011-12-14
  • 打赏
  • 举报
回复
package com.yixun.wap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/** *//**
*
* @descripte use java.net.HttpURLConnection to request resource form web.
* @author Gavin.lee
* @date 2009-5-23 12:21:00
* @version 1.0
*/
public class TransactionCenter {
private URL url;
private HttpURLConnection urlconn;

String inencoding;
String outencoding;

public TransactionCenter(String inencoding, String outencoding) {
this.inencoding = inencoding;
this.outencoding = outencoding;
}

public String connect(String params, String postUrl) {
BufferedReader br = null;
String response = "", brLine = "";
try {
//params=URLEncoder.encode(params,"GB2312"); //use URLEncoder.encode for encode the params

url = new URL(postUrl);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestProperty("user-agent","mozilla/4.7 [en] (win98; i)"); //set request header
urlconn.setRequestProperty("X-Forwarded-For", "127.0.0.1");
urlconn.setConnectTimeout(30000);
urlconn.setReadTimeout(30000);
urlconn.setRequestMethod("POST"); // request method, default GET
urlconn.setUseCaches(false); //Post can not user cache
urlconn.setDoOutput(true); //set output from urlconn
urlconn.setDoInput(true); //set input from urlconn
OutputStream out = urlconn.getOutputStream();
out.write(params.getBytes(outencoding));
out.flush();
out.close(); // output stream close,That's means need not to post data to this outputstream

br = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), inencoding));
while((brLine = br.readLine())!=null)
response =(new StringBuilder(String.valueOf(response))).append(brLine).toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch (IOException e) {
System.out.println("input stream close fail");
}
urlconn.disconnect();
}
return response;
}

public static void main(String[] args) {
TransactionCenter tc = new TransactionCenter("GBK", "GBK");
String response = tc.connect("c_id=10041&cpid=5&c_type=1&lotid=1&expect=08059","http://inter.boss.com/interface/client/requestwap.php?");
System.out.println(response);
}
}


hllfl 2011-12-13
  • 打赏
  • 举报
回复
这。。。spring托管
fengtiejun 2011-12-12
  • 打赏
  • 举报
回复
顶啊,认真学习啊,对spring mybatis有用啊

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧