一、通过maven加载类库
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.16.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、建立目录结构
三、修改配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=111111
mybatis.type-aliases-package=com.aliyun.edu.dxh
mybatis.mapper-locations=classpath*:mappers/*.xml
四、编写控制器、业务、PO、mapper、xml:
4.1 控制器:
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/detail")
public Student detail(@RequestParam("id") Long id) {
return studentService.detail(id);
}
@RequestMapping("/modify")
public int modify() {
return studentService.modifyStudentAddress(1L, "test-4", 1L, "chinese-4");
}
}
4.2 编写service
4.2.1 service的接口层
public interface StudentService {
public Student detail(Long id);
int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName);
}
4.2.2 service的实现层
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Resource
private SourceMapper sourceMapper;
public Student detail(Long id) {
return studentMapper.detail(id);
}
@Transactional
public int modifyStudentAddress(Long studentId, String address, Long sourceId, String sourceName) {
studentMapper.modifyStudentAddress(studentId, address);
int a = 1/0;
sourceMapper.modifySource(sourceId, sourceName);
return 1;
}
}
4.3 mapper层
4.3.1 SourceMapper.java (课程表对应的mapper接口)
@Mapper
public interface SourceMapper {
public int modifySource(@Param("sourceId") Long sourceId, @Param("sourceName") String sourceName);
}
4.3.2 StudentMapper.java (学生信息表对应的mapper接口)
@Mapper
public interface StudentMapper {
public Student detail(@Param("studentId") Long id);
public int modifyStudentAddress(@Param("studentId") Long id, @Param("address") String address);
}
4.4 po(持久化对象:表对应的实体类)
4.4.1 Source.java (课程表对应的实体类)
@Data
public class Source {
private Long id;
private String sourceName;
private String isDelete;
}
4.4.2 Student.java(学生表对应的实体类)
@Data
public class Student {
private Long id;
private String name;
private String sex;
private String address;
}
4.5、数据库操作对应的XML(resources/mappers)
4.5.1 SourceMapper.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.aliyun.edu.dxh.mapper.SourceMapper">
<resultMap id="student" type="com.aliyun.edu.dxh.po.Source">
<id column="id" jdbcType="BIGINT" property="id"/>
<id column="source_name" jdbcType="VARCHAR" property="sourceName"/>
<id column="is_delete" jdbcType="VARCHAR" property="isDelete"/>
</resultMap>
<update id="modifySource">
update source
set source_name=#{sourceName}
where id=#{sourceId}
</update>
</mapper>
4.5.2 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.aliyun.edu.dxh.mapper.StudentMapper">
<resultMap id="student" type="com.aliyun.edu.dxh.po.Student">
<id column="id" jdbcType="BIGINT" property="id"/>
<id column="name" jdbcType="VARCHAR" property="name"/>
<id column="sex" jdbcType="VARCHAR" property="sex"/>
<id column="address" jdbcType="VARCHAR" property="address"/>
</resultMap>
<select id="detail" resultType="student">
select id,name,sex,address
from student
where id = #{studentId}
</select>
<update id="modifyStudentAddress">
update student
set address=#{address}
where id=#{studentId}
</update>
</mapper>
五、主应用类
@SpringBootApplication
//@EnableTransactionManagement
public class SpringbootMybatisTx {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisTx.class, args);
}
}