1.引入相应的jar包。(Mybatis的jar包,Spring的jar包,mybatis-spring-1.1.1.jar)。
2.编写相应的包(三层的包)。搭建。
3.配置相应的spring的配置。
1)配置相应的数据源的配置。
<?xmlversion="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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-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">
<!-- 配置数据源-->
<beanid="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<propertyname="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<propertyname="username">
<value>bbs</value>
</property>
<propertyname="password">
<value>123</value>
</property>
</bean>
</beans>
2)配置mybatis的SqlSessionFactory(也需要在ApplicationContext里做配置)。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource"/>
<property name="configLocation"value="classpath:mybatis-config.xml"></property>
</bean>
4.搭建mybatis的框架了(编写相应的实体类,SQL映射文件)。结果如下:
注意的问题:去掉<association>节点里的foreignColumn属性。
去掉所有的select节点里的resultSets的属性。
比如之前设置的UserInfoMapper。
<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEmapper
PUBLIC "-//mybatis.org//DTD Mapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.gxa.bj.dao.imp.UserMapper">
<insert id="addItem"parameterType="com.gxa.bj.model.UserInfo">
insert intoUserInfo(userid,username,userpwd,useremail,useraddress,regreason)
values(usernext.nextval,#{userName},#{userPwd},#{userEmail},#{userAddress},#{regReason})
</insert>
<delete id="removeItem">
delete from UserInfo where userId=#{id}
</delete>
<update id="updateItem"parameterType="com.gxa.bj.model.UserInfo">
update userinfo set
<iftest="userName!=null">
userName = #{userName},
</if>
<iftest="userPwd!=null">
userPwd = #{userPwd},
</if>
<if test="userEmail!=null">
userEmail = #{userEmail},
</if>
<if test="userAddress!=null">
userAddress = #{userAddress},
</if>
<if test="regReason!=null">
regReason = #{regReason},
</if>
userId=#{userId} WhereuserId=#{userId}
</update>
<select id="getModel"resultType="com.gxa.bj.model.UserInfo">
select * from userinfo whereuserid=#{id}
</select>
<select id="getUsers"parameterType="java.lang.String"resultType="com.gxa.bj.model.UserInfo"resultSets="com.gxa.bj.model.UserInfo">
select * from userinfo where userNamelike '%${value}%'
</select>
<select id="getList"resultSets="com.gxa.bj.model.UserInfo"parameterType="com.gxa.bj.model.UserInfo"resultType="com.gxa.bj.model.UserInfo">
Select * From userInfo
<where>
<iftest="userName!=null">
And userName like #{userName}
</if>
<if test="userId>0">
And userId =#{userId}
</if>
<if test="userPwd!=null anduserPwd!=''">
And userPwd like #{userPwd}
</if>
</where>
</select>
<select id="getListByPage"parameterType="com.gxa.bj.model.UserInfoPage" resultType="com.gxa.bj.model.UserInfo">
Select u.*
From (Select rownum as num, userinfo.*
from userinfo
<where>
<iftest="userName!=null">
And userName like #{userName}
</if>
<if test="userId >=10">
And userId =#{userId}
</if>
<if test="userPwd!=null and userPwd!='' ">
And userPwd like #{userPwd}
</if>
</where>
) u Where u.num between #{startNum} and #{endNum}
</select>
</mapper>
5.编写相应的dao层。比如创建的是UserInfoMapper。截图如下:(这个层里全是接口)
6.编写相应的Service层,sevice层里需要引入的是dao层。
packagecom.gxa.bj.service;
importjava.util.List;
importcom.gxa.bj.dao.imp.UserMapper;
importcom.gxa.bj.model.UserInfo;
public classUserInfoService {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapperuserMapper) {
this.userMapper = userMapper;
}
public List<UserInfo> getList(UserInfot){
returnuserMapper.getList(t);
}
}
7.编写相应的action层:action层里需要service层:
packagecom.gxa.bj.action;
importjava.util.List;
importcom.gxa.bj.model.UserInfo;
importcom.gxa.bj.service.UserInfoService;
public classUserInfoAction {
private UserInfoService userInfoService;
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoServiceuserInfoService) {
this.userInfoService =userInfoService;
}
public List<UserInfo> getList(UserInfou){
return userInfoService.getList(u);
}
}
8.在spring里的配置文件,将各层注入到spring中。
<!--配置dao层 -->
<bean id="userMapper"class="org.mybatis.spring.mapper.MapperFactoryBean">
<propertyname="mapperInterface"value="com.gxa.bj.dao.imp.UserMapper"></property>
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property>
</bean>
<!-- 配置Service层 -->
<bean id="userInfoService"class="com.gxa.bj.service.UserInfoService">
<property name="userMapper"ref="userMapper"></property>
</bean>
<!-- 配置Action层 -->
<bean id="userInfoAction"class="com.gxa.bj.action.UserInfoAction">
<propertyname="userInfoService"ref="userInfoService"></property>
</bean>
补充,如果改用C3P0作为数据源:
需要引入C3P0的jar包:
第二步,将之前的C3P0的配置改成spring中的属性注入。
比如之前的C3P0的配置如下:
c3p0.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
c3p0.driverClass=oracle.jdbc.driver.OracleDriver
c3p0.user=bbs
c3p0.password=123
c3p0.acquireIncrement=3
c3p0.idleConnectionTestPeriod=60
c3p0.initialPoolSize=10
c3p0.maxIdleTime=60
c3p0.maxPoolSize=20
c3p0.maxStatements=100
c3p0.minPoolSize=5
改成spring中的注入方式:
<!-- 配置数据源-->
<bean id="jdbcDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<propertyname="jdbcUrl">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="user">
<value>bbs</value>
</property>
<propertyname="password">
<value>123</value>
</property>
<propertyname="initialPoolSize">
<value>10</value>
</property>
</bean>