victory的博客

长安一片月,万户捣衣声

0%

MyBatis | 逆向工程

逆向工程

1.项目目录

2.导入逆向工程的jar包

mybatis-generator-core-1.3.2.jar

3.编写MBG的配置文件

<?xml version="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">

<generatorConfiguration>

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <!-- 设置连接数据库的信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/ssm"
        userId="root"
        password="root">
    </jdbcConnection>

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

    <!-- javabean的生成策略 -->
    <javaModelGenerator targetPackage="com.atguigu.bean" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>


    <!-- 映射文件的生成策略 -->
    <sqlMapGenerator targetPackage="com.atguigu.mapper"  targetProject=".\conf">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- mapper接口的生成策略 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mapper"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!-- 设置要将数据库中的哪张表逆向生成哪一个javabean -->
    <table tableName="emp" domainObjectName="Emp"></table>
    <table tableName="dept" domainObjectName="Dept"></table>

  </context>
</generatorConfiguration>

4.运行代码生成器生成代码

@Test
public void testMBG() throws Exception {
   List<String> warnings = new ArrayList<String>();
   boolean overwrite = true;
   File configFile = new File("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);
}

5.逆向工程的使用
基本查询与带条件查询测试

@Test
    public void testCRUD() throws Exception{
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        
//        Emp emp = mapper.selectByPrimaryKey(2);
//        System.out.println(emp);
        
        EmpExample example = new EmpExample();
        
        Criteria c1 = example.createCriteria();
        c1.andEnameLike("%a%");
        c1.andSexEqualTo("1");
        
        Criteria c2 = example.createCriteria();
        c2.andDidEqualTo(2);
        
        example.or(c2);
        
        List<Emp> list = mapper.selectByExample(example);
        for (Emp emp : list) {
            System.out.println(emp);
        }
        
        //查询全部
//        List<Emp> list1 = mapper.selectByExample(null);
//        for (Emp emp : list1) {
//            System.out.println(emp);
//        }
    }