MyBatis General-Purpose Mapper for general example use

Source: Internet
Author: User
Tags table name

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.