Why should there be mybatis
MyBatis is a Java ORM framework, and ORM appears to simplify development. The initial development approach is to separate the business logic from the database query logic, either to write SQL statements in the program, or to invoke SQL stored procedures. This leads to the need to switch between language logic and SQL logic, resulting in inefficient development. So there is a series of ORM Framework, ORM Framework to the database table and Java objects, when manipulating the database, only need to manipulate the object's Java object, such as setting a few and conditions, only need to set a few properties.
Why should there be mybatis generator
Although there is a mybatis framework, but learning MyBatis also need to learn costs, especially the configuration of the XML file it needs, it is quite cumbersome, and configuration errors, not easy to locate. When there is an inexplicable error or a mass of objects to be generated, there is often a feeling of love wandering in the brain. Therefore, MyBatis generator was born.
It only requires a simple configuration to complete a large number of tables to MyBatis Java object generation work, not only fast, but not error, allows developers to really focus on the development of business logic.
Official MyBatis generator function is relatively simple, for a slightly more complex but the development of the necessary paging function, BULK Insert function, etc., but has a mature plug-in function support.
I've put our usual mybatis build tools into GitHub, which has integrated paging, bulk INSERT, and serialization capabilities. Can be viewed here, the use method has been described.
MyBatis Generator-generated file structure
The resulting file contains three categories:
1.Model entity file, a database table to generate a Model entity;
2.ModelExample files, this file and the entity file in the same directory, mainly for query condition construction;
3.Mapper interface file, data number operation methods are defined in this interface;
4.Mapper XML configuration file;
The corresponding directory structure and files can be generated by configuring the file generation path in the configuration file and setting the corresponding package name. I set the build directory to the test directory, the entity package name is set to Com.fengzheng.dao.entity, the interface package name is set to Com.fengzheng.dao.mapper, and the resulting file directory structure is shown in the following illustration:
How do I write code?
All of the method calls come from the generated interface file, which in Spring MVC needs to be declared by the caller, using a blacklist interface as an example, the generated interface file is Blacklistipmapper, so the caller wants to declare this interface as follows:
@Autowired
private Blacklistipmapper blacklistipmapper;
Database query
Query is the most common function, the following method is to query IP for a certain value of the record, if you know the primary key, you can use the Selectbyprimarykey method.
Public Blacklistip get (String IP) {
blacklistipexample example = new Blacklistipexample ();
Example.createcriteria (). Andipequalto (IP);
list<blacklistip> blacklistiplist = blacklistipmapper.selectbyexample (example);
if (Blacklistiplist!=null && blacklistiplist.size () >0) {return
blacklistiplist.get (0);
}
return null;
}
The update, add, Delete method invocation method is similar, you can see the related document introduction.
Sort
Public Blacklistip get (String IP) {
blacklistipexample example = new Blacklistipexample ();
Example.setorderbyclause ("Create_time desc"); Sorted by creation time
Example.createcriteria (). Andipequalto (IP);
list<blacklistip> blacklistiplist = blacklistipmapper.selectbyexample (example);
if (Blacklistiplist!=null && blacklistiplist.size () >0) {return
blacklistiplist.get (0);
}
return null;
}
Paging
Public PageInfo list (account account, PageInfo pageinfo,string starttime,string endtime) {
Account.setisdel ( Sysparamdetailconstant.is_del_false);
accountexample example = Getcondition (account,starttime,endtime);
if (null!= pageInfo && null!= pageinfo.getpagestart ()) {
Example.setlimitclausestart ( Pageinfo.getpagestart ());
Example.setlimitclausecount (Pageinfo.getpagecount ());
}
Example.setorderbyclause ("Create_time desc");
list<account> list = Accountmapper.selectbyexample (example);
int totalcount = Accountmapper.countbyexample (example);
Pageinfo.setlist (list);
Pageinfo.settotalcount (totalcount);
return pageInfo;
}
Implement query conditions such as a=x and (b=xx or b=xxx)
Although it is convenient to generate code automatically, but the advantages of everything is a disadvantage, mybatis generator no way to generate table join function, can only be added manually. A conditional concatenation such as a=x and (b=xx or B=XXX) is implemented as follows.
Accountexample accountexample = new Accountexample ();
Accountexample.criteria Criteria = Accountexample.createcriteria (). Andtypeequalto ("4");
Criteria.addcriterion (String.Format ("(id=%d or id=%d)", 34,35));
list<account> accounts = accountmapper.selectbyexample (accountexample);
return accounts;
But you need to modify a little bit of code, modify the Examplegenerator line of code under the Org.mybatis.generator.codegen.mybatis3.model package, method.setvisibility ( javavisibility.protected); Changed to Method.setvisibility (Javavisibility.public);
The changes have been synchronized to the GitHub.
The above is a small set to introduce the MyBatis generator generate code and the use of the way, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!