Using the Universal mapper in Spring4
Spring4 adds support for generics injection, which is very useful for general Mapper, and can be said to have this feature, which can be written directly in the service Mapper<UserInfo> mapper and can be used BaseService<T> to implement generic Service .
This document focuses on the best use of general mapper in Spring4 * *.
First, configure the Universal mapper in the Spring4
The difference between the other inside configuration is that the generic mapper class can be configured in Spring4, and we can put the configuration provided in the General mapper Mapper<T> into spring, if you have a generic mapper of your own implementation, you can configure this:
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value= " Com.isea533.mybatis.mapper,com.isea533.mybatis.mapperhelper "/></bean>
Here basePackage , when configured, the package that the generic is Mapper<T> located on is com.isea533.mybatis.mapperhelper also configured. This can be injected directly into the Spring4 Mapper<T> .
The other is the configuration of the general mapper itself:
<bean class= "Com.isea533.mybatis.mapperhelper.MapperHelper" depends-on= "sqlsession" init-method= "Initmapper" SC Ope= "Singleton" lazy-init= "false" > <property name= "mappers" > <array> <value>com. isea533.mybatis.mapperhelper.mapper</value> </array> </property> <property name= "sqlsess Ions "ref=" sqlsession "/></bean>
There is no difference between the configuration and Spring3, the other needs to be guaranteed sqlSession , can be configured as follows:
<bean id= "sqlsession" class= "Org.mybatis.spring.SqlSessionTemplate" scope= "prototype" > <constructor-arg index= "0" ref= "sqlsessionfactory"/></bean>
Other configurations can be configured in the usual way, no special place, and if someone doesn't understand what the complete configuration looks like, here's an example:
Mybatis-spring4 Project
Applicationcontext.xml
Ii. inheritance
Mapper<T>Implementing your own Entity interface classes
Here Country2Mapper is an example:
Public interface Country2mapper extends mapper<country2> {//Omit other methods you have added}
If you click into the above Country2Mapper view, you will find there are some Example methods, these are generated by the code generator, the generated method does not contain a generic crud, only Example the method, there is a corresponding Country2Mapper.xml .
This example mainly shows that, in addition to the general mapper method, you can add their own methods, and the original no difference.
The entity code here is Country2 as follows:
@Table (name= "Country") public class Country2 {@Id private Integer Id; Private String CountryName; Private String CountryCode; Omit getter and Setter methods}
Here the corresponding table name is country . There is only one primary key id .
Third, use in the service
There are many ways to use the service.
The first, direct injection of the above defined
Country2Mapper
@Servicepublic class Demoservice {@Autowired private country2mapper mapper; Public list<country2> selectpage (int pagenum,int pageSize) {pagehelper.startpage (pagenum, pageSize); SPRING4 supports generic injection return mapper.select (NULL); }}
This way is too common, too ordinary, not much to explain here.
The second type, generic injection
This approach is rarely used, but SPRING4 supports generics injection, so on the first basis we can write the following code:
@Servicepublic class Demoservice {@Autowired private mapper<country2> Mapper; Public list<country2> selectpage (int pagenum,int pageSize) {//the page plug-in is used here Pagehelper pagehelper.startpage (Pagenum, pageSize); SPRING4 supports generic injection return mapper.select (NULL); }}
For those who do not know about generics injection, they may not be used Mapper<Country2> mapper to this type of writing, in fact, the advantages of this is not obvious. is not as clear as the first.
But in the second, we can elicit a third, or it might be a common service.
The third, General Service
The general operating database is in Service progress, inevitably write a large number of redundant crud methods, if you can have a generic Service , it will certainly reduce a lot of work.
Here, with a simple extension, a more complex package, you can practice according to your own situation.
Here's a simple example:
@Servicepublic abstract class BaseService<T> { @Autowired protected mapper<t> mapper; public int save (t entity) { return mapper.insert (entity); } public int delete (T entity) { return mapper.deletebyprimarykey (Entity); } /** * single-table paging query * * @param pageNum * @param pagesize * @return */ public list<t> selectpage (int pagenum,int pagesize) { pagehelper.startpage (PAgenum, pagesize); //spring4 supports generics injection return mapper.select (null); }}
Create an abstract class like the one shown above BaseService<T> , where three methods are encapsulated as simple examples. More complex logic is needed to explore it on your own.
Then modify the example just now DemoService :
@Servicepublic class Demoservice extends baseservice<country2>{}
Because BaseService<T> a single-table paging plug-in is encapsulated, DemoService there is no code in the present.
Suppose we want to add a save method that contains a checksum. Add the following code:
@Servicepublic class DemoService extends BaseService<Country2>{ public int save (Country2 country2) { if (country2 == null) { throw new nullpointerexception ("Saved objects cannot be empty!"); } if ( Country2.getcountrycode () == null | | country2.getcountrycode (). Equals ("")) { throw new runtimeexception ("Country code cannot be empty!"); } if ( Country2.getcountryname () == null | | country2.getcountryname (). Equals ("")) { throw new runtimeexception ("Country name cannot be empty!"); } return Super.save (Country2); }}
Above is just an example, whether to throw an exception you don't care.
It should also be seen from this example how convenient it is when using SPRING4 and Universal mapper.
About inheritance
Mapper<T>
Why do I have to design for the need to inherit Mapper<T> and realize myself at first Mapper ?
The main consideration is two aspects.
By <T> easily getting the type of the generic, you do not need to pass the entity type in a common method.
By inheriting Mapper , for example Country2Mapper , having a separate Mapper namespace, you can cache the results and do so without the need for interceptors.
Now with the Spring4, there is a very important reason.
support Generic injection , you can implement your own generic service, on the basis of general-purpose mapper to simplify operations, speed up development efficiency.
At last
if previously said General mapper is not as good as Mybatis-generator automatic generation , I can only say to see a person likes, do not need universal Mapper can not use, General Mapper just to meet a part of the needs of people.
Now, * * If there are people who say that General mapper is not as good as mybatis-generator, I would advise him to look at this document
In fact, there is no need to say that the better, suitable for their own good.
Also after reading this document, do not need to say * * General mapper than mybatis-generator automatically generated good * *, because I and some friends are translating **mybatis-generator**, and finally provide * * Integrated plug-ins for Mybatis-generator and Universal Mapper * *, you can generate entity classes directly with **mybatis-generator**, inherit entity mapper from generic Mapper, and XML files.
mybatis-generator Chinese document address : http://generator.sturgeon.mopaas.com/
Mybatis-generator official English address : http://mybatis.github.io/generator/index.html
This document has not been translated, and the level of translators is limited, if you find that translation errors or inappropriate places, you can mention issue at the address below
Submit Issue
The above address is just the post-generated project document address, not the project we use directly for translation.
Using the Universal mapper in Spring4