Using the Mybatis3--mapping file method

Source: Internet
Author: User

1.pom.xml Environment Construction Reference http://my.oschina.net/u/555061/blog/507474

2. Give the directory structure,

3. Unlike the annotation method, the Usersmapper.java below the DAO package is no longer annotated,

Package Com.xuebaosoft.mybatis3.mybatis_mapper.dao;import Java.util.list;import Java.util.map;import    Com.xuebaosoft.mybatis3.mybatis_mapper.model.users;public interface Usersmapper {int deletebyprimarykey (String ID);    int Insert (Users record);    int insertselective (Users record);    Users Selectbyprimarykey (String ID);    int updatebyprimarykeyselective (Users record); int Updatebyprimarykey (Users record); List<users> userpaginglist (map<string, object> offsetandpagesize);}

In fact, this is the Userdao.java with the annotated chapter http://my.oschina.net/u/555061/blog/507474 introduced in order to Usersmapper.java

4. Look at the configuration file and see how the annotations function is implemented in mapping mode, Mybatis-config.xml configured as follows,

<?xml version= "1.0"  encoding= "UTF-8"  ?>  <! doctype configuration public  "-//ibatis.apache.org//dtd config 3.0//en"   "HTTP// IBATIS.APACHE.ORG/DTD/IBATIS-3-CONFIG.DTD "><configuration><environments default=" Environment " ><environment id= "Environment" ><transactionmanager type= "JDBC"  /><dataSource  type= "Pooled" ><property name= "Driver"  value= "Com.mysql.jdbc.Driver"  />< Property name= "url" value= "Jdbc:mysql://192.168.191.1:3306/xuebaodb?characterencoding=utf-8"  /> <property name= "username"  value= "root"  /><property name= "password"  value= " Root " /></dataSource></environment></environments><mappers><mapper  Resource= "Usersmapper.xml" &NBSP;/></MAPPERS></CONFIGURATION> 

This is actually an increase:

<mappers><mapper resource= "Usersmapper.xml"/></mappers>

It is said that when the configuration Mybatis-config.xml is loaded, the Usersmapper.xml is registered with the environment. And in Usersmapper.xml the Com.xuebaosoft.mybatis3.mybatis_ is quoted. Mapper.dao.UsersMapper This class, so the equivalent of the DAO Layer Mapper class has, the mapping SQL Mapper.xml has, DataSource has been configured, so that all the environment has been built.

5.usersmapper.xml as follows,

