概述
写javabean对象正常的操作是:根据table写数据库表—>javaBean对象—>BookDao—>dao.xml—>。。。
逆向工程:
根据数据表table,逆向分析数据表,自动生成javaBean—BookDao—dao.xml—xxx
MBG:MyBatis Generator:代码生成器;
MyBatis官方提供的代码生成器;帮我们逆向生成;
实现
1、导包
<!--mybatis的逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
2、写配置
mbg.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>
<!--
MyBatis3Simple:基础班CRUD
MyBatis3:复杂版CRUD
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- jdbcConnection:指导连接到哪个数据库 -->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator:生成pojo
targetPackage:生成的pojo放在哪个包
targetProject:放在哪个工程下
-->
<javaModelGenerator targetPackage="com.lly.pojo"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方 -->
<sqlMapGenerator targetPackage="mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator:mapper接口生成的地方 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.lly.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table:指定要逆向生成哪个数据表
tableName="t_cat":表名
domainObjectName="":这个表对应的对象名
-->
<table tableName="cat" domainObjectName="Cat"></table>
<table tableName="teacher" domainObjectName="Teacher"></table>
</context>
</generatorConfiguration>
3、生成
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/resources/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);
System.out.println("生成ok了!");
}
4、使用插件生成
pom.xml
<!--使用插件启动mbg-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</dependency>
<!--引入mbg插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!--以下依赖不导入的话可能会报以下异常-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/mbg.xml</configurationFile>
</configuration>
</plugin>
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:
generate (default-cli) on project SSM: Execution default-cli of goal
org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate failed:
Exception getting JDBC Driver: com.mysql.cj.jdbc.Driver -> [Help 1]
补充:
用逆向工程可能会造成自己写的文件被清掉哟
分页插件
1、导包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
2、写配置
mybatis-config.xml
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
3、测试
PageHelper.startPage(pn,5);
List<Teacher> list = teacherService.getAll();
//将查询的结果放在PageInfo()中,这个PageInfo就有非常多能用的东西
//PageInfo(参数1,参数2);参数2传入要连续要显示的页码
PageInfo pageInfo = new PageInfo(list,5);
System.out.println("当前页码: "+pageInfo.getPageNum());
System.out.println("总页码: "+pageInfo.getPages());
System.out.println("总记录数: "+pageInfo.getTotal());
System.out.println("当前页有几条记录: "+pageInfo.getSize());
System.out.println("当前页的pageSize: "+pageInfo.getPageSize());
System.out.println("前一页: "+pageInfo.getPrePage());
System.out.println("结果: "+pageInfo.getList());//查询结果