MyBatis整合Spring框架的使用,需要用到的是包
mybatis-x.x.x.jar
mybatis-spring-x.x.x.jar
spring系列包,我使用的是spring3
数据库连接池使用c3p0
需要注意的是,建议在整合的时候使用3.1以上的mybatis-spring包
下面来一步一步的实现mybatis-spring整合
1,实体类:省略get和set方法
package com.tenghu.mybatis.model; import java.io.Serializable; public class Emp implements Serializable{ private int id; private int age; private String name; public Emp(int id, int age, String name) { super(); this.id = id; this.age = age; this.name = name; } public Emp() { super(); } }2,映射文件:EmpMapper.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.tenghu.mybatis.mapper.EmpMapper"> <!-- 查询所有数据 --> <select id="queryForList" resultType="Emp"> select * from emp </select> <!-- 根据ID查询数据 --> <select id="queryEmpById" resultType="Emp"> select * from emp where id=#{id} </select> <!-- 添加数据 --> <insert id="addEmp" parameterType="Emp"> insert into emp values(#{name},#{id},#{age}) </insert> <!-- 修改数据 --> <update id="updateEmp" parameterType="Emp"> update emp <set> <if test="name!=null"> name=#{name}, </if> <if test="age!=0"> age=#{age} </if> </set> where id=#{id} </update> <!-- 删除数据 --> <delete id="deleteEmp" parameterType="int"> delete from emp where id=#{id} </delete> </mapper>3,操作数据接口与实现类:
package com.tenghu.mybatis.inter; import java.util.List; import com.tenghu.mybatis.model.Emp; public interface EmpManager { /** * 查询所有数据 * @return */ public List<Emp> queryForList(); /** * 根据ID查询数据 * @param id * @return */ public Emp queryEmpById(int id); /** * 添加数据 * @param emp * @return */ public int addEmp(Emp emp); /** * 修改数据 * @param emp * @return */ public int updateEmp(Emp emp); /** * 删除数据 * @param id * @return */ public int deleteEmp(int id); }
package com.tenghu.mybatis.inter.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.tenghu.mybatis.inter.EmpManager; import com.tenghu.mybatis.model.Emp; public class EmpManagerImpl implements EmpManager{ private SqlSession sqlSession; private String namespace="com.tenghu.mybatis.mapper.EmpMapper."; @Override public List<Emp> queryForList() { return sqlSession.selectList(namespace+"queryForList"); } public SqlSession getSqlSession() { return sqlSession; } public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } @Override public Emp queryEmpById(int id) { return sqlSession.selectOne(namespace+"queryEmpById", id); } @Override public int addEmp(Emp emp) { return sqlSession.insert(namespace+"addEmp", emp); } @Override public int updateEmp(Emp emp) { return sqlSession.update(namespace+"updateEmp", emp); } @Override public int deleteEmp(int id) { return sqlSession.delete(namespace+"deleteEmp", id); } }
<?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 alias="Emp" type="com.tenghu.mybatis.model.Emp"/> </typeAliases> </configuration>
<?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: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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <!-- 引入属性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="prototype"> <property name="locations"> <list> <value>classpath*:jdbc.properties</value> </list> </property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" scope="prototype"> <property name="driverClass" value="${mysql.driver}"/> <property name="jdbcUrl" value="${mysql.url}"/> <property name="user" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> <property name="maxPoolSize" value="100"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="mapperLocations" value="classpath*:com/tenghu/mybatis/mapper/*.xml"/> </bean> <!-- 配置SqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" scope="prototype"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用申明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
<?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: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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName"> <bean id="empManagerImpl" class="com.tenghu.mybatis.inter.impl.EmpManagerImpl" scope="prototype"/> </beans>
mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://127.0.0.1:3306/company mysql.username=root mysql.password=xiaohu
package com.tenghu.mybatis.util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextUtil { private ApplicationContextUtil(){} private static ApplicationContext applicationContext; static{ applicationContext=new ClassPathXmlApplicationContext("classpath*:spring/*.xml"); } public static <T> T getBean(String names){ return (T) applicationContext.getBean(names); } }
package com.tenghu.mybatis.test; import java.util.List; import org.junit.Test; import com.tenghu.mybatis.inter.EmpManager; import com.tenghu.mybatis.model.Emp; import com.tenghu.mybatis.util.ApplicationContextUtil; public class EmpManagerTest { /** * 查询所有数据 */ @Test public void testQueryForList(){ EmpManager empManager=ApplicationContextUtil.getBean("empManagerImpl"); List<Emp> empList=empManager.queryForList(); for (Emp emp : empList) { System.out.println(emp.getAge()+"\t"+emp.getId()+"\t"+emp.getName()); } } /** * 根据ID查询数据 */ @Test public void testQueryEmpById(){ EmpManager empManager=ApplicationContextUtil.getBean("empManagerImpl"); Emp emp=empManager.queryEmpById(6); System.out.println(emp.getAge()+"\t"+emp.getId()+"\t"+emp.getName()); } /** * 添加数据 */ @Test public void testAddEmp(){ EmpManager empManager=ApplicationContextUtil.getBean("empManagerImpl"); Emp emp=new Emp(7, 34, "李思思"); int result=empManager.addEmp(emp); System.out.println(result>0?"添加成功":"添加失败"); } /** * 修改数据 */ @Test public void testUpdateEmp(){ EmpManager empManager=ApplicationContextUtil.getBean("empManagerImpl"); Emp emp=new Emp(7, 54, "李思思"); int result=empManager.updateEmp(emp); System.out.println(result>0?"修改成功":"修改失败"); } /** * 删除数据 */ @Test public void testDeleteEmp(){ EmpManager empManager=ApplicationContextUtil.getBean("empManagerImpl"); int result=empManager.deleteEmp(7); System.out.println(result>0?"删除成功":"删除失败"); } }