一、通过maven加载类库
spring-boot-parent
org.springframework.boot
2.1.16.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
8.0.21
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.projectlombok
lombok
1.18.12
provided
org.springframework.boot
spring-boot-maven-plugin
二、建立目录结构
三、修改配置
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
update source
set source_name=#{sourceName}
where id=#{sourceId}
4.5.2 StudentMapper.xml
select id,name,sex,address
from student
where id = #{studentId}
update student
set address=#{address}
where id=#{studentId}
五、主应用类
@SpringBootApplication
//@EnableTransactionManagement
public class SpringbootMybatisTx {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisTx.class, args);
}
}