Mybatis Generator generates code and usage, mybatisgenerator
This article original, reproduced Please note: http://www.cnblogs.com/fengzheng/p/5889312.html
Why is there mybatis?
Mybatis is a Java ORM framework. The emergence of ORM is to simplify development. The initial development method was to separate the business logic from the database query logic, write SQL statements in the program, or call the SQL stored procedure. In this way, the thinking needs to switch between the language logic and the SQL logic, resulting in low development efficiency. Therefore, a series of ORM frameworks have emerged. The ORM framework maps database tables with Java objects. When operating a database, you only need to operate on the Java objects of the objects. For example, you can set several and conditions, you only need to set several attributes.
Why does mybatis generator exist?
Although the mybatis framework is available, learning mybatis also requires learning costs, especially the XML files required for configuration. In addition, configuration errors are not easy to locate. When an inexplicable error occurs or a large number of objects need to be generated, there is often a feeling of being heartless. Therefore, mybatis generator came into being.
It only requires simple configuration to generate a large number of tables into mybatis Java objects, which is fast and error-free, allowing developers to focus on business logic development.
The mybatis generator function officially provided is relatively simple. It is not implemented for paging and batch insert functions that are slightly complicated but must be used in development. However, mature plug-in functions are supported.
I have put the mybatis generation tool that we usually use on github. It has integrated paging, batch insertion, and serialization functions. You canHereYou have already introduced how to use it.
File structure generated by mybatis generator
Three types of files are generated:
Configure the file generation path in the configuration file and set the corresponding package name to generate the corresponding directory structure and file. I set the generated directory to the test directory and the object package name to com. fengzheng. dao. entity. The interface package name is set to com. fengzheng. dao. mapper, and then the generated file directory structure is shown in:
How to write code?
All method calls come from the generated interface file. In Spring MVC, You need to declare it on the caller. For example, use a blacklist interface. The generated interface file is BlackListIPMapper, therefore, the caller must declare this interface as follows:
@Autowiredprivate BlackListIPMapper blackListipMapper;
Database Query
Query is the most common function. The following method is to query records whose IP address is a certain value. 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 method for updating, adding, and deleting methods is similar. For details, refer to the relevant documentation.
Sort
Public BlackListIP get (String ip) {BlackListIPExample example = new BlackListIPExample (); example. setOrderByClause ("CREATE_TIME desc"); // sort example by creation time. 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 automatic generation of code is very convenient, there are both advantages and disadvantages. mybatis generator cannot generate the join function and can only be manually added. The following shows how to concatenate conditions such as a = x and (B = xx or B = xxx.
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;
However, you need to modify the org. mybatis. generator. codegen. the 524th line code of ExampleGenerator under the mybatis3.model package. setVisibility (JavaVisibility. PROTECTED); changed to method. setVisibility (JavaVisibility. PUBLIC );
The changes have been synchronized to github.
There is no perfect wheel in the world, and you don't need it. That's your thing.