代码地址:https://gitee.com/codinginn/spring-boot-tutorial/tree/master/001-mybatis-reverse
1. 准备数据库create database springboot;
use springboot;
DROp TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_student
-- ----------------------------
INSERT INTO `t_student` VALUES ('1', 'jinkesi', '22');
INSERT INTO `t_student` VALUES ('2', 'lisi', '33');
INSERT INTO `t_student` VALUES ('3', 'wwangwu', '21');
INSERT INTO `t_student` VALUES ('4', 'liuneng', '12');
INSERT INTO `t_student` VALUES ('5', 'asan', '34');
INSERT INTO `t_student` VALUES ('6', 'json', '32');
INSERT INTO `t_student` VALUES ('7', 'jack', '21');
INSERT INTO `t_student` VALUES ('8', 'anny', '23');
INSERT INTO `t_student` VALUES ('9', 'tom', '24');
2. 在pom.xml中引入mybatis和mysql的jar包
3. 在application.properties中配置数据库资源4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.4 com.example mybatisreverse 0.0.1-SNAPSHOT 001-mybatis-reverse 001-mybatis-reverse 11 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 mysql mysql-connector-java org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.6 GeneratorMapper.xml true true
server.port=8080 server.servlet.context-path=/ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDateTimeCode=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root4 操作Mybatis逆向工程
module根目录下创建GeneratorMapper.xml
编辑GeneratorMapper.xml
- 使用 Mybatis 逆向工程生成接口、映射文件以及实体 bean
双击如下标注的位置
此时项目结构如下图所示:
生成的文件如下
Student.java
package com.hashnode.mybatisreverse.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
StudentMapper.java
package com.hashnode.mybatisreverse.mapper;
import com.hashnode.mybatisreverse.model.Student;
//使用@MapperScan或在main函数所在的类使用@MapperScan(basePackages = "com.hashnode.mybatisreverse.mapper")将Mapper类放入容器,这里使用的是后者
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
StudentMapper.xml
5 编写Controller,Service代码id, name, age
StudentService.java
package com.hashnode.mybatisreverse.service;
import com.hashnode.mybatisreverse.mapper.StudentMapper;
import com.hashnode.mybatisreverse.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public Student queryStudentById(Integer id){
Student student = studentMapper.selectByPrimaryKey(id);
return student;
}
}
StudentController.java
package com.hashnode.mybatisreverse.controller;
import com.hashnode.mybatisreverse.model.Student;
import com.hashnode.mybatisreverse.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller()
@RequestMapping("student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "query")
public @ResponseBody Object query(){
Student student = studentService.queryStudentById(1);
return student;
}
}
Application.java代码如下
package com.hashnode.mybatisreverse;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.hashnode.mybatisreverse.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
运行结果如下:



