Mybatis七(MBG 逆向工程)

官方地址:http://www.mybatis.org/generator/

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

 

<1>编写mbg.xml配置文件 

"1.0" encoding="UTF-8"?>
DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">


    "testTables" targetRuntime="MyBatis3">
        
            
            "suppressAllComments" value="true" />
        
        
        "com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
            password="root">
        
        
        

        
        
            "forceBigDecimals" value="false" />
        

        
        "com.generator.model"
            targetProject=".\src">
            
            "enableSubPackages" value="false" />
            
            "trimStrings" value="true" />
        
        
        "com.generator.mapper"
            targetProject=".\src">
            
            "enableSubPackages" value="false" />
        
        
        "XMLMAPPER"
            targetPackage="com.generator.mapper" targetProject=".\src">
            
            "enableSubPackages" value="false" />
        
        
        "user" domainObjectName="User">
"department">

<2>建立pacage,对应目录

<3>编写生成代码类,运行后刷新目录

public class MybatisGeneratorTest {
    
    @Test
    public void generator() throws Exception{
 
        List warnings = new ArrayList();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("config/mbg.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);
 
    } 
}

<4>.生成的逆向工程测试

 public void testSelectByExample() {
        String resource = "mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            UserExample example = new UserExample();

             //Criteria类是UserExample类里面的内部类,Criteria类是干什么用的呢?它专门用于封装自定义查询条件的
             Criteria criteria = example.createCriteria();
             criteria.andGenderEqualTo("1");
             criteria.andLastNameLike("%张%");

        // 执行查询  

        List list = mapper.selectByExample(example);

for (User user : list) {
                System.out.println(user);
            }
            sqlSession.commit();
            // 释放资源
            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

你可能感兴趣的:(Mybatis七(MBG 逆向工程))