作者:Orson
cnblogs.com/java-class/p/6237564.html
- 1. 搭建 MyBatis Generator 插件環境
- a. 添加插件依賴 pom.xml
- b. 配置文件 generatorConfig.xml
- c. 數據庫配置文件 jdbc.properties
- d. 配置插件啟動項
- 2.項目實戰
- a. 比如在一個項目 我們要刪除某個小組下某個用戶的信息
- b. 根據小組ID(非主鍵 更新小組信息)
- c. 各種查詢
IDEA 逆向 MyBatis 工程時,不像支持 Hibernate 那樣有自帶插件,需要集成第三方的 MyBatis Generator。
MyBatis Generator的詳細介紹 http://mybatis.github.io/generator/index.html
本篇博客圖解 MyBatis Generator 的使用過程,并結合實戰說明逆向工程的使用方式。
1. 搭建 MyBatis Generator 插件環境
a. 添加插件依賴 pom.xml
??????????
????????????<plugin>
????????????????<groupId>org.mybatis.generatorgroupId>
????????????????<artifactId>mybatis-generator-maven-pluginartifactId>
????????????????<version>1.3.2version>
????????????????<configuration>
????????????????????<configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
????????????????????<verbose>trueverbose>
????????????????????<overwrite>trueoverwrite>
????????????????configuration>
????????????????<executions>
????????????????????<execution>
????????????????????????<id>Generate?MyBatis?Artifactsid>
????????????????????execution>
????????????????executions>
????????????????<dependencies>
????????????????????<dependency>
????????????????????????<groupId>org.mybatis.generatorgroupId>
????????????????????????<artifactId>mybatis-generator-coreartifactId>
????????????????????????<version>1.3.2version>
????????????????????dependency>
????????????????dependencies>
????????????plugin>
b. 配置文件 generatorConfig.xml
<?xml ?version="1.0"?encoding="UTF-8"?>
generatorConfiguration
????????PUBLIC?"-//mybatis.org//DTD?MyBatis?Generator?Configuration?1.0//EN"
????????"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
????<properties?resource="jdbc.properties"/>
????<classPathEntry?location="${jdbc_driverLocation}"/>?
????<context?id="default"?targetRuntime="MyBatis3">
????????
????????<commentGenerator>
????????????<property?name="suppressDate"?value="true"/>
????????????<property?name="suppressAllComments"?value="true"/>
????????commentGenerator>
????????
????????<jdbcConnectiondriverClass="${jdbc_driverClass}"connectionURL="${jdbc_url}"userId="${jdbc_user}"password="${jdbc_pwd}">
????????jdbcConnection>
????????
????????<javaTypeResolver><property?name="forceBigDecimals"?value="false"/>javaTypeResolver>
????????
????????<javaModelGenerator?targetPackage="com.rambo.sdm.dao.pojo"?targetProject="src/main/java">
????????????
????????????<property?name="enableSubPackages"?value="false"/>
????????????
????????????<property?name="constructorBased"?value="true"/>
????????????
????????????<property?name="trimStrings"?value="true"/>
????????????
????????????<property?name="immutable"?value="false"/>
????????javaModelGenerator>
????????
????????<sqlMapGenerator?targetPackage="com.rambo.sdm.dao.mapper"?targetProject="src/main/java">
????????????<property?name="enableSubPackages"?value="false"/>
????????sqlMapGenerator>
????????
????????<javaClientGenerator?targetPackage="com.rambo.sdm.dao.inter"?targetProject="src/main/java"?type="XMLMAPPER">
????????????<property?name="enableSubPackages"?value="true"/>
????????javaClientGenerator>
????????<table?tableName="user"?domainObjectName="UserPO">
????????????<generatedKey?column="uuid"?sqlStatement="SELECT?REPLACE(UUID(),'-','')?UUID?FROM?DUAL"/>
????????table>
????context>
generatorConfiguration>
c. 數據庫配置文件 jdbc.properties
jdbc_driverLocation=D:\\Program Files\\Repository\\mysql\\mysql-connector-java\\5.1.38\\mysql-connector-java-5.1.38.jar
jdbc_driverClass=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf-8
jdbc_user=root
jdbc_pwd=123456
validationQuery = select 1
d. 配置插件啟動項

2.項目實戰
User類就是普通的實體類,定義了數據庫對應的字段,以及set/get方法
Mybatis 引入了 Example 類,用來封裝數據庫查詢條件。
a. 比如在一個項目 我們要刪除某個小組下某個用戶的信息
????public?int?deleteUserApplyInfo(long?user_id,long?team_id){
????????StudyTeamUserApplyInfoExample?ue?=?new?StudyTeamUserApplyInfoExample();
????????ue.createCriteria().andUserIdEqualTo(new?BigDecimal(user_id)).andTeamIdEqualTo(new?BigDecimal(team_id));
????????return?studyTeamUserApplyInfoDAO.deleteByExample(ue);
????}
b. 根據小組ID(非主鍵 更新小組信息)
???public?int?updateStudyTeamInfo(StudyTeamInfo?st){
????????StudyTeamInfoExample?ste?=?new?StudyTeamInfoExample();
????????ste.createCriteria().andTeamIdEqualTo(st.getTeamId());
????????return?studyTeamInfoDAO.updateByExampleSelective(st,ste);
????}
c. 各種查詢
(1)模糊查詢并且排序
public?List?getStudyTeamInfoByName(String?team_name){
????????StudyTeamInfoExample?se?=?new?StudyTeamInfoExample();
????????se.createCriteria().andTeamNameLike("%"+team_name+"%").andEnableEqualTo((short)1);
????????se.setOrderByClause("team_score?desc");
????????List?ls?=?studyTeamInfoDAO.selectByExample(se);if(ls!=null&&ls.size()>0){return?ls;
????????}return?null;
????}
(2)大于等于某個分數 并且小于某個分數的查詢
public?StudyTeamLevel?getStudyTeamLevel(long?score){
????????StudyTeamLevelExample?le?=?new?StudyTeamLevelExample();
????????le.createCriteria().andNeedScoreLessThanOrEqualTo(score).andUpScoreGreaterThan(score);
????????List?ls?=?studyTeamLevelDAO.selectByExample(le);if(ls!=null&&ls.size()>0){return?ls.get(0);
精彩推薦
一百期Java面試題匯總SpringBoot內容聚合IntelliJ IDEA內容聚合Mybatis內容聚合
歡迎長按下圖關注公眾號后端技術精選