# # introduction
-
1. Build the MyBatis Generator plug-in environment
-
A. Add plug-in dependencies on POM.xml
-
B. Configure file generatorconfig.xml
-
C. Database configuration file jdbc.properties
-
D. Configure the plug-in boot option
-
2. Project practice
-
A. For example, in a project, we need to delete the information of a user under a certain group
-
B. Update group information based on group ID (non-primary key)
-
C. Various queries
IDEA reverse MyBatis project, unlike Hibernate support has its own plug-in, need to integrate a third party MyBatis Generator.
This section illustrates the process of using MyBatis Generator and illustrates the use of reverse engineering.
1. Build the MyBatis Generator plug-in environment
A. Add plug-in dependencies on POM.xml
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;"> ` <! GroupId >org.mybatis. Generator </groupId> < artifactId > mybatis generator - maven plugin - < / artifactId > < version > 1.3.2 < / version > < configuration > <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>` </pre>
Copy the code
B. Configure file generatorconfig.xml
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;"> ` <? 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>
<properties resource="jdbc.properties"/>
<classPathEntry location="${jdbc_driverLocation}"/ > <! -- Specify the location of the JDBC driver JAR package for a specific database --> <context id="default" targetRuntime="MyBatis3"> <! -- optional, designed to control comments when creating classes --> <commentGenerator> <property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/> </commentGenerator> <! -- JDBC database connection --> <jdbcConnection driverClass="${jdbc_driverClass}"
connectionURL="${jdbc_url}"
userId="${jdbc_user}"
password="${jdbc_pwd}"> </jdbcConnection> <! -- Optional, type handler, conversion control between database type and Java type --> <javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver> <! -- Model Model generator, used to generate classes with primary keys, The Example class targetPackage specifies the package name of the generated model. TargetProject specifies the path under this item --> <javaModelGenerator targetPackage="com.rambo.sdm.dao.pojo" targetProject="src/main/java"> <! - whether to allow package, namely the targetPackage. SchemaName. TableName - > < property name ="enableSubPackages" value="false"/ > <! Add a constructor to model --> <property name="constructorBased" value="true"/ > <! -- Whether to trim columns of type CHAR --> <property name="trimStrings" value="true"/ > <! The generated Model object will not have setter methods, only constructor methods --> <property name="immutable" value="false"/> </javaModelGenerator> <! SqlMapGenerator targetPackage= --> <sqlMapGenerator targetPackage="com.rambo.sdm.dao.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/> </sqlMapGenerator> <! Client-side code that generates easy-to-use code for Model objects and XML configuration filestype="ANNOTATEDMAPPER"Generate Java Models and annotation-based Mapper objectstype="MIXEDMAPPER"To generate annotation-based Java Models and corresponding Mapper objectstype="XMLMAPPER", generate SQLMap XML file and separate Mapper interface --> <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>` </pre>
Copy the code
C. Database configuration file jdbc.properties
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;">`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` </pre>Copy the code
D. Configure the plug-in boot option
2. Project practice
The User class is a generic entity class that defines database fields and set/ GET methods
Mybatis introduces the Example class to encapsulate database query criteria.
A. For example, in a project, we need to delete the information of a user under a certain group
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;">`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);
}` </pre>
Copy the code
B. Update group information based on group ID (non-primary key)
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;">`public int updateStudyTeamInfo(StudyTeamInfo st){
StudyTeamInfoExample ste = new StudyTeamInfoExample();
ste.createCriteria().andTeamIdEqualTo(st.getTeamId());
return studyTeamInfoDAO.updateByExampleSelective(st,ste);
}` </pre>
Copy the code
C. Various queries
(1) Fuzzy query and sort
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;">`public List<StudyTeamInfo> getStudyTeamInfoByName(String team_name){
StudyTeamInfoExample se = new StudyTeamInfoExample();
se.createCriteria().andTeamNameLike("%"+team_name+"%").andEnableEqualTo((short)1);
se.setOrderByClause("team_score desc");
List<StudyTeamInfo> ls = studyTeamInfoDAO.selectByExample(se);
if(ls! =null&&ls.size()>0){return ls;
}
return null;
}` </pre>
Copy the code
(2) queries greater than or equal to a certain score and less than a certain score
<pre data-tool="Mdnice Editor" style="margin: 10px 0px; padding: 0px; max-width: 100%; box-sizing: border-box ! important; word-wrap: break-word ! important; border-radius: 5px; Box-shadow: rgba(0, 0, 0, 0.54902) 0px 2px 10px;">`public StudyTeamLevel getStudyTeamLevel(long score){
StudyTeamLevelExample le = new StudyTeamLevelExample();
le.createCriteria().andNeedScoreLessThanOrEqualTo(score).andUpScoreGreaterThan(score);
List<StudyTeamLevel> ls = studyTeamLevelDAO.selectByExample(le);
if(ls! =null&&ls.size()>0){returnls.get(0); ` </pre>Copy the code
END……