栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实操MyBatis逆向之mybatis-generator-maven-plugin

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实操MyBatis逆向之mybatis-generator-maven-plugin

代码地址: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包


    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
                
            
        
    


3. 在application.properties中配置数据库资源
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=root
4 操作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




    
  
    
    
    
  
    
  
    id, name, age