1、逆向工程概念 正向工程:先创建 Java 实体类,由框架负责根据实体类生成数据库表。Hibernate 是支持正向工程的。 逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:Java 实体类 Mapper 接口 Mapper 配置文件 2、基本原理
3、搭建环境 ① 加入 jar 包 hamcrest-core-1.3.jar junit-4.12.jar log4j.jar mybatis-3.4.1.jarmybatis-generator-core-1.3.2.jar mysql-connector-java-5.1.7-bin.jar
② 加入配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 <?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/mybatis" userId ="root" password ="root" > </jdbcConnection > <javaModelGenerator targetPackage ="com.atguigu.mybatis.entity" targetProject =".\pro08-mybatis-MBG\src" > <property name ="enableSubPackages" value ="true" /> <property name ="trimStrings" value ="true" /> </javaModelGenerator > <sqlMapGenerator targetPackage ="com.atguigu.mybatis.mapper" targetProject =".\pro08-mybatis-MBG\src" > <property name ="enableSubPackages" value ="true" /> </sqlMapGenerator > <javaClientGenerator type ="XMLMAPPER" targetPackage ="com.atguigu.mybatis.mapper" targetProject =".\pro08-mybatis-MBG\src" > <property name ="enableSubPackages" value ="true" /> </javaClientGenerator > <table tableName ="t_emp" domainObjectName ="Employee" /> <table tableName ="t_customer" domainObjectName ="Customer" /> <table tableName ="t_order" domainObjectName ="Order" /> </context > </generatorConfiguration >
③java 代码 1 2 3 4 5 6 7 8 9 List<String> warnings = new ArrayList<String>(); 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 );
④ 说明 1 targetProject=".\module名称\src"
4、QBC 查询 ① 概念 QBC:Query By Criteria
QBC 查询最大的特点就是将 SQL 语句中的 WHERE 子句进行了组件化的封装,让我们可以通过调用 Criteria 对象的方法自由的拼装查询条件。
② 使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 EmployeeExample example = new EmployeeExample(); EmployeeExample.Criteria criteria01 = example.createCriteria(); EmployeeExample.Criteria criteria02 = example.or(); criteria01 .andEmpAgeBetween(9 , 99 ) .andEmpNameLike("%o%" ) .andEmpGenderEqualTo("male" ) .andEmpSalaryGreaterThan(500.55 ); criteria02 .andEmpAgeBetween(9 , 99 ) .andEmpNameLike("%o%" ) .andEmpGenderEqualTo("male" ) .andEmpSalaryGreaterThan(500.55 ); SqlSession session = factory.openSession(); EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); List<Employee> employeeList = mapper.selectByExample(example); for (Employee employee : employeeList) { System.out.println("employee = " + employee); } session.close();
5、注意 将来会使用 Maven 命令执行生成过程 在实际开发中,建议把 MBG 本身和项目主体代码分开:不要放在同一个工程或模块内