MyBatis Generator:代码生成器

目录

一、简介

二、使用步骤

1.编写MGB的配置文件

2.运行代码生成器代码

3.简单测试

4.QBC风格的带条件查询测试


一、简介

MyBatis Generator:

  • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂的sql定义需要我们手工编写

  • 官方文档地址:

http://www.mybatis.org/generator/

  • 官方工程地址

https://github.com/mybatis/generator/releases

二、使用步骤

1.编写MGB的配置文件


    
    
        
        
        
​
        
            
        
​
        
        
            
            
        
​
        
        
            
        
​
        
        
            
        
​
        
        
       
   

2.运行代码生成器代码

//如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容问题的出现
List warnings = new ArrayList();
        boolean overwrite = true;
        File configFile = new File("generatorConfig.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);

3.简单测试

private static SqlSessionFactory getSqlSessionFactory() throws IOException {
        //1.根据xml配置文件创建一个SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(resourceAsStream);
    }
    //查询id为1的员工
    @Test
    public void test01() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try {
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            Employee employee = employeeMapper.selectByPrimaryKey(1);
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }
    }

4.QBC风格的带条件查询测试

//查询名字带c或者id为1的员工
    @Test
    public void test02() throws Exception {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        try {
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            //xxxExample就是封装查询条件的
            EmployeeExample employeeExample = new EmployeeExample();
            //创建一个Criteria,用来拼装查询条件
            EmployeeExample.Criteria criteria = employeeExample.createCriteria();
            //查询名字带c的
            criteria.andNameLike("%c%");
            EmployeeExample.Criteria criteria1 = employeeExample.createCriteria();
            //或者id为1的
            criteria1.andIdEqualTo(1);
            //两个条件用or相连
            employeeExample.or(criteria1);
            List employees = employeeMapper.selectByExample(employeeExample);
            for (Employee employee : employees) {
                System.out.println(employee);
            }
        } finally {
            sqlSession.close();
        }

 

你可能感兴趣的:(MyBatis)