Spring同Mybatis的整合
1.引入相应的jar包。(Mybatis的jar包,Spring的jar包,mybatis-spring-1.1.1.jar)。
2.编写相应的包(三层的包)。搭建。
3.配置相应的spring的配置。
1)配置相应的数据源的配置。
<?xml version="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">
<!-- 配置数据源-->
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="username">
<value>bbs</value>
</property>
<property name="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。
<?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="com.gxa.bj.dao.imp.UserMapper">
<insert id="addItem" parameterType="com.gxa.bj.model.UserInfo">
insert into UserInfo(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
<if test="userName!=null">
userName = #{userName},
</if>
<if test="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} Where userId=#{userId}
</update>
<select id="getModel" resultType="com.gxa.bj.model.UserInfo">
select * from userinfo where userid=#{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 userName like '%${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>
<if test="userName!=null">
And userName like #{userName}
</if>
<if test="userId>0">
And userId =#{userId}
</if>
<if test="userPwd!=null and userPwd!=''">
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>
<if test="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层。
package com.gxa.bj.service;
import java.util.List;
import com.gxa.bj.dao.imp.UserMapper;
import com.gxa.bj.model.UserInfo;
public class UserInfoService {
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<UserInfo> getList(UserInfo t){
return userMapper.getList(t);
}
}
7.编写相应的action层:action层里需要service层:
package com.gxa.bj.action;
import java.util.List;
import com.gxa.bj.model.UserInfo;
import com.gxa.bj.service.UserInfoService;
public class UserInfoAction {
private UserInfoService userInfoService;
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public List<UserInfo> getList(UserInfo u){
return userInfoService.getList(u);
}
}
8.在spring里的配置文件,将各层注入到spring中。
<!--配置dao层 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.gxa.bj.dao.imp.UserMapper"></property>
<property name="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">
<property name="userInfoService" ref="userInfoService"></property>
</bean>