聲明:本文參考《Java Persistence with MyBatis 3》 第五章 java
1 使用maven進行依賴管理 pom.xml
node
<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
mysql
<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來進行數據庫的操做了。spring
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
經過sqlSessionFactory獲得SqlSessionTemplate對象,並注入到Dao中使用MyBatis定義的Mapper接口進行編程。sql
至於注入方式咱們有兩種:數據庫
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 聲明的方式session
@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自動給咱們注入SqlSessionmybatis
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 (聲明註解)的形式app
<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; } }