SpringBoot-20-Mybatis code generation
What is Mybatis?
Mybatis is an open source project of apache ibatis, which was officially renamed mybatis in 2010. It is a Java persistence layer framework. The persistence layer framework provided by ibatis includes SqL Maps and Data Access Objects(Daos)
Mybatis features:
-
Mybatis is easy to learn: you can quickly master and realize development through official documents
-
Support dynamic sql writing
-
Reduce the high coupling between sql and code, and separate the business layer and data access layer
However, there are many repetitive operations when operating mybatis. In order to reduce the trouble of setting configuration files and table interaction in mybatis, some mybatis code generation schemes appear:
-
Mybatis Generator
-
Mybtis Plus
This time, we introduce the enhancement of Mybatis by Mybatis Generator and the data operation of Mybatis.
Mybatis Generator implementation
There are many ways to implement Mybatis Generator, including xml configuration, code configuration and plug-in. Today we will introduce the simplest Idea plug-in to implement Mybatis Generator. Better Mybatis Generator is a code that can automatically generate mybatis related code according to the table, (including dao, example, domain and xml) the official website of the related plug-ins better Mybatis Generator * *. The installation steps are as follows:
The process of generating Mybatis code using better Mybatis generator
- The steps for configuring the link to mysql Idea are as follows:
- Select the corresponding table for code generation. The specific steps are as follows:
Add related dependencies of Mybatis
After the project is created, we will create a project in POM Add mybatis and mysql drivers to XML, which can be viewed by beginners SpringBoot project creation How schools create projects.
<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>
Add Mybatis related configuration
We need to be in application YML configuration database link and the storage location of xml files in Mybatis.
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
Configure the package scanning location of mapper in Mybatis
There are two ways to configure the package scanning path (choose one of the two configuration methods):
- Configure in Main portal
@MapperScan(basePackages = {"com.learn.springboot.mapper"}) @SpringBootApplication public class SpringBootPart18Application { public static void main(String[] args) { SpringApplication.run(SpringBootPart18Application.class, args); } }
- Add Config configuration class (recommended for personal use to prepare for configuring mybatis paging and multiple data sources in the future)
@Configuration @MapperScan(basePackages = {"com.learn.springboot.mapper"}) public class MybatisConfig { }
Implementation of Service layer
- The implementation code of Service interface layer is as follows:
public interface StudentService { void saveStudent(Student student); void deleteStudent(Long id); void updateStudent(Student student); Student getStudent(Long id); List<Student> getAll(); }
- The implementation code of Service layer interface is as follows:
@Service public class StudentServiceImpl implements StudentService { /** * mapper injection */ @Autowired private StudentDaoMapper studentDaoMapper; /** * Add student * @param student */ @Override public void saveStudent(Student student) { //Automatic code generation studentDaoMapper.insert(student); } /** * Delete student by ID * @param id */ @Override public void deleteStudent(Long id) { //Automatic code generation studentDaoMapper.deleteByPrimaryKey(id); } /** * Update students * @param student */ @Override public void updateStudent(Student student) { //Automatic code generation studentDaoMapper.updateByPrimaryKeySelective(student); } /** * Query students by ID * @param id * @return */ @Override public Student getStudent(Long id) { //Automatic code generation return studentDaoMapper.selectByPrimaryKey(id); } /** * Get all data * @return */ @Override public List<Student> getAll() { //Automatic code generation return studentDaoMapper.selectByExample(null); } }
Implementation of Controller layer
@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(); } }
test
Use postman to test the interfaces:
-
http://localhost:8899/student/selectall GET method
-
http://localhost:8899/student/select/11 GET method
-
http://localhost:8899/student/update POST method
-
http://localhost:8899/student/delete/11 GET method
-
http://localhost:8899/student/create POST method
The postman test interface method is shown in the figure below:
If you think this article is good, welcome to pay attention and support. Your attention is the driving force of my persistence!
Original is not easy, reprint please indicate the source, thank you for your support! If this article is useful to you, welcome to forward and share!