本文主要介绍spring整合myBatis,用jar包是:spring 4.1 + myBatis 3.2.2 + mybatis-spring 1.1。
项目的源码和jar包可以去我的资源下载 http://download.csdn.net/detail/psp0001060/9659564
一、整个项目的一个结构
二、主要整合部分
ClazzController:
package com.edwin.action; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.Assert; import com.edwin.dao.ClazzMapper; import com.edwin.model.Clazz; import com.edwin.service.ClazzService; public class ClazzController { ApplicationContext ac = null; @Before public void prepar(){ ac = new ClassPathXmlApplicationContext( "/applicationContext.xml"); ClazzMapper clazzMapper =(ClazzMapper) ac.getBean("clazzMapper"); Assert.notNull(clazzMapper); } @Test public void testClzInsert(){ ClazzService cs = (ClazzService) ac.getBean("clazzService"); Clazz clz = new Clazz(); // clz.setId(10); clz.setClassname("class010"); cs.insert(clz); } }
clazzService:
package com.edwin.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.edwin.dao.ClazzMapper; import com.edwin.model.Clazz; @Service(value="clazzService") public class ClazzService { @Resource(name="clazzMapper") private ClazzMapper clazzMapper; public ClazzMapper getClazzMapper() { return clazzMapper; } public void setClazzMapper(ClazzMapper clazzMapper) { this.clazzMapper = clazzMapper; } public int insert(Clazz clz){ return clazzMapper.insert(clz); } }
ClazzMapper:
package com.edwin.dao; import com.edwin.model.Clazz; public interface ClazzMapper { int deleteByPrimaryKey(Integer id); int insert(Clazz record); int insertSelective(Clazz record); Clazz selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Clazz record); int updateByPrimaryKey(Clazz record); }
ClazzMapper.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.edwin.dao.ClazzMapper"> <resultMap id="BaseResultMap" type="Clazz"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="classname" property="classname" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List"> id, classname </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select <include refid="Base_Column_List" /> from clazz where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from clazz where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="Clazz"> insert into clazz (id, classname) values (#{id,jdbcType=INTEGER}, #{classname,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="Clazz"> insert into clazz <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="classname != null"> classname, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="classname != null"> #{classname,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="Clazz"> update clazz <set> <if test="classname != null"> classname = #{classname,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="Clazz"> update clazz set classname = #{classname,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <context:component-scan base-package="com.edwin.service"></context:component-scan> <!-- 数据源c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="5" /> <property name="initialPoolSize" value="2" /> <property name="acquireIncrement" value="2" /> </bean> <!-- sessionFactory 将spring和mybatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:com/edwin/mapper/*.xml" /> <!-- 加载mapper文件 --> </bean> <!-- dao mapping接口扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value=" com.edwin.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.edwin.service.*.*(..))" id="pointCut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" /> </aop:config> </beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.edwin.model.Clazz" alias="Clazz" /> </typeAliases> </configuration>