Why do I need mybatis
mybatis is a Java ORM framework, and ORM emerged to simplify development. The initial development method was that business logic and database query logic were separated, either writing SQL statements in a program, or calling SQL stored procedures. This leads to the need to switch between language logic and sql logic, resulting in inefficient development. Therefore, a series of ORM frameworks have emerged. The ORM framework corresponds to database tables and Java objects. When operating the database, you only need to operate the Java objects of the object, such as setting several and conditions, and only setting a few attributes.
Why do I need mybatis generator
Although there is the mybatis framework, learning mybatis also requires learning costs, especially configuring the XML files it requires, which is quite cumbersome, and errors occur in the configuration, which is not easy to locate. When there are inexplicable mistakes or large quantities of objects that need to be generated, there is often a feeling of being reluctant to live and wandering in the mind. Therefore, mybatis generator came into being.
It only requires simple configuration to complete the generation of large amounts of tables to mybatis Java objects. It is not only fast, but also has no errors, allowing developers to truly focus on the development of business logic.
The official mybatis generator function is relatively simple, and it has not been implemented for slightly complex pagination functions, batch insert functions, etc. that are inevitably used in development, but it has mature plug-in functions supported.
I have put the mybatis generation tool we usually use to github, which has integrated pagination, batch insertion, and serialization functions. You can check it here and have already introduced how to use it.
mybatis generator file structure
The generated file contains three categories:
1. Model entity file, a database table generates a Model entity;
2.ModelExample file, this file and entity file are in the same directory, and are mainly used for querying conditional construction;
3.Mapper interface file and data operation methods are defined in this interface;
4.Mapper XML configuration file;
Configure the file generation path in the configuration file and set the corresponding package name to generate the corresponding directory structure and files. I set the generated directory to the test directory, the entity package name is com.fengzheng.dao.entity, the interface package name is com.fengzheng.dao.mapper, and then the generated file directory structure is shown in the figure below:
How to write code
All method calls come from the generated interface file. In Spring MVC, it is necessary to declare it on the caller. Using a blacklist interface as an example, the generated interface file is BlackListIPMapper, so the caller needs to declare this interface, as follows:
@Autowiredprivate BlackListIPMapper blackListipMapper;
Database query
Query is the most commonly used function. The following method is to query records whose IP 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 = blackListMapper.selectByExample(example);if(blackListIPList!=null && blackListIPList.size()>0){return blackListIPList.get(0);}return null;} The method of calling the method is similar. For details, please see the introduction of the relevant documents.
Sort
public BlackListIP get(String ip){BlackListIPExample example = new BlackListIPExample();example.setOrderByClause("CREATE_TIME desc"); //Sort by creation time example.createCriteria().andIpEqualTo(ip);List<BlackListIP> blackListIPList = blackListMapper.selectByExample(example);if(blackListIPList!=null && blackListIPList.size()>0){return blackListIPList.get(0);}return null;}Pagination
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, everything has its advantages and disadvantages. Mybatis generator has no way to generate table join function, so it can only be added manually. Conditional splicing 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; However, you need to modify a little code, modify the 524th line of ExampleGenerator under the org.mybatis.generator.codegen.mybatis3.model package, change method.setVisability(JavaVisability.PROTECTED); to method.setVisability(JavaVisability.PUBLIC);
Changes have been synchronized to github.
The above is the code and usage of MyBatis Generator introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!