pom.xml配置
Maven的pom中主要包括:
- MyBatis
- MyBatis-Spring
- Spring
- MySQL-Connector-Java
- Druid
Maven的pom中主要包括:
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mybatis3" /> <context:property-placeholder location="classpath:application.properties" /> <!--定义TransactionManager,在业务层使用@Transactional注解使Service成为事务管理的service,比如StudentService--> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--SqlSessionTemplate实现了SqlSession接口,线程安全,可以单例使用,--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!--sqlSessionFactory与主配置文件对应,可以直接使用configLocation指定主配置文件--> <!--也可以对主配置文件中的配置项单独配置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.mybatis3.domain"/> <property name="typeHandlersPackage" value="com.mybatis3.typehandlers"/> <property name="mapperLocations" value="classpath*:com/mybatis3/**/*.xml" /> <!--指向映射配置文件--> </bean> <!--数据源配置,在sqlSessionFactory引用--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
/** * */ package com.mybatis3.services; import com.mybatis3.domain.Tutor; import com.mybatis3.mappers.TutorMapper; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author Siva * */ @Service @Transactional public class TutorService { @Autowired private SqlSession sqlSession; //自动注入Spring定义的SqlSession private TutorMapper getTutorMapper(){ return sqlSession.getMapper(TutorMapper.class);//通过sqlSession获得相应的Mapper } public List<Tutor> findAllTutors() { return getTutorMapper().findAllTutors(); } public Tutor findTutorById(int tutorId) { return getTutorMapper().findTutorById(tutorId); } public Tutor findTutorByNameAndEmail(String name, String email) { return getTutorMapper().findTutorByNameAndEmail(name, email); } public Tutor createTutor(Tutor tutor) { getTutorMapper().insertTutor(tutor); return tutor; } public Tutor updateTutor(Tutor tutor) { getTutorMapper().updateTutor(tutor); return tutor; } public boolean deleteTutor(int tutorId) { boolean deleted = false; int nor = getTutorMapper().deleteTutor(tutorId); deleted = (nor == 1); return deleted; } public Tutor selectTutorById(int tutorId) { return getTutorMapper().selectTutorById(tutorId); } public Tutor selectTutorWithCoursesById(int tutorId) { return getTutorMapper().selectTutorWithCoursesById(tutorId); } }