I. Use
1. in maven's pom.xml < Import plug-ins in build></build>l
<!--Reverse Generation Plugin--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--Path to configuration file--> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>
2. Create generatorConfig.xml in resourses
<?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"> <!-- Configuration Generator:All numbered content needs to be modified to its own content or path --> <generatorConfiguration> <!--1,Database Driver jar:Add your own jar Route --> <classPathEntry location="D:\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar" /> <context id="MyBatis" targetRuntime="MyBatis3"> <!--Remove Comments --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--2,Database Connection --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT" userId="root" password="123456"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolved to Integer; by true Time handles JDBC DECIMAL and NUMERIC Type resolved to java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--3,Generate Entity Class specifies the package name and generated address (you can customize the address, but the path does not exist and will not be automatically created) Use Maven Generated in target Directory, created automatically) --> <javaModelGenerator targetPackage="com.kkb.pojo" targetProject="src\main\java"> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--4,generate SQLmapper.xml file --> <sqlMapGenerator targetPackage="com.kkb.mapper" targetProject="src\main\resources"> </sqlMapGenerator> <!--5,generate Dao(Mapper)file,Generate Interface --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.kkb.mapper" targetProject="src\main\java"> </javaClientGenerator> <!--6,Which tables to generate(change tableName) --> <!-- tableName:The table name to be generated corresponds to the database representation enableCountByExample:Count Add to statement where Conditional query, default is true open enableUpdateByExample:Update Add to statement where Conditional query, default is true open enableDeleteByExample:Delete Add to statement where Conditional query, default is true open enableSelectByExample:Select Add to multiple statements where Conditional query, default is true open selectByExampleQueryId:Select Add to a single object statement where Conditional query, default is true open --> <table tableName="Team"> <property name="useActualColumnNames" value="true"/> </table> <table tableName="Player"> <property name="useActualColumnNames" value="true"/> </table> <table tableName="game"> <property name="useActualColumnNames" value="true"/> </table> <table tableName="GameType"> <property name="useActualColumnNames" value="true"/> </table> <table tableName="Admins"> <property name="useActualColumnNames" value="true"/> </table> <table tableName="AdminRole"> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
3. Start mybatis-generator:generate
Entity classes will appear after generation Entity Class Name+Example Class This class is used when handling multiconditional queries and sorting
Note: The xml Mapping file corresponding to the mapper should be placed in the same path under resources, which must be created hierarchically (there is no need to create it manually when auto-generating).
2. Instructions for the use of automatically generated interface classes
Examples of teamMapper interfaces for Team entity classes
long countByExample(TeamExample example); //Conditional Statistics
int deleteByExample(TeamExample example); //Conditional deletion
Team selectByPrimaryKey(Integer teamId); //Query by primary key int deleteByPrimaryKey(Integer teamId); //Delete by Primary Key
int updateByPrimaryKey(Team record); //Update all fields you inject with updateByPrimaryKey based on the primary key update
int updateByPrimaryKeySelective(Team record); //Update based on the primary key, pdateByPrimaryKeySelective updates the field judgment (ignores updates if it is Null), and you can use this method if you only want to update a field.
int insert(Team record); //Insert sql statement will appear that all properties are not set property assignment null property is one-to-one correspondence with database table column name
int insertSelective(Team record); //Selective insertion of sql statement will only show the property set other default null not set
int updateByExample(@Param("record") Team record, @Param("example") TeamExample example); //updateByExample is passed in an object, updating the entire data, and automatically set to null if there is no value attribute in the object.
int updateByExampleSelective(@Param("record") Team record, @Param("example") TeamExample example); //Update several attributes in a row without changing other values //Objects can have only one parameter inside and all others are null, but when updated, only the column with attributes will be updated, what the other columns were before, what they are now, and will not be modified
Methods with Example all create containers to hold multiple conditions
//Multiple Conditions TeamExample example=new TeamExample(); //Create containers for conditions TeamExample.Criteria criteria = example.createCriteria();
Methods that criateria objects can call:
//Add condition name is empty public Criteria andTeamNameIsNull() { addCriterion("teamName is null"); return (Criteria) this; } //Add condition name cannot be empty public Criteria andTeamNameIsNotNull() { addCriterion("teamName is not null"); return (Criteria) this; } //Add condition equals public Criteria andTeamNameEqualTo(String value) { addCriterion("teamName =", value, "teamName"); return (Criteria) this; } //Add condition is not equal to public Criteria andTeamNameNotEqualTo(String value) { addCriterion("teamName <>", value, "teamName"); return (Criteria) this; } //Add condition greater than public Criteria andTeamNameGreaterThan(String value) { addCriterion("teamName >", value, "teamName"); return (Criteria) this; } //Add condition greater than or equal to public Criteria andTeamNameGreaterThanOrEqualTo(String value) { addCriterion("teamName >=", value, "teamName"); return (Criteria) this; } //Add condition less than public Criteria andTeamNameLessThan(String value) { addCriterion("teamName <", value, "teamName"); return (Criteria) this; } //Add condition less than or equal to public Criteria andTeamNameLessThanOrEqualTo(String value) { addCriterion("teamName <=", value, "teamName"); return (Criteria) this; } //Add conditional fuzzy query like public Criteria andTeamNameLike(String value) { addCriterion("teamName like", value, "teamName"); return (Criteria) this; } //Add conditional fuzzy query notlike public Criteria andTeamNameNotLike(String value) { addCriterion("teamName not like", value, "teamName"); return (Criteria) this; } //Add condition in List<?> condition public Criteria andTeamNameIn(List<String> values) { addCriterion("teamName in", values, "teamName"); return (Criteria) this; } //Add condition not in List<?> condition public Criteria andTeamNameNotIn(List<String> values) { addCriterion("teamName not in", values, "teamName"); return (Criteria) this; } //Add a conditional range within the constraint interval public Criteria andTeamNameBetween(String value1, String value2) { addCriterion("teamName between", value1, value2, "teamName"); return (Criteria) this; } //Add Conditional Range No longer Constrains Interval public Criteria andTeamNameNotBetween(String value1, String value2) { addCriterion("teamName not between", value1, value2, "teamName"); return (Criteria) this; }
Whether you are describing a type's properties is the default of the above method, then you need to call meaningful methods based on the specific type.
Call method example:
Fuzzy Query
criteria.andTeamNameLike("%"+"name".trim()+"%");
Finally, use dynamic proxy objects at the srvice layer
@Resource private TeamMapper teamMapper;
Methods in the service layer
//Multiple Conditions TeamExample example=new TeamExample(); //Create containers for conditions TeamExample.Criteria criteria = example.createCriteria(); List<Team> list = teamMapper.selectByExample(example);