技術分享2018/11/2-Mybatis 逆向工程
阿新 • • 發佈:2018-11-26
文章目錄
MyBatis 逆向工程
一、Mybatis Generator:
1)簡稱 MBG,是一個專門為 MyBatis 框架使用者定製的程式碼生成器,可以快速的根據表生成對應的對映檔案、介面,以及 Bean 類,支援基本的增刪改查,以及 QBC 風格的條件查詢。但是表連線、儲存過程等複雜 sql 的定義需要我們手工編寫
2)官方文件地址
3)官方工程地址
二、MBG 使用步驟-使用 maven 構造
1)引入外掛
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency >
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- mybatis 逆向工程 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2)編寫 MBG 的配置檔案,指定生成策略==【注意檔名稱:必須為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>
<!--
<classPathEntry
location="C:\Users\YoYo\.m2\repository\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar"/>
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!--取消註釋-->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--配置資料庫連線-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql:///ssmcrud?serverTimezone=GMT&allowMultiQueries=true"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--java 模型生成:指定 javabean 生成的位置-->
<javaModelGenerator
targetPackage="info.yingying.crud.bean"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--指定 sql 對映檔案的位置-->
<sqlMapGenerator
targetPackage="mybatis.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--指定 dao 介面的位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="info.yingying.crud.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--指定每個表的生成策略-->
<!--加上 schema 指定特定的資料庫-->
<table schema="ssmcrud" tableName="employee" domainObjectName="Employee"/>
<table schema="ssmcrud" tableName="department" domainObjectName="Department"/>
</context>
</generatorConfiguration>
也可在配置檔案中指定資料庫 jar 包,這是第二種方式
3)執行 maven 外掛
4)測試
① 準備 db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///ssmcrud?serverTimezone=GMT&allowMultiQueries=true
user=root
password=root
② 準備 mybatis 核心配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="info.yingying.crud.dao"/>
</mappers>
</configuration>
③ 準備測試程式碼
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
注:有興趣的同學可以去學習 MyBatis Plus
Mybatis-PageHelper
1)匯入依賴
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.7</version>
</dependency>
2)在 mybatis 核心配置檔案中配置外掛
<plugins>
<!-- com.github.pagehelper 為 PageHelper 類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
3)測試
Page<Object> page = PageHelper.startPage(1, 10);// 緊跟著的查詢會被分頁
List<Employee> employees = employeeMapper.selectByExample(null);
for (Employee employee : employees) {
System.out.println(employee);
}