MyBatis-06 逆向工程

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

官方文档地址为http://www.mybatis.org/generator/,官方工程地址为https://github.com/mybatis/generator/releases。

其中,MyBatis中逆向工程的具体测试代码下载地址:https://download.csdn.net/download/bingbeichen/10538138。


使用步骤

第一步,导入所需要的jar包,包括log4j-1.2.17.jar、mybatis-3.4.1.jar、mybatis-generator-core-1.3.2.jar、mysql-connector-java-5.1.7-bin.jar。

第二步,编写MBG的配置文件。





<generatorConfiguration>

    
    
    <context id="DB2Tables" targetRuntime="MyBatis3Simple">

        
        <jdbcConnection 
            driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis" 
            userId="root" 
            password="root">
        jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.qiaobc.mybatis.bean"
            targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mapper.xml"
            targetProject=".\conf">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.qiaobc.mybatis.mapper" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>

        
        <table tableName="tbl_dept" domainObjectName="Department">table>
        <table tableName="tbl_employee" domainObjectName="Employee">table>

    context>
generatorConfiguration>

第三步:运行生成器代码,以根据数据表自动生成对应的JavaBean、Mapper接口和SQL映射文件。

public class MBGTest {

    @Test
    public void test() throws Exception {
        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);
    }

}

第四步,测试。

public class MBGTest {

    /**
     * 测试targetRuntime="MyBatis3Simple" : 可以生成基本的增删改查
     * @throws IOException
     */
    @Test
    public void testMyBatis3Simple() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            List emps = mapper.selectAll();
            for (Employee emp : emps) {
                System.out.println(emp.getLastName());
            }
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 测试targetRuntime="MyBatis3" : 可以生成带条件的增删改查
     * @throws IOException
     */
    @Test
    public void testMyBatis3() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            // XxxExample用于封装查询条件
            // 1.查询所有员工姓名
            List emps = mapper.selectByExample(null);

            // 2.1 使用Example的Criteria封装查询条件
            EmployeeExample example = new EmployeeExample();
            Criteria criteria = example.createCriteria();
            criteria.andLastNameLike("%e%");
            criteria.andGenderEqualTo("1");

            Criteria criteria2 = example.createCriteria();
            criteria2.andEmailLike("%e%");

            example.or(criteria);

            // 2.查询员工中名字含有e且性别为1的员工,或者email中含有e
            emps = mapper.selectByExample(example);
            for (Employee emp : emps) {
                System.out.println(emp.getLastName());
            }
        } finally {
            sqlSession.close();
        }
    }

    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

}

你可能感兴趣的:(MyBatis-06 逆向工程)