SpringBoot-20-Mybatis代碼生成
什麼是Mybatis?
mybatis是apache的一個開源項目ibatis,2010年正式改名為mybatis。他是一個Java的持久層架構,ibatis提供的持久層架構包含 SqL Maps和Data Access Objects(Daos)
Mybatis特點:
- Mybatis簡單易學:可以通過官方文檔,快速掌握和實作開發
- 支援動态sql編寫
- 降低sql和代碼的高耦合性,将業務層和資料通路層分開
但是在操作Mybatis的時候會有很多重複性操作,為了使得我們減少設定mybatis中的配置檔案以及表互動的麻煩,出現了一些mybatis代碼生成方案:
- Mybatis Generator
- Mybtis Plus
我們這次介紹Mybatis Generator對Mybatis進行增強和Mybatis的資料操作。
Mybatis Generator實作
Mybatis Generator有多種實作方式xml配置實作方式,代碼配置方式以及通過插件方式實作,今天我們就介紹一個最簡單的Idea插件方式實作Mybatis Generator,better-mybatis-generator是一個可以根據表進行自動生成mybatis相關的代碼,(包括:dao、example、domain、xml)相關插件better-mybatis-generator**官方網址,安裝步驟如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiMGc902byZ2PhZDNmRWMldzMxIWOxU2YhdzNmRDMmZWO1ETN4YzNxE2LcBza5QTcsJja2FXLp1ibj1ycvR3Lc5Wanlmcv9CXt92YucWbp9WYpRXdvRnL2A3Lc9CX6MHc0RHaiojIsJye.jpg)
使用better-mybatis-generator生成Mybatis代碼過程
- 在Idea的DataBase中配置相對于的mysql連結步驟如下:
- 選擇相對應的表進行代碼生成,具體步驟如下圖:
添加Mybatis的相關依賴
建立項目以後我們要在pom.xml中添加mybatis和mysql驅動,初學者可以檢視SpringBoot項目建立學校如何建立項目。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
添加Mybatis相關配置
我們需要在application.yml配置資料庫連結,以及Mybatis中xml檔案存放位置。
server:
port: 8899
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
logging:
level:
com.learn.springboot: debug
配置Mybatis中mapper的包掃描位置
配置包掃描路徑有兩種方式(兩者配置方法選一個):
- 在Main入口配置
@MapperScan(basePackages = {"com.learn.springboot.mapper"})
@SpringBootApplication
public class SpringBootPart18Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootPart18Application.class, args);
}
}
- 添加Config配置類(個人推薦使用,為以後配置mybatis分頁,多資料源等作準備)
@Configuration
@MapperScan(basePackages = {"com.learn.springboot.mapper"})
public class MybatisConfig {
}
Service層的實作
- Service接口層的實作,代碼如下:
public interface StudentService {
void saveStudent(Student student);
void deleteStudent(Long id);
void updateStudent(Student student);
Student getStudent(Long id);
List<Student> getAll();
}
- Service層接口的實作代碼如下:
@Service
public class StudentServiceImpl implements StudentService {
/**
* mapper注入
*/
@Autowired
private StudentDaoMapper studentDaoMapper;
/**
* 添加學生
* @param student
*/
@Override
public void saveStudent(Student student) {
//代碼自動生成
studentDaoMapper.insert(student);
}
/**
* 根據ID删除學生
* @param id
*/
@Override
public void deleteStudent(Long id) {
//代碼自動生成
studentDaoMapper.deleteByPrimaryKey(id);
}
/**
* 更新學生
* @param student
*/
@Override
public void updateStudent(Student student) {
//代碼自動生成
studentDaoMapper.updateByPrimaryKeySelective(student);
}
/**
* 根據ID查詢學生
* @param id
* @return
*/
@Override
public Student getStudent(Long id) {
//代碼自動生成
return studentDaoMapper.selectByPrimaryKey(id);
}
/**
* 擷取所有資料
* @return
*/
@Override
public List<Student> getAll() {
//代碼自動生成
return studentDaoMapper.selectByExample(null);
}
}
Controller層的實作
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping("create")
public void saveStudent(@RequestBody Student student) {
studentService.saveStudent(student);
}
@GetMapping("/delete/{id}")
public void deleteStudent(@PathVariable("id") Long id) {
studentService.deleteStudent(id);
}
@PostMapping("update")
public void updateStudent(@RequestBody Student student) {
studentService.updateStudent(student);
}
@GetMapping("/select/{id}")
public Student getStudent(@PathVariable("id") Long id) {
return studentService.getStudent(id);
}
@GetMapping("/selectall")
public List<Student> getAll() {
return studentService.getAll();
}
}
測試
使用postman測試一下分别測試一下接口:
- http://localhost:8899/student/selectall GET方法
- http://localhost:8899/student/select/11 GET方法
- http://localhost:8899/student/update POST方法
- http://localhost:8899/student/delete/11 GET方法
- http://localhost:8899/student/create POST方法
postman測試接口方法如下圖:
如果您覺得本文不錯,歡迎關注支援,您的關注是我堅持的動力!
原創不易,轉載請注明出處,感謝支援!如果本文對您有用,歡迎轉發分享!