1、集成mybatis。
2、事务管理
一、集成mybatis
备注:通过mapper.xml文件来进行与数据库的操作(sql语句灵活,较常使用)
一、集成mybatis
1、引入jar包
<!-- mysql 数据库驱动. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-boot 与 mybatis配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version><!-- 最新版本 -->
</dependency>
2、新建表 t_student,t_teacher
3、建表格对应的实体类(set和get方法就不累述)
4、建mapper接口映射xml。
@Repository("stuDao")
public interface StudentMappper {
public List<StudentEbo> listStu(@Param("name") String name);
public StudentEbo getStuById(@Param("id") int id);
public void addStu(StudentEbo stu);
}
5、src/main/resources下新建mappers文件夹,新建studentMapper.xml文件
<?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">
<mapper namespace="com.hunqian.mapper.dao.StudentMappper">
<resultMap type="com.hunqian.model.StudentEbo" id="StudentMap">
<result property="id" column="id" jdbcType="INTEGER" javaType="Integer" />
<result property="name" column="name" jdbcType="VARCHAR" javaType="String" />
<result property="createTime" column="create_time" jdbcType="TIMESTAMP" javaType="java.sql.Timestamp" />
</resultMap>
<sql id="studentEboMap">
SELECT
s.id,s.name,s.create_time
FROM
t_student AS s
</sql>
<select id="getStuById" resultMap="StudentMap">
<include refid="studentEboMap"/>
<where>
1=1
<if test=" id > 0">
AND s.id = ${id}
</if>
</where>
</select>
<select id="listStu" resultMap="StudentMap">
<include refid="studentEboMap"/>
<where>
1=1
<if test=" name != null and name != '' ">
AND s.name LIKE '%${name}%'
</if>
</where>
</select>
<insert id="addStu" parameterType="com.hunqian.model.StudentEbo" useGeneratedKeys="true" keyProperty="id">
insert into t_student
(id,name,create_time)
value
(null,#{name},#{createTime})
</insert>
</mapper>
备注:此处注意mapper接口与实体类的路径,必须与xml文件中的一致。
6、配置application.properties文件中添加数据库连接的配置信息。
spring.datasource.url = jdbc\:mysql\://localhost\:3306/******
spring.datasource.username = ****
spring.datasource.password = ******
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#spring.datasource.max-active=20
#spring.datasource.max-idle=8
#spring.datasource.min-idle=8
#spring.datasource.initial-size=10
mybatis.mapper-locations=classpath\:mappers/*.xml
注意:数据库的连接要与自己的意志
7、写关于mapper接口的调用(注意此步也可省略,直接调用mapper接口,但本文按实际开发大致流程走,所以写业务接口与实现)
IStudentService.java
package com.hunqian.service;
import java.util.List;
import com.hunqian.model.StudentEbo;
import com.hunqian.model.TeacherEbo;
public interface IStudentService {
public List<StudentEbo> listStu(String name);
public StudentEbo getStuById(int id);
public StudentEbo addStu(StudentEbo stu);
}
StudentService.java
package com.hunqian.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.hunqian.mapper.dao.StudentMappper;
import com.hunqian.model.StudentEbo;
import com.hunqian.model.TeacherEbo;
import com.hunqian.service.IStudentService;
import com.hunqian.service.ITeacherService;
@Service("stuService")
public class StudentService implements IStudentService {
@Autowired
private StudentMappper stuDao;
@Autowired
private ITeacherService tchServcie;
public List<StudentEbo> listStu(String name){
if(name == null || name.trim().length() == 0)
return new ArrayList<StudentEbo>();
return stuDao.listStu(name);
}
public StudentEbo getStuById(int id){
if(id < 0)
return null;
return stuDao.getStuById(id);
}
public StudentEbo addStu(StudentEbo stu){
stuDao.addStu(stu);
return stu;
}
}
8、启动类启动关于接口和mapper接口扫描
备注:关于注解@ComponentScan、@MapperScan的意思本文就不做累述,请自行查阅
@ComponentScan(basePackages="com.hunqian")
@MapperScan("com.hunqian.mapper")
9、启动类添加测试
1>bean注入StudentService类
@Autowired
private IStudentService stuServcie;
2>添加测试
@RequestMapping("addstu")
public String addstu(){
StudentEbo stu = new StudentEbo();
stu.setName("魂牵");
stu.setCreateTime(new Timestamp(System.currentTimeMillis()));
StudentEbo s = stuServcie.addStu(stu);
return s.toString();
}
@RequestMapping("teststu")
public String stu(){
StudentEbo stu = stuServcie.getStuById(11);
return stu.getName();
}
10、运行结果
备注:这样关于spring boot与mybatis的基本整合已经成功。
二、关于事务配置
备注:测试事务标准 ,同时向两张表中添加数据,若未配置事务,一张表数据添加出错,另一张表仍然可添加成功。若配置事务,则会发生事务回滚,两条数据均添加不成功。
1、未配置事务
按t_student的方式添加一张新表的映射操作(就不做累述)。
本文添加t_teacher 表,以及相关的xml、mapper、service等
IStudentServcie.java中添加
public String testTrans(StudentEbo stu,TeacherEbo tch);
实现类StudentService.java中:
注入TeacherService类
@Autowired
private ITeacherService tchServcie;
实现方法testTrans
public String testTrans(StudentEbo stu,TeacherEbo tch){
//添加老师
tchServcie.addTch(tch);
//添加学生
stuDao.addStu(stu);
return "添加成功";
}
启动类中添加测试:
@RequestMapping("addtran")
public String addtran(){
TeacherEbo t = new TeacherEbo();
t.setName("与1111");
t.setCreateTime(new Date());
StudentEbo stu = new StudentEbo();
stu.setCreateTime(new Timestamp(System.currentTimeMillis()));
try {
tchServcie.addTch(t);
stuServcie.addStu(stu);
} catch (Exception e) {
e.printStackTrace();
}
return "添加tran";
}
运行结果:虽然报错,但t_teacher仍然添加数据成功。
2、配置事务
业务层方法上添加事务注解
备注:关于事务的详细配置,请根据个人业务需要自行设置。
启动类中开启事务
@EnableTransactionManagement //开启事务
再次测试发现报错两条数据均不能添加成功。说明事务配置成功,发生的事务回滚。
github地址:https://github.com/hunqian/spring-boot-more-mybatis.git
本文仅限关于mybatis和事务的基本配置,详细配置请根据自身框架和业务需要进一步完善。