MyBatis 與 Spring 的整合
1.在pom.xml文件中添加下列內容,導入相關的jar包。
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
2.Spring 管理SqlSessionFactory,在Spring IoC容器配置文件Context.xml中添加下列內容:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/kfcl/hps/information/dao/mapper/*.xml"/>
<property name="typeAliasesPackage" value="com.kfcl.hps.information.model"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"></property>
</bean>
</property>
</bean>
其中,dataSource爲Spring管理的的連接池對象,mapperLocations填寫MyBatis映射文件的路徑,這裏與Spring整合使用自動掃描機制,會自動搜索該路徑所有符合條件的映射文件並解析,typeAliasesPackage填寫Model類的路徑,主要是掃描類型別名,Model類使用註解方式爲Model全限定類名設置別名。
3.Spring管理SqlSessionTemplate,在Spring IoC容器配置文件Context.xml中添加下列內容:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
4.爲每個dao接口定義bean。
我們可以在Context.xml配置文件裏爲每個dao接口定義bean,但mybatis還提供了一種更簡便的自動掃描註解的機制,即<mybatis:scan/>
。 配置<mybatis:scan/>
,需要在Context.xml配置文件裏添加:
<mybatis:scan base-package="com.example.dao"/>
5.配置事務管理器。
在Context.xml文件中添加下列代碼配置事務管理器:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
添加下列代碼,啓用事務註解類,即可使用@Transactional註解啓用事務。
<!-- 啓用事務註釋 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
下面是關於事務管理的一個例子:
如果不加註解@Transactional的話,事務不會滾,即事務a生效,事務b不執行
到這裏已經完成了MyBatis和Spring 的整合。
下面是一個整合後的例子:
1.建表:
--創建學生表
create table Student(
stuNo number,
stuName varchar2(16),
phone varchar2(11)
);
--序列
create sequence student_seq;
2.創建對應的Model類:StudentModel.class,包路徑爲:com.example.model
public class StudentModel {
private int stuNo;
private String stuName;
private String phone;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.創建dao層接口,包路徑爲:com.example.dao
public interface IStudentDao {
//增加
public void create(StudentModel student) throws Exception;
//刪除
public void delete(StudentModel student) throws Exception;
//修改
public void update(StudentModel student) throws Exception;
//查詢-全部
public List<StudentModel> selectListByAll() throws Exception;
}
4.創建映射文件IStudentDaoMapper.xml,包路徑:com.example.dao.mapper
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.IStudentDao">
<insert id="create" parameterType="com.example.model.StudentModel">
insert into Student(StuNo,StuName,Phone)
values(Student_SEQ.nextval,#{stuName},#{phone})
</insert>
<update id="update" parameterType="com.example.model.StudentModel">
update Student set StuName=#{stuName}, Phone=#{phone}
where StuNo=#{stuNo}
</update>
<delete id="delete" parameterType="com.example.model.StudentModel">
delete from HT_Neighbourhood where StuNo=#{stuNo}
</delete>
<!-- 查詢-全部 -->
<select id="selectListByAll" resultType="com.example.model.StudentModel">
select * from Student
</select>
</mapper>
5.創建業務層接口IStudentService,包路徑:com.example.service
public interface IStudentService {
//新增
public void add(StudentModel student) throws Exception;
//修改
public void modify(StudentModel student) throws Exception;
//刪除
public void delete(StudentModel student) throws Exception;
//查詢-全部記錄
public List<StudentModel> listByAll() throws Exception;
}
6.創建業務層實現類StudentServiceImpl,包路徑:com.example.service.impl
@Service("studentService")
public class StudentServiceImpl implements IStudentService {
private IStudentDao studentDao;
@Autowired
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void add(StudentModel student) throws Exception {
studentDao.create(student);
}
@Override
public void modify(StudentModel student) throws Exception {
studentDao.update(student);
}
@Override
public void delete(StudentModel student) throws Exception {
studentDao.delete(student);
}
@Override
public List<StudentModel> listByAll() throws Exception {
return studentDao.selectListByAll();
}
}
7.修改Ioc容器配置內容,修改所有的包路徑:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/example/dao/mapper/*.xml"/>
<property name="typeAliasesPackage" value="com.example.model"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"></property>
</bean>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 自動掃描 -->
<mybatis:scan base-package="com.example.dao"/>
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 啓用事務註釋 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
8.創建測試類
public class StudentServiceTest {
public static final void main(String[] args) throws Exception {
ApplicationContext wac=new ClassPathXmlApplicationContext("context.xml") ;
IStudentService studentService=wac.getBean("studentService",IStudentService.class);
List<StudentModel> list=studentService.listByAll();
for(StudentModel student:list) {
System.out.println(student.getStuNo()+" "+student.getStuName()+" "+student.getPhone());
}
}
}
9.執行測試類: