mybatis-逆向工程


目录
  • 环境
  • 测试
    • 简单查询
    • 模糊查询
    • 两个条件

环境


mybatis-generator-core-1.4.0.jar:https://github.com/mybatis/generator/releases
文档:http://mybatis.org/generator/

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80022
 Source Host           : localhost:3306
 Source Schema         : school

 Target Server Type    : MySQL
 Target Server Version : 80022
 File Encoding         : 65001

 Date: 20/11/2021 17:20:00
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `tea_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `tea_age` int(0) NULL DEFAULT NULL,
  `tea_email` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 65 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, 'set', 22, 'tom@qq.com');
INSERT INTO `teacher` VALUES (2, 'lili', 22, 'lili@qq.com');
INSERT INTO `teacher` VALUES (3, 'lucy', 22, 'lucy@qq.com');
INSERT INTO `teacher` VALUES (4, 'lisi', 22, 'lisi@qq.com');
INSERT INTO `teacher` VALUES (62, 'teacher1', 22, '1@qq.com');
INSERT INTO `teacher` VALUES (63, 'teacher2', 22, '2@qq.com');
INSERT INTO `teacher` VALUES (64, 'teacher3', 22, '3@qq.com');

SET FOREIGN_KEY_CHECKS = 1;
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
db.username=root
db.password=123456
#dbconfig.properties
# Global logging configuration
log4j.rootLogger=debug, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
#log4j.properties
<?xml version="1.0" encoding="UTF-8" ?>



    
    
    
        
        
        
        
        
        
    

    
        
    

    
        
        
            
            
                
                
                
                
            
        
    

    
    
        
        
    
    
    
        
    





<?xml version="1.0" encoding="UTF-8"?>




    
    
    
        
        

        
        
            
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        
        

测试

简单查询

  @Test
  public void test() throws InterruptedException, SQLException, IOException, InvalidConfigurationException, XMLParserException {
    List warnings = new ArrayList<>();
    boolean overwrite = true;
    File configFile = new File("mgb.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    myBatisGenerator.generate(null);
  }



  @Test
  public void test1() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
      Teacher teacher = mapper.selectByPrimaryKey(1);
      System.out.println("teacher = " + teacher);
    }
  }

模糊查询

  @Test
  public void test2() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
      TeacherExample example = new TeacherExample();
      TeacherExample.Criteria criteria = example.createCriteria();
      criteria.andTeaNameLike("%l%");
      List teachers = mapper.selectByExample(example);
      for (Teacher teacher : teachers) {
        System.out.println(teacher.getTeaName());
      }
    }
  }

两个条件

  @Test
  public void test3() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
      TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
      TeacherExample example = new TeacherExample();
      TeacherExample.Criteria criteria = example.createCriteria();
      criteria.andTeaNameLike("%l%");
      TeacherExample.Criteria criteria1 = example.createCriteria();
      criteria1.andTeaEmailLike("%1%");
      example.or(criteria1);
      List teachers = mapper.selectByExample(example);
      for (Teacher teacher : teachers) {
        System.out.println(teacher.getTeaName() + "\t" + teacher.getTeaEmail());
      }
    }
  }