MyBatis与Spring集成
beans.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 1. 数据源:DriverManagerDataSource --> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /> <property name="username" value="root" /> <property name="password" value="mysql" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> </bean> <!-- 2. mybatis的sqlSession的工厂:SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- datasource: 引用数据源 --> <property name="dataSource" ref="ds" /> <!-- 别名包,设置之后自动扫描包下的类 --> <property name="typeAliasesPackage" value="com.stone.bean" /> </bean> <!-- 3. mybatis自动扫描加载sql映射文件:MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage: 指定Sql映射文件/接口所在的包,自动扫描 --> <property name="basePackage" value="com.stone.dao" /> <!-- sqlSessionFactory 引用定义好的sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 4. 事务管理:DataSourceTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- dataSource 引用上面定义的数据源 --> <property name="dataSource" ref="ds" /> </bean> <!-- 5.使用声明式事务 --> <!-- transaction-manager : 引用上面定义的事务管理器 --> <tx:annotation-driven transaction-manager="txManager" /> </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> </configuration>
Java Bean
package com.stone.bean; import java.text.SimpleDateFormat; import java.util.Date; public class Person { private int id; private String name; private Date birthday; public Person() { super(); } public Person(int id, String name, Date birthday) { super(); this.id = id; this.name = name; this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:SS"); return "Person [id=" + id + ", name=" + name + ", birthday=" + dateFormat.format(birthday) + "]"; } }
dao - 两个文件名必须一样
package com.stone.dao; import java.util.List; import com.stone.bean.Person; public interface PersonMapper { void save(Person person); void update(Person person); void delete(int id); Person findById(int id); List<Person> findAll(); }
<?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"> <!-- namespace:必须与对应的接口全类名一致 --> <mapper namespace="com.stone.dao.PersonMapper"> <!-- id:必须与对应接口的某个对应的方法一致 --> <insert id="save" parameterType="Person"> insert into person(name,birthday) values(#{name},#{birthday}) </insert> <update id="update" parameterType="Person"> update person set name=#{name},birthday=#{birthday} where id=#{id} </update> <delete id="delete" parameterType="int"> delete from person where id=#{id} </delete> <select id="findById" parameterType="int" resultType="Person"> select id,name,birthday from person where id=#{id} </select> <select id="findAll" resultType="Person"> select id,name,birthday from person </select> </mapper>
test
package com.stone.dao.test; import java.util.Date; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.stone.bean.Person; import com.stone.dao.PersonMapper; // 使用Spring的测试框架 @RunWith(SpringJUnit4ClassRunner.class) // 加载spring的配置文件beans.xml @ContextConfiguration("/beans.xml") public class SMTest { @Autowired private PersonMapper personMapper; @Test public void testAdd() { Person person = new Person(-1, "smName", new Date()); personMapper.save(person); } }
classpath
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/> <classpathentry kind="lib" path="lib/cglib-nodep-3.1.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/> <classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/> <classpathentry kind="lib" path="lib/mybatis-3.2.8.jar"/> <classpathentry kind="lib" path="lib/mybatis-spring-1.2.2.jar" sourcepath="D:/stono/javasoft/mybatis/spring-mybatis-spring-1.2.2.zip"/> <classpathentry kind="lib" path="lib/mysql-connector-java-5.1.7-bin.jar"/> <classpathentry kind="lib" path="lib/spring-aop-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-beans-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-context-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-core-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-expression-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-jdbc-4.1.4.RELEASE.jar" sourcepath="D:/stono/javasoft/spring/spring-framework-4.1.4.RELEASE/libs/spring-jdbc-4.1.4.RELEASE-sources.jar"/> <classpathentry kind="lib" path="lib/spring-test-4.1.4.RELEASE.jar"/> <classpathentry kind="lib" path="lib/spring-tx-4.1.4.RELEASE.jar"/> <classpathentry kind="output" path="bin"/> </classpath>