First of all, the following code is based on the Sun Chenbin of the Big Brother's ' MyBatis automatic creation table/Update table structure/Dynamic table ' This article evolved, here thank the blogger, inspired by the blogger, so that my technology has improved a lot, thank you very much.
Let's get down to business:
One, talk about what can do to use model to automatically create a table can automatically initialize part of the data to the database using model for basic additions and deletions to check
Second, to say a different place first (a lot of ah, I changed the unrecognizable (/ω╲)) directly kill the non-Mysql database part of the code Table.java and Column.java annotations have added comment attributes, that is, table and field comments Column.java increased The unsigned property, which is the Number field unsigned property, can increase the capacity of the Number field Initdata.java method annotation, which is used to add the method to initialize data after the data table initialization is completed. Configloder class for loading configuration files Model2table.properties. The original is loaded through the spring annotation. Mysqltypeconstant.java increases the type of tinyint, which is the Boolean type of MySQL. Classtools.java also made a partial modification to add a method for scanning classes with @InitData annotations. The scanned model path adds wildcard support, but only * * two asterisks. Then there are more small details than the detailed, basic each class has moved Ψ ( ̄∀ ̄) Ψ.
Iii. Methods of Use
1. Establishment of model2table.properties under the Resources folder
# Mybatis.table.auto=create | Update | None
mybatis.table.auto=update
# Mybatis.table.initdata=none | Insert
Mybatis.table.initdata=insert
# supports wildcard characters, but only supports * * Two asterisk
Mybatis.model.pack=com.wb.**.model
# because the type and generated SQL is MySQL version, temporarily only support MySQL
Mybatis.database.type=mysql
2. In the configuration file, add the following
<bean id= "Basedaosupport" class= "Com.wb.plugins.model2table.command.BaseDaoSupportImpl" >
<property Name= "Sqlsessionfactory" ref= "sqlsessionfactory"/>
</bean>
3. In accordance with the original practice is to find a way to scan the mapper file, I use the wildcard character.
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= " DataSource "ref=" datasourcelog4j "/>
<property name=" mapperlocations ">
<array>
< value>classpath*:* */*-mapper.xml</value>
</array>
</property>
<!-- Load MyBatis Global configuration file-->
<property name= "configlocation" value= "Classpath:mybatis-config.xml" ></ Property>
</bean>
4. The next step is to write a model test run
Package Com.wb.sys.model;
Import java.io.Serializable;
Import Com.wb.plugins.model2table.annotation.Column;
Import com.wb.plugins.model2table.annotation.Table;
Import com.wb.plugins.model2table.constants.MySqlTypeConstant; /** * @author Ai Xiaotian * @email: wongtp@outlook.com * @date: December 25, 2017 afternoon 7:33:29/@Table (name = "Sys_dict2", comment =
"Dictionary table") public class Sysdict implements Serializable {private static final long serialversionuid = 1L; @Column (name = "id", isunsigned = true, type = mysqltypeconstant.bigint, length = $, IsKey = true, IsNull = False, Commen
t = "dictionary table ID") Private Long ID; @Column (name = "ParentID", isunsigned = true, type = mysqltypeconstant.bigint, length = +, IsNull = true, comment = "Parent ID
") Private Long parentid; @Column (name = "Path", type = mysqltypeconstant.varchar, length = m, IsUnique = true, comment = "tree path") Private Strin
G Path; @Column (name = "Dictname", type = mysqltypeconstant.varchar, length = m, comment = "Dictionary name ") private String dictname;
@Column (name = "Code", type = mysqltypeconstant.varchar, length =, comment = "dictionary encoding") private String code;
@Column (name = "OrderNo", type = mysqltypeconstant.int, length = 5, comment = "sort number") Private Integer orderNo; @Column (name = "HasChildren", type = mysqltypeconstant.tinyint, length = 1, comment = "have child nodes, 0 No, 1 is") Private Boolean ha
Schildren;
Omitting the getter and setter methods ....
}
5. Write the initialization data class, the back should be able to write their own, go, Pikachu
Package Com.wb.sys.model;
Import Com.wb.plugins.model2table.annotation.InitData;
/**
* @author Ai Xiaotian
* @email: wongtp@outlook.com
* @date: December 26, 2017 afternoon 6:34:41
* * Public
class Testone {
@InitData public
void InitData () {
System.out.println ("\n\n\n===================testone");
}
}
Attached github: Source Address model2table
Finally say two: The reason for the change is to tie in with the current development, the original and the current system is not very compatible with the changes, but also in order to meet the needs of the business, facilitate the deployment and initialization of the project