Mabatis Mapper Development Code

Source: Internet
Author: User

Define a Mapper interface

 Package mapper; Import Pojo. User;  Public Interface Usermapper {    public User getUser (intthrows  Exception;       Public void throws Exception;}

Define a usermapper.xml point to it, note that namespace needs to point to its full path, the SQL statement ID is the same as the method name, and then the arguments and the results are all in the same way as the method arguments.

The XML name is not necessarily the same as the interface name, it is searched by namespace.

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Mapper." Usermapper ">    <SelectID= "GetUser"ParameterType= "int"Resulttype= "User">Select ID, name, age, Gmt_create gmtcreate from users where Id=#{id}</Select>        <InsertID= "Insertuser"ParameterType= "User">        <SelectkeyKeyproperty= "id"Order= "after"Resulttype= "Java.lang.Integer">SELECT last_insert_id ()</Selectkey>INSERT into users (name, age, Gmt_create, money) VALUE (' Xiao ', ' $ ', ' 1991-06-26 ', 10000)</Insert>

Test

Private sqlsessionfactory sqlsessionfactory; @Beforepublic void SetUp () throws exception{sqlsessionfactory = Mybatisutils.getfactory ();} @Testpublic void Testgetuser () throws Exception{sqlsession sqlsession = Sqlsessionfactory.opensession (); Usermapper usermapper = Sqlsession.getmapper (Usermapper.class); User user = Usermapper.getuser (1); SYSTEM.OUT.PRINTLN (user);}

Returns the proxy class through Sqlsession.getmapper.

Intelligently chooses to use SelectOne or selectlist based on the parameters returned, that is, the programmer can define

Public User getUser (int id) throws Exception;

Mabatis Mapper Development Code

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.