`

spring4+mybatis3 集成实例(一)

阅读更多

 

<?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:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
   
    <!-- 说明:下面有的Bean配置提供了多种方案,请根据需要采用某一种(别忘了注释掉其他同类方案) -->

 <!-- 自动加载属性配置文件 -->
 <context:property-placeholder location="classpath:jdbc.properties" />
 
 <!-- 自动扫描Spring注解配置 -->
 <context:component-scan base-package="cn" />
 
<!--  <bean id="applicationUtil" class="job.SpringContextHolder"/> -->


 
 <!-- spring 整合mybatis -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="url" value="${url}"></property>
  <property name="driverClassName" value="${driverClassName}"></property>
  <property name="username" value="root"></property>
  <property name="password" value="password"></property>
  <!--连接池启动时的初始化-->
  <property name="initialSize" value="2"></property>
  <!--连接池的最大值-->
  <property name="maxActive" value="100"></property>
  <!-- 最大空闲值-->
  <property name="maxIdle" value="20"></property>
  <!-- 最小空闲值-->
  <property name="minIdle" value="10"></property>
  <property name="validationQuery" value="${validationQuery}"></property>
 </bean>
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="mapperLocations" value="classpath:cn/mapper/*.xml"></property>
  <property name="configLocation" value="classpath:configuration.xml"></property>
 </bean>
 <!-- 配置扫描器 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="cn.mapper"></property>
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 </bean>
 
 <!-- spring的事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <!-- 注解方式配置事务 -->
 <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

需要加入spring-mybatis集成jar包:http://mybatis.github.io/spring/

 

@Repository
public interface UserMapper {

 public abstract int delete(String userId);
 
 public abstract int insert(User user);
 
 public abstract int update(User user);
 
 public abstract User selectById(String userId);
 
 public abstract List<User> select();
}

<?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="cn.mapper.UserMapper" >


 <resultMap id="userDomain" type="cn.domain.User">
  <id property="userId" column="USERID" ></id>
  <result property="userName" column="USERNAME"></result>
  <result property="birthday" column="BIRTHDAY"></result>
  <result property="salary" column="SALARY"></result>
  
<!--   <result property="birthdayStr" column="BIRTHDAY" typeHandler="cn.util.MyDateHandler"></result> -->
  <result property="birthdayStr" column="BIRTHDAY" typeHandler="myDateHandler"></result>
 </resultMap>
 
 <sql id="userFields">
  USERID, USERNAME, BIRTHDAY, SALARY
 </sql>

 <select id="selectById" parameterType="string" resultType="cn.domain.User">
  select
  <include refid="userFields"></include>
  from user
  where USERID = #{userId}
 </select>
 
 <select id="select" resultMap="userDomain">
  select
  <include refid="userFields"></include>
  from user
 </select>

 <insert id="insert" parameterType="cn.domain.User" >
  insert into user
  <trim prefix="(" suffix=")" suffixOverrides=",">
   <if test="userId != null">
    USERID,
   </if>
   <if test="userName != null">
    USERNAME,
   </if>
   <if test="birthday != null">
    BIRTHDAY,
   </if>
   <if test="salary != null">
    SALARY,
   </if>
  </trim>
  
  <trim prefix="values (" suffix=")" suffixOverrides=",">
   <if test="userId != null">
    #{userId},
   </if>
   <if test="userName != null">
    #{userName},
   </if>
   <if test="birthday != null">
    #{birthday},
   </if>
   <if test="salary != null">
    #{salary},
   </if>
  </trim>
 </insert>
 
 <update id="update" parameterType="cn.domain.User">
  update user
  set USERNAME = #{userName},
   BIRTHDAY = #{birthday},
   SALARY   = #{salary}
  where USERID = #{userId}
 </update>
 
 <delete id="delete" parameterType="string">
  delete from user where USERID = #{userId}
 </delete>
</mapper>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics