持续学习&持续更新中…
守破离
【Java从零到架构师第③季】【43】SpringBoot-MyBatis
- 引入依赖
- 数据源配置
- MyBatis配置
- 扫描DAO
- MyBatis主配置—XML
- MyBatis主配置—注解
- MyBatis主配置—application.yml
- starter的命名规范
- useGeneratedKeys
- 实例
- 项目结构
- Application
- MyBatisConfig
- application.yml 与 MyBatis映射文件
- mybatis-config.xml
- index.html
- controller
- service
- dao
- 参考
http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
数据源配置org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 mysql mysql-connector-java com.alibaba druid-spring-boot-starter 1.2.1 org.springframework.boot spring-boot-starter-thymeleaf
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test_mybatis?serverTimezone=UTC
druid:
initial-size: 5
max-active: 10
MyBatis配置
扫描DAO
MyBatis主配置—XML
mybatis: config-location: classpath:mybatis-config.xml
MyBatis主配置—注解
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer customizer() {
return (configuration) -> {
configuration.setMapUnderscoreToCamelCase(true);
};
}
}
MyBatis主配置—application.yml
mybatis:
configuration:
map-underscore-to-camel-case: true
starter的命名规范
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
useGeneratedKeysuseGeneratedKeys直接写在映射文件中:
实例 项目结构 ApplicationINSERT INTO skill(name, level) VALUES (#{name}, #{level})
@SpringBootApplication
@MapperScan("programmer.lp.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MyBatisConfig
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer customizer() {
return (configuration) -> {
configuration.setMapUnderscoreToCamelCase(true);
configuration.setUseGeneratedKeys(true);
};
}
}
application.yml 与 MyBatis映射文件
server:
servlet:
context-path: /mybatis
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/test_mybatis?serverTimezone=UTC
druid:
initial-size: 5
max-active: 10
mybatis:
type-aliases-package: programmer.lp.domain
# mapper-locations: classpath:mappers*.properties
***.java -->
mybatis-config.xml
index.html
Skill
保存
删除
controller
@RestController
@RequestMapping("/skills")
public class SkillController {
@Autowired
private SkillService service;
@GetMapping("/list")
public List list() {
return service.list();
}
@GetMapping("/get")
public Skill get(Integer id) {
return service.get(id);
}
@PostMapping("/save")
public String save(Skill skill) {
String[] msgs;
Integer id = skill.getId();
if (id != null && id > 0) {
msgs = new String[] {"更新成功", "更新失败"};
} else {
msgs = new String[] {"添加成功", "添加失败"};
}
return service.save(skill) ? msgs[0] + "_" + skill.getId() : msgs[1];
}
@PostMapping("/remove")
public String remove(Integer id) {
return service.remove(id) ? "删除成功" : "删除失败";
}
}
service
@Service
@Transactional
public class SkillServiceImpl implements SkillService {
@Autowired
private SkillDao dao;
@Override
public boolean save(Skill skill) {
System.out.println(skill.getId());
Integer id = skill.getId();
if (id == null || id < 1) {
return dao.save(skill);
}
return dao.update(skill);
}
@Override
@Transactional(readOnly = true)
public List list() {
return dao.list();
}
@Override
@Transactional(readOnly = true)
public Skill get(Integer id) {
return dao.get(id);
}
@Override
public boolean remove(Integer id) {
return dao.remove(id);
}
}
public interface SkillService {
boolean save(Skill skill);
List list();
Skill get(Integer id);
boolean remove(Integer id);
}
dao
public interface SkillDao {
boolean save(Skill skill);
boolean update(Skill skill);
boolean remove(Integer id);
@Select("SELECT * FROM skill")
List list();
@Select("SELECT * FROM skill WHERe id = #{id}")
Skill get(Integer id);
}
参考INSERT INTO skill(name, level) VALUES (#{name}, #{level}) UPDATE skill SET name = #{name}, level = #{level} WHERe id = #{id} DELETE FROM skill WHERe id = #{id}
小码哥-李明杰: Java从0到架构师③进阶互联网架构师.
本文完,感谢您的关注支持!



