Spring同Mybatis的整合

1.引入相应的jar包。(Mybatisjar包,Springjar包,mybatis-spring-1.1.1.jar)


2.编写相应的包(三层的包)。搭建


3.配置相应的spring的配置 package applicationContext.xml

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">


<span style="color:#ff0000;"><!-- 配置数据源--></span>
<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>  



2)配置mybatisSqlSessionFactory(也需要在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。说如果缺少什么,而已经写了的就直接删除

package UserInfoMapper.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="com.gxa.bj.dao.imp.UserMapper">
 <insert id="addItem" parameterType="com.gxa.bj.modle.UserInfo">
       insert into UserInfo(userId,userName,userPwd,userEmail)
       values(usernext.nextval,#{userName},#{userPwd},#{userEmail})
 </insert>
 <delete id="removeItem" >
       delete from UserInfo where userId = #{id}
 </delete>
 <update id="updateItem" parameterType="com.gxa.bj.modle.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>
       userId=#{userId} where userId=#{userId}       
 </update>
 <select id="getMolde" resultType="com.gxa.bj.modle.UserInfo">
       select * from UserInfo where userId = #{id}
 </select>
 <select id="getUsers" parameterType="java.lang.String" resultType="com.gxa.bj.modle.UserInfo">
       select * from UserInfo where userName like '%${value}%'
 </select>
 <select id="getList" resultType="com.gxa.bj.modle.UserInfo" parameterType="com.gxa.bj.modle.UserInfo">
        select * from UserInfo where 1=1
        <if test="userName!=null">
            and userName = #{userName}
        </if>
        <if test="userId>0">
            and userId=#{userId}
        </if>
        <if test="userPwd!=null">
            and userPwd = #{userPwd}
        </if>
        <if test="userEmail!=null">
           and userEmail =#{userEmail}
        </if>
 </select>
 <select id="getListByPage" resultType="com.gxa.bj.modle.UserInfoPage" parameterType="com.gxa.bj.modle.UserInfoPage">
        select u.*
        From(select rownum as num, userinfo.*
              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 #{userName}
                      </if>
              </where>
              ) u where u.num between #{startNum}and#{endNum}
 </select>
</mapper>


package PostInfoMapper.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="com.gxa.bj.dao.imp.PostMapper">
     <select id="getMolde" parameterType="java.lang.String" resultMap="PostInfoResult">
            select * from postinfo p inner join userinfo u
                  on p.userId=u.UserId
                  inner join typeinfo t
                  on p.typeId=t.typeId
                  where p.postId=#{id}
     </select>
     <resultMap type="com.gxa.bj.modle.PostInfo" id="PostInfoResult">
                <result column="postId"  property="postId"></result>
                <result column="postName"  property="postName"></result>
                <result column="postContent"  property="postContent"></result>
                <result column="postTime"  property="postTime"></result>
                <result column="userId"  property="userId"></result>
                <result column="typeId"  property="typeId"></result>
                <association property="postTypeInfo" javaType="com.gxa.bj.modle.TypeInfo" >
                           <id column="typeId" property="typeId"></id>
                           <result column="typeContent" property="typeContent"></result>
                </association>
                 <association property="postUserInfo" javaType="com.gxa.bj.modle.UserInfo" >
                           <id column="userId" property="userId"></id>
                           <result column="typeName" property="typeName"></result>
                </association>
     </resultMap>
</mapper>

5.编写相应的dao层。比如创建的是UserInfoMapper。截图如下:(这个层里全是接口)


6.编写相应的Service层,sevice层里需要引入的是dao

import java.util.List;
import com.gxa.bj.dao.imp.UserMapper;
import com.gxa.bj.modle.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 u){//查询集合
  System.out.println(userMapper.hashCode());
  return userMapper.getList(u);
   }
 
}

7.编写相应的action层:action层里需要service层:

import java.util.List;
import com.gxa.bj.modle.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 t){
  return userInfoService.getList(t);
   }
}

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>

补充,如果改用C3P0作为数据源:
需要引入C3P0jar包:


第二步,将之前的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中的注入方式:

<span style="color:#ff0000;"><!-- 配置数据源--></span>
<bean id="jdbcDataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</property>
<property name="user">
<value>bbs</value>
</property>
<property name="password">
<value>123</value>
</property>
<property name="initialPoolSize">
    <value>10</value>
</property>
</bean>  













































































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值