正向工程:先创建java实体类,有框架负责根据实体类生成数据库表。Hibernate是支持正向工程
逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:
⑴Java实体类
⑵Mapper接口
⑶Mapper映射文件
注意:逆向工程生成的都是单表资源
13.1、创建逆向工程的步骤
⑴添加依赖
<!-- 控制 Maven 在构建过程中相关配置 --> < build > <!-- 构建过程中用到的插件 --> < plugins > <!-- 具体插件,逆向工程的操作是以构建过程中插件形式出现的 --> < plugin > < groupId >org.mybatis.generator</ groupId > < artifactId >mybatis-generator-maven-plugin</ artifactId > < version >1.3.0</ version > <!-- 插件的依赖 --> < dependencies > <!-- 逆向工程的核心依赖 --> < dependency > < groupId >org.mybatis.generator</ groupId > < artifactId >mybatis-generator-core</ artifactId > < version >1.3.2</ version > </ dependency > <!-- MySQL 驱动 --> < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.6</ version > </ dependency > </ dependencies > </ plugin > </ plugins > </ build >
⑵配置MyBatis的核心配置文件
配置pojo包和mapper包
⑶创建逆向工程的配置文件,该文件文件名必须是:generatorConfig.xml
<? 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 > <!-- targetRuntime: 执行生成的逆向工程的版本 MyBatis3Simple: 生成基本的 CRUD (清新简洁版) MyBatis3: 生成带条件的 CRUD (奢华尊享版) --> < context id ="DB2Tables" targetRuntime ="MyBatis3" > <!-- 数据库的连接信息 --> < jdbcConnection driverClass ="com.mysql.jdbc.Driver" connectionURL ="jdbc:mysql://localhost:3306/mybatis_demo" userId ="root" password ="root" > </ jdbcConnection > <!-- javaBean 的生成策略 --> < javaModelGenerator targetPackage ="com.qcby.mybatis.pojo" targetProject =".\src\main\java" > < property name ="enableSubPackages" value ="true" /> < property name ="trimStrings" value ="true" /> </ javaModelGenerator > <!-- SQL 映射文件的生成策略 --> < sqlMapGenerator targetPackage ="com.qcby.mybatis.mapper" targetProject =".\src\main\resources" > < property name ="enableSubPackages" value ="true" /> </ sqlMapGenerator > <!-- Mapper 接口的生成策略 --> < javaClientGenerator type ="XMLMAPPER" targetPackage ="com.qcby.mybatis.mapper" targetProject =".\src\main\java" > < property name ="enableSubPackages" value ="true" /> </ javaClientGenerator > <!-- 逆向分析的表 --> <!-- tableName 设置为 * 号,可以对应所有表,此时不写 domainObjectName --> <!-- domainObjectName 属性指定生成出来的实体类的类名 --> < table tableName ="emp" domainObjectName ="Emp" /> < table tableName ="dept" domainObjectName ="Dept" /> </ context > </ generatorConfiguration >
13.2测试
/** * 根据主键查询 */ @Test public void testSelectByPrimaryKey (){ Emp emp = empMapper .selectByPrimaryKey( 1 ); System . out .println( emp ); } /** * 带条件查询 */ @Test public void testSelectByExample (){ List < Emp > emps = empMapper .selectByExample( null ); emps .forEach( System . out ::println); } @Test public void testSelectByExample2 (){ EmpExample example = new EmpExample(); example .createCriteria().andEmpNameEqualTo( " 李四 " ).andAgeGreaterThanOrEqualTo( 22 ); List < Emp > emps = empMapper .selectByExample( example ); emps .forEach( System . out ::println); } @Test public void testSelectByExample3 (){ EmpExample example = new EmpExample(); example .createCriteria().andEmpNameEqualTo( " 王五 " ).andAgeGreaterThanOrEqualTo( 22 ); example .or().andSexEqualTo( " 男 " ); List < Emp > emps = empMapper .selectByExample( example ); emps .forEach( System . out ::println); } @Test public void testUpdateByPrimaryKey (){ Emp emp = new Emp( 2 , " 小张 " , null , " 女 " ); // int i = empMapper.updateByPrimaryKey(emp); // if(i>0){ // System.out.println(" 更新成功 "); // }else { // System.out.println(" 更新失败 "); // } int i = empMapper .updateByPrimaryKeySelective( emp ); if ( i > 0 ){ System . out .println( " 更新成功 " ); } else { System . out .println( " 更新失败 " ); } }