Mapper.xml configuration file Detailed

Source: Internet
Author: User

--The namespace is typically the path to the Maper interface that corresponds to the mapper mapping file

<mapper namespace= "com.harbsoft.com.mybatis.mapper.UserMapper" >


--Turn on level two cache (entity classes must be serialized)

<cache type= "Org.mybatis.caches.ehcache.EhcacheCache"/>


--Extracting a common SQL

<sql id= "User_query_where" >

General SQL

</sql>


--if

<if test= "Id!=null and id!=" >

usually a WHERE condition statement

</if>


--foreach

<foreach collection= "IDs" item= "id" open= "and (" close= ")" separator= "or" >

User.id=#{id}

</foreach>


--$

and user.username like ' ${username}% '


--#

and user.sex = #{sex}


--resultmap corresponds to the mapping of the table to the entity class-the entity class that corresponds to the Type database table, the alias or the full class name can

<resultmap type= "Person" id= "Resultmapperson" >

<!--The primary key of the result set--

--PRIMARY Key <id/>

<id property= "userid" column= "id"/>

<!--normal Columns--

--column is a field in a database, the property is a field in an entity class

<result property= "name" column= "username"/>

<result property= "addr" column= "Address"/>

</resultMap>


-one-to-one relationship processing (an order corresponds to a user, which is equivalent to a field in a class, which is an object)

<association property= "user" javatype= "Com.harbosoft.mybatis.po.User" >

<id property= "id" column= "user_id"/>

<result property= "username" column= "username"/>

<result property= "Sex" column= "sex"/>

<result property= "Address" column= "Address"/>

</association>



--one-to-many relationship processing (a user has multiple orders, which is equivalent to a field in a class, which is a collection)

<!--order Information--

<collection property= "Orders" oftype= "Com.harbosoft.mybatis.po.Orders" >

<!--order number--

<result property= "Order_number" column= "Order_number"/>

<result property= "id" column= "id"/>

</collection>


Three can be nested use

One user--------multiple orders-------multiple order Details

One user--------multiple orders-------multiple order Details--------multiple items



--select

Public User finduserbyid(int ID)

<select id= "finduserbyid" parametertype= "int" resulttype= "user" >

SELECT * from USER WHERE id=#{ID}

ID-------The method name of the Mapper interface;

ParameterType types of method parameters-------the Mapper interface

Resulttype The return value type of the method that---------the Mapper interface

User----------is an alias (full name is Com.harbosoft.mybatis.Items)

The ID and formal parameters remain the same (#)

</select>


--The return value is the list user

Public list<User> finduserbyname(String username)

<select id= "finduserbyname" parametertype= "string" resulttype= "user" >

SELECT * from USER WHERE username like ' ${value}% '

The method returns a value of type list, but the collection is loaded with user, so the value of Resulttype is as long as it is stored in the collection

Value can be arbitrary, anything can ($)

</select>


--The return value is List parameter is User Resulttype

Public list<User> Finduserlist (user user) throws Exception;

<!--Comprehensive query user information--

<select id= "finduserlist" parametertype= "user" resulttype= "user" >

SELECT * from USER

<where>

<!--user's query criteria--

<include refID= "User_query_where"/>

the article is lost and may be reused, so extract it and use include when referencing

</where>

</select>



<sql id= "User_query_where" >
<if test= "Id!=null and id!=" >
and User.id=#{id}

</if>

<foreach collection= "IDs" item= "id" open= "and (" close= ")" separator= "or" >
User.id=#{id} and (UserID =
</foreach>
<if test= "Username!=null and username!=" >
and user.username like ' ${username}% '
</if>
<if test= "Sex!=null and sex!=" >
and user.sex = #{sex}
</if>

</sql>


--The return value is List Resultmap

Public list<person> finduserlistresultmap(user user) throws Exception;

<!--Comprehensive query user information using resultmap-->

<select id= "finduserlistresultmap" parametertype= "user" resultmap= "Resultmapperson" >

SELECT * from USER WHERE username like ' ${username}% ' and Sex=#{sex}

</select>


--parameter is map HashMap resulttype

Public list<user> Finduserlistbyhashmap(map map) throws Exception;

<!--query user information via HashMap-

<select id= "finduserlistbyhashmap" parametertype= "hashmap" resulttype= "User" >

SELECT * from USER WHERE username like ' ${name}% ' and Sex=#{sex}

</select>


--The return value is map Resulttype

Public Map finduserbyidreturnmap(int ID) throws Exception;

<!--get individual user information back HashMap--

<select id= "finduserbyidreturnmap" parametertype= "int" resulttype= "hashmap" >

SELECT * from USER WHERE id=#{ID}

</select>



--insert

public void Insertuser(user user) throws Exception;

<insert id= "insertuser" parametertype= "user" >

<!--keyproperty: Specify properties of Pojo objects for primary key mappings

Order:selectkey The order of execution, MySQL is set here for after

When actually used in an enterprise, the primary key usually uses the UUID (), which is the SELECT uuid ()

-

<selectkey keyproperty= "id" order= "after" resulttype= "int" >

SELECT last_insert_id ()

</selectKey>

INSERT into USER (Username,birthday,sex,address,detail,score)

VALUES (#{username},#{birthday},#{sex},#{address},#{detail},#{score})

</insert>

--update

public void Updateuserbyid(user user) throws Exception;

<update id= "updateuserbyid" parametertype= "com.harbsoft.mybatis.po.User" >

Update user Set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address},detail=#{detail},score=#{ Score}
Where Id=#{id}

</update>


--delete

public void Deleteuserbyid(int ID) throws Exception;

<delete id= "deleteuserbyid" parametertype= "java.lang.Integer" >
Delete from user where id=#{value}

</delete>


Mapper.xml configuration file Detailed

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.