Mybatis_generator reverse generation tool

role

  • generatepojoEntity class
  • generateXXXmapper.java
  • generateXXXmapper.xml

Reverse build project

The project address

mybatis-generator-for-icoding

Configuration file generatorconfig.xml

<?xml version="1.0" encoding="UTF-8"? >

      

<generatorConfiguration>
    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <! -- mapper directory -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.icoding.my.mapper.MyMapper"/>
        </plugin>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mall-dev"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <! Pojo package -->
        <javaModelGenerator targetPackage="com.icoding.pojo" targetProject="src/main/java"/>

				<! -- mapper generated directory -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

				<! Mapper -->
        <javaClientGenerator targetPackage="com.icoding.mapper" targetProject="src/main/java" type="XMLMAPPER"/>

    	<! SQL > select * from db where SQL >
			<table tableName="carousel"></table>
      <table tableName="category"></table>
      <table tableName="items"></table>
      <table tableName="items_comments"></table>
      <table tableName="items_img"></table>
      <table tableName="items_param"></table>
      <table tableName="items_spec"></table>
      <table tableName="order_items"></table>
      <table tableName="order_status"></table>
      <table tableName="orders"></table>
      <table tableName="user_address"></table>
      <table tableName="users"></table>

    </context>
</generatorConfiguration>

Copy the code

Generate code utility classes in reverse

public class GeneratorDisplay {
	public void generator(a) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		// Specify the reverse engineering configuration file
		File configFile = new File("generatorConfig.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);
	}

	public static void main(String[] args) throws Exception {
		try {
			GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
			generatorSqlmap.generator();
		} catch(Exception e) { e.printStackTrace(); }}}Copy the code

Reference project Configuration

Introduction of generic Mapper tools in POM

<! Mapper reverse tool -->
<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>2.1.5</version>
</dependency>
Copy the code

inapplication.ymlIntroduction of the common Mapper configuration in

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Mybatis mapper configuration
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Generic Mapper configuration
mapper:
  mappers: com.icoding.my.mapper.MyMapper
  not-empty: false  Select * from db where username = 'username'; = null, append username after SQL! = ' '
  identity: MYSQL
Copy the code

The introduction ofMyMapperInterface class (General database operation method CRUD)

/** * inherit MyMapper */
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {}Copy the code

First take a look at MyMapper’s inherited parent class, such as:

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> 
Copy the code

MySqlMapper

MySqlMapper

MySqlMapper

MySqlMapper

public interface MySqlMapper<T> extends
        InsertListMapper<T>,
        InsertUseGeneratedKeysMapper<T>
Copy the code

Mapper (); mapper (); mapper (); mapper (); mapper (); mapper ();

The method name operation note
insertList(list) Batch Database Insertion The primary key must be added automatically
InsertUseGeneratedKeysMapper(record) Insert table data The primary key must be added automatically

In traditional JavaWeb development, these two approaches are fine, but not in distributed situations where you need to design globally unique distributed primary keys.

Mapper

public interface Mapper<T> extends
        BaseMapper<T>,
        ExampleMapper<T>,
        RowBoundsMapper<T>,
        Marker
Copy the code

What are the methods in each parent class?

  • BaseMapper

  • ExampleMapper

    The Example class is used to provide the user with a custom condition to implement, namely a WHERE condition

  • RowBoundsMapper

    Used for paging.