声明:本文参考《Java Persistence with MyBatis 3》 第五章
1 使用maven进行依赖管理 pom.xml
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.3.RELEASE</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.1.3.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency>
2 在 spring的配置文件中添加MyBatis的配置 spring.xml
<beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/elearning"/> <property name="username" value="root"/> <property name="password" value="admin"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliases" value="com.mybatis3.domain.Student,com.mybatis3.domain.Tutor"/> <property name="typeAliasesPackage" value="com.mybatis3.domain"/> <property name="typeHandlers" value="com.mybatis3.typehandlers.PhoneTypeHandler"/> <property name="typeHandlersPackage" value="com.mybatis3.typehandlers"/> <property name="mapperLocations" value="classpath*:com/mybatis3/**/*.xml"/> <property name="configLocation" value="WEB-INF/mybatis-config.xml"/> </bean> </beans>
通过上面的配置我们得到了SqlSessionFactory对象,好接下来我们就可以使用该对象获取SqlSession来进行数据库的操作了。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
通过sqlSessionFactory得到SqlSessionTemplate对象,并注入到Dao中使用MyBatis定义的Mapper接口进行编程。
至于注入方式我们有两种:
1 xml的方式
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
public class StudentDaoImpl implements StudentDao{ private SqlSession sqlSession; public void setSqlSession(SqlSession session) { this.sqlSession = session; } public void createStudent(Student student){ StudentMapper mapper = this.sqlSession.getMapper(StudentMapper.class); mapper.insertStudent(student); } }
2 声明的方式
@Repository public class StudentDaoImpl implements StudentDao { private SqlSession sqlSession; @Autowired public void setSqlSession(SqlSession session) { this.sqlSession = session; } public void createStudent(Student student) { StudentMapper mapper = this.sqlSession.getMapper(StudentMapper.class); mapper.insertStudent(student); } }
3 使用SqlSessionDaoSupport的方式,这种方式Spring自动给我们注入SqlSession
public class StudentMapperImpl extends SqlSessionDaoSupport implements StudentMapper { public void createStudent(Student student) { StudentMapper mapper = getSqlSession().getMapper(StudentMapper.class); mapper.insertAddress(student.getAddress()); //Custom logic mapper.insertStudent(student); } } <bean id="studentMapper" class="com.mybatis3.dao.StudentMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
上面是使用传统的方式进行的数据访问,接下来我们使用Mapper Interface (声明注解)的形式
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis3.mappers.StudentMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> public interface StudentMapper { @Select("select stud_id as studId, name, email, phone from students where stud_id=#{id}") Student findStudentById(Integer id); }
xml中声明出Bean,注入到Service中
<bean id="studentService" class="com.mybatis3.services. StudentService"> <property name="studentMapper" ref="studentMapper" /> </bean> public class StudentService { private StudentMapper studentMapper; public void setStudentMapper (StudentMapperstudentMapper) { this. studentMapper = studentMapper; } public void createStudent(Student student) { this.studentMapper.insertStudent(student); } }
还可以简化配置使用扫描包的形式发现Mapper接口 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis3.mappers" /> </bean> MyBatis-Spring-1.2.0 • <mybatis:scan/> 在xml中配置 • @MapperScan annotation (requires Spring 3.1+) 在configuration 配置类中声明
<bean id="transactionManager" class="org.springframework.jdbc. datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> 直接使用注解声明事务 @Service @Transactional public class StudentService { @Autowired private StudentMapper studentMapper; public Student createStudent(Student student) { studentMapper.insertAddress(student.getAddress()); if(student.getName().equalsIgnoreCase("")){ throw new RuntimeException("Student name should not be empty."); } studentMapper.insertStudent(student); return student; } }