<?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 " ><mapper namespace=" Com.xuebaosoft.mybatis3.mybatis_ Mapper.dao.UsersMapper "><resultmap id=" Baseresultmap "type=" Com.xuebaosoft.mybatis3.mybatis_ Mapper.model.Users "><id column=" id " property=" id " jdbctype=" VARCHAR " /><result  column= "username"  property= "username"  jdbctype= "VARCHAR"  /><result column= " Password " property=" password " jdbctype=" VARCHAR " /><result column=" name "  property= "name"  jdbctype= "VARCHAR"  /><result column= "nickname"  property= "nickname"  jdbctype= "varchar"  /><result column= "sex"  property= "sex"  jdbctype= "varchar"  /><result column= "Picture"  property= "Picture"  jdbctype= "VARCHAR" &nbSp;/><result column= "Createtime"  property= "Createtime"  jdbctype= "VARCHAR"  />< Result column= "Lastlogintime"  property= "Lastlogintime" jdbctype= "VARCHAR"  /><result  Column= "Tilepath"  property= "Tilepath"  jdbctype= "VARCHAR"  /></resultmap><sql id = "Base_column_list" >id, username, password, name, nickname, sex, picture,  createtime,lastlogintime,tilepath</sql><select id= "Selectbyprimarykey"  resultMap= " Baseresultmap "parametertype=" java.lang.String ">select<include refid=" Base_column_list " /> From userswhere id = #{id,jdbctype=varchar}</select><delete id= " Deletebyprimarykey " parametertype=" java.lang.String ">delete from userswhere id =  #{id,jdbctype=varchar}</delete><insert id= "Insert"  parametertype= " Com.xuebaosoft.mybatis3.mybatis_mapper.model.Users ";insert into users  (Id, username, password,name, nickname, sex,picture,  createtime, lastlogintime,tilepath) values  (#{id,jdbctype=varchar}, #{username,jdbctype= Varchar},#{password,jdbctype=varchar},#{name,jdbctype=varchar}, #{nickname,jdbctype=varchar}, #{sex, Jdbctype=varchar},#{picture,jdbctype=varchar}, #{createtime,jdbctype=varchar},#{lastlogintime,jdbctype= Varchar},#{tilepath,jdbctype=varchar}) </insert><insert id= "Insertselective"  parameterType= " Com.xuebaosoft.mybatis3.mybatis_mapper.model.Users ">insert into users<trim prefix=" ("  suffix= ")"  suffixoverrides= "," ><if test= "Id != null" >id,</if><if  test= "Username != null" >username,</if><if test= "Password != null" >password,</if><if test= "Name != null" >name,</if><if test= " Nickname != null ">niCkname,</if><if test= "Sex != null" >sex,</if><if test= "picture !=  null ">picture,</if><if test=" Createtime != null ">createtime,</if> <if test= "Lastlogintime != null" >lastlogintime,</if><if test= "Tilepath  != null ">tilepath,</if></trim><trim prefix=" values  (" suffix=") "  suffixoverrides= "," ><if test= "Id != null" >#{id,jdbcType=VARCHAR},</if>< If test= "Username != null" >#{username,jdbctype=varchar},</if><if test= " Password != null ">#{password,jdbctype=varchar},</if><if test=" name !=  Null ">#{name,jdbctype=varchar},</if><if test=" Nickname != null ">#{nickname, Jdbctype=varchar},</if><if test= "Sex != null" >#{sex,jdbcType=VARCHAR},</if> <if test= "Picture !=&nbsP;null ">#{picture,jdbctype=varchar},</if><if test=" Createtime != null ">#{ Createtime,jdbctype=varchar},</if><if test= "Lastlogintime != null" >#{lastlogintime, Jdbctype=varchar},</if><if test= "Tilepath != null" >#{tilepath,jdbctype=varchar}, </if></trim></insert><update id= "Updatebyprimarykeyselective"  parameterType= " Com.xuebaosoft.mybatis3.mybatis_mapper.model.Users ">update users<set><if test=" username  != null ">username = #{username,jdbctype=varchar},</if><if test=" password  != null ">password = #{password,jdbctype=varchar},</if><if test=" name  != null ">name = #{name,jdbctype=varchar},</if><if test=" nickname !=  null ">nickname = #{nickname,jdbctype=varchar},</if><if test=" sex !=  null ">sex = #{sex,jdbctype=varchar},</if><if test= "Picture != null" >picture = #{ Picture,jdbctype=varchar},</if><if test= "Createtime != null" >createtime =  #{createtime,jdbctype=varchar},</if><if test= "Lastlogintime != null" > Lastlogintime = #{lastlogintime,jdbctype=varchar},</if><if test= "tilepath !=  null ">tilepath = #{tilepath,jdbctype=varchar},</if></set>where id =  #{id,jdbctype=varchar}</update><update id= "Updatebyprimarykey"  parameterType= " Com.xuebaosoft.mybatis3.mybatis_mapper.model.Users ">update usersset username = #{ Username,jdbctype=varchar},password = #{password,jdbctype=varchar},name = #{name,jdbctype= Varchar},nickname = #{nickname,jdbctype=varchar},sex = #{sex,jdbctype=varchar},picture  = #{picture,jdbctype=varchar},createtime&nBsp;= #{createtime,jdbctype=varchar},lastlogintime = #{lastlogintime,jdbctype=varchar},tilepath  = #{tilepath,jdbcType=VARCHAR}where id = #{id,jdbcType=VARCHAR}</update>< Select id= "Userpaginglist"  parametertype= "map" resultmap= "Baseresultmap" >select<include  Refid= "Base_column_list"  />from users limit #{offset},#{pagesize}</select></ Mapper>

6.junit for testing,

package com.xuebaosoft.mybatis3.mybatis_mapper;import java.util.list;import  junit.framework.test;import junit.framework.testcase;import junit.framework.testsuite;import  Com.xuebaosoft.mybatis3.mybatis_mapper.model.users;import com.xuebaosoft.mybatis3.mybatis_ mapper.service.userservice;import com.xuebaosoft.mybatis3.mybatis_mapper.service.impl.userserviceimpl; Public class apptest extends testcase {public apptest (String testName)  {super (testname);} Public static test suite ()  {return new testsuite (AppTest.class);} Public void testapp ()  {userservice userservice = new userserviceimpl ();// Users user = new users ("123321",  "Zhangsan",  "111111",  "name",//"nickname",   "Sex",  "picture",  "createtime",//"Lastlogintime",  "Tilepath");//userservice.update (user); Users user = userservice.getuserByid ("123321"); SYSTEM.OUT.PRINTLN (user); List<users> list = userservice.getusers (0,&NBSP;10); SYSTEM.OUT.PRINTLN (list);}}


Using the Mybatis3--mapping file method

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.