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