spring和mybatis整合

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


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="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>bbss</value>
</property>
<property name="password">
<value>123</value>
</property>
<property name="initialPoolSize">
   <value>10</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映射文件)。比如:

<?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.dao.imp.UserMapper">
  <select id="getModel" resultType="com.model.UserInfo">
    select * from UserInfo where userId = #{userId}
  </select>
  <select id="getUsers" parameterType="java.lang.String" resultType="com.model.UserInfo" >
    select * from UserInfo where userName like '%${value}%'
  </select>
  <delete id="removeItem">
    delete from UserInfo where userId=#{userId}
  </delete>
  <insert id="addItem" parameterType="com.model.UserInfo">
    insert into UserInfo(userid,username,userpwd,usermail,address,reason)
    values(usernext.nextval,#{userName},#{userPwd},#{userMail},#{address},#{reason})
  </insert>
  <insert id="getInsertUserId" parameterType="com.model.UserInfo">
      <selectKey keyProperty="userId" resultType="int" order="BEFORE">
           select usernext.nextval from dual
       </selectKey>
       insert into UserInfo(userid,username,userpwd,usermail,address,reason)
    values(#{userId},#{userName},#{userPwd},#{userMail},#{address},#{reason})
  </insert>
  <update id="updateItem" parameterType="com.model.UserInfo">
      update UserInfo set  
      <if test= "userName!=null">
          userName=#{userName},
      </if>
      <if test= "userPwd!=null">
          userPwd=#{userPwd},
      </if>
      <if test= "userMail!=null">
          userMail=#{userMail},
      </if>
      <if test= "address!=null">
          address=#{address},
      </if>
      <if test= "reason!=null">
          reason=#{reason},
      </if>
          userId=#{userId} where userId=#{userId}
  </update>
  <select id="getList" parameterType="com.model.UserInfo" resultType="com.model.UserInfo">
     select * from UserInfo where 1=1
      <if test= "userName!=null">
          And userName=#{userName}
      </if>
      <if test= "userPwd!=null"> 
          And userPwd=#{userPwd}
      </if>
      <if test= "userMail!=null">
          And userMail=#{userMail}
      </if>
      <if test= "address!=null">
          And address=#{address}
      </if>
      <if test= "reason!=null">
          And reason=#{reason}
      </if>
      <if test= "userId>0">
          And userId=#{userId}
      </if>
  </select>
  <select id="getListByPage" parameterType="com.model.UserInfo" resultType="com.model.UserInfo">
     Select u.*
     From  (Select rownum as num,*
            from userInfo Where 1=1
            
              <if test= "userName!=null">
                  And userName=#{userName}
              </if>
     <if test= "userPwd!=null"> 
         And userPwd=#{userPwd}
     </if>
     <if test= "userMail!=null">
         And userMail=#{userMail}
     </if>
     <if test= "address!=null">
         And address=#{address}
     </if>
     <if test= "reason!=null">
         And reason=#{reason}
     </if>
     <if test= "userId>0">
         And userId=#{userId}
     </if>
      ) u Where u.num between (#{pageIndex}-1)*#{pageSize} and #{pageIndex}*#{pageSize}
       
  </select>
  <select id="getListByPages" parameterType="com.model.UserInfoPage" resultType="com.model.UserInfo">
      select u.*
      From (Select rownum as num,userInfo.*
            from userInfo
            <where>
            <if test= "userName!=null">
                  And userName=#{userName}
              </if>
     <if test= "userPwd!=null"> 
         And userPwd=#{userPwd}
     </if>
     <if test= "userMail!=null">
         And userMail=#{userMail}
     </if>
     <if test= "address!=null">
         And address=#{address}
     </if>
     <if test= "reason!=null">
         And reason=#{reason}
     </if>
     <if test= "userId>0">
         And userId=#{userId}
     </if>
            </where>)u
        Where  u.num between  #{startNum} and #{endNum}
  </select>

</mapper>


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

 <!--配置dao层 -->  
   

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

package com.service;


import java.util.List;


import com.dao.imp.UserMapper;
import com.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 u){
return userMapper.getList(u);

}


}

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

package com.action;


import java.util.List;


import com.model.UserInfo;
import com.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.dao.imp.UserMapper"></property>
       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
   </bean>
   <!-- 配置Service层 -->
   <bean id="userInfoService" class="com.service.UserInfoService">
       <property name="userMapper" ref="userMapper"></property>
   </bean>
   <!-- 配置action层 -->
   <bean id="userInfoAction" class="com.action.UserInfoAction">
       <property name="userInfoService" ref="userInfoService"></property>
   </bean>










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值