MyBatis General-Purpose Mapper General example use, of course, there is no general mapper common example use
Want to lazy not write a lot of mybatis in the Mapper.xml file, you want to use the generic mapper class to reduce the workload.
First, I'm using a MAVEN project, so importing Mapper's maven dependency
[HTML] view plain copy <dependency> <groupId>tk.mybatis</groupId> <artifactid> ;mapper</artifactid> <version>3.2.0</version> </dependency>
There is also a necessary dependency: The project relies on JPA annotations and needs to add Maven dependencies: [HTML] view plain copy <dependency> <groupid>javax.persis tence</groupid> <artifactId>persistence-api</artifactId> <version>1.0</version> &L T;/dependency>
Next, configure mapper [HTML] View plain copy in configuration file Applicationcontext.xml <bean class= "Tk.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "Basepackage" value= "Com.isscas.ucqcs.common.dao"/> <property name= "Properties" > <value> mappers=tk.mybatis.mapper.common.mapper //This is the Mapper interface configuration, which is not writable when the interface is configured for this default </value> </ property> </bean>
Modify the MyBatis configuration org directly to TK (HTML) view plain copy <bean class= "Org.mybatis.spring.mapper.MapperScannerCon Figurer ">
Here, the configuration of mapper has been fully completed.
All methods in the generic mapper class can be called as long as the Mapper<t> interface is inherited on its own mapper interface.
also note that the Mapper class has its own parsing method for the Entity class: Table name and field name will default to the class name, hump to underline (that is, usernamed corresponding table name/field name user_name), using @column (name = "real name") You can specify the table name/field name.
Also, you need @id to mark the primary key field, and for unwanted fields, you can @tranisent ignore
The Mapper interface contains a single table of additions and deletions to check the paging function.
A query example is given below: [HTML] view plain copy countrymapper mapper = Sqlsession.getmapper (Countrymapper.class); Query all list<country> countrylist = Mapper.select (New country ()); Total//General Example query Example Example = new Example (country.class); Example.createcriteria (). Andgreaterthan ("id", 100);//The conditions given here are id>100 countrylist = Mapper.selectbyexample ( example);