1. 插入
1
2
3
4
5
6
|
<insert id= "需要實現的接口里的方法名" parameterType= "方法參數類型,如果是對象要寫全類名" > INSERT sql命令(命令里通過#{}獲取對象屬性) <!--注意屬性名區分大小寫 --> </insert> <mapper> |
EG:
1
2
3
4
5
|
<mapper namespace= "com.mlj.dao.PersonDao" > <insert id= "insertPerson" parameterType= "com.mlj.entity.Prac_Person" > INSERT INTO PRAC_PERSON(p_NAME,P_PASSWORD) VALUES(#{name},#{password}) </insert> </mapper> |
2. 查詢
1
2
3
4
|
<select id= "方法名" parameterType= "方法參數類型" resultType= "方法返回值類型,全類名" > SELECT 表里字段名 AS 結果字段名 FROM 表名 WHERE 條件 <!--注意:結果字段名與屬性名保持一致,區分大小寫--> </select> |
EG:
1
2
3
4
5
6
7
8
|
<resultMap type= "Address" id= "address" > <result column= "A_PERSON" property= "personId" /> <result column= "A_ADDRESS" property= "address" /> <result column= "A_NUMBER" property= "number" /></resultMap> <select id= "selectAddressByPersonId" parameterType= "java.lang.String" resultMap= "address" > SELECT * FROM PRAC_ADDRESS LEFT JOIN PRAC_PERSON ON A_PERSON=#{personId} AND PRAC_ADDRESS.A_PERSON=PRAC_PERSON.P_ID </select> |
此處先配置resultMapp,使表列名與屬性名一致。
3.修改
與前面插入除了sql語句基本一致,直接貼代碼
1
2
3
4
|
< update id= "updatePersonInformation" parameterType= "com.mlj.entity.Prac_Person" > UPDATE PRAC_PERSON SET P_NAME=#{ name },P_PASSWORD=#{ password } WHERE P_ID=#{id} <! -- 屬性字段名區分大小寫 --> </ update > |
4.刪除
與前面插入除了sql語句基本一致,直接貼代碼
1
2
3
|
< delete id= "deletePerson" parameterType= "java.lang.Integer" > DELETE FROM PRAC_PERSON WHERE P_ID=#{id} </ delete > |
下面看下mybatis的mapper配置文件的一般寫法
mapper.xml大致如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?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.hzcominfo.voucher.CommodityCategoryManager" > <cache-ref namespace= "com.hzcominfo.dataggr.cloud" /> <insert id= "insertCommodityCategoryManager" parameterType= "com.hzcominfo.voucher.mapper.CommodityCategoryManager" keyProperty= "id" > INSERT INTO COMMODITY_CATEGORY_MANAGER ( <include refid= "fields" /> ) VALUES ( <include refid= "values" /> ) </insert> <update id= "updateCommodityCategoryManager" parameterType= "com.hzcominfo.voucher.mapper.CommodityCategoryManagerKey" > UPDATE COMMODITY_CATEGORY_MANAGER <include refid= "set" /> <include refid= "where" /> </update> <update id= "deleteCommodityCategoryManager" parameterType= "com.hzcominfo.voucher.mapper.CommodityCategoryManagerKey" > DELETE FROM COMMODITY_CATEGORY_MANAGER <include refid= "where" /> </update> <select id= "selectCommodityCategoryManager" parameterType= "String" resultType= "com.hzcominfo.voucher.mapper.CommodityCategoryManager" > SELECT * FROM COMMODITY_CATEGORY_MANAGER <include refid= "where" /> </select> <select id= "selectCommodityCategoryManagerByCriteria" parameterType= "net.butfly.albacore.dbo.criteria.Criteria" resultType= "com.hzcominfo.voucher.mapper.CommodityCategoryManagerKey" > SELECT CATEGORY_ID, USER_ID FROM COMMODITY_CATEGORY_MANAGER <include refid= "where" /> </select> <select id= "countCommodityCategoryManagerByCriteria" parameterType= "net.butfly.albacore.dbo.criteria.Criteria" resultType= "long" > SELECT count(*) FROM COMMODITY_CATEGORY_MANAGER <include refid= "where" /> </select> <sql id= "fields" > < if test= "categoryId!=null" >CATEGORY_ID</ if > < if test= "userId!=null" >,USER_ID</ if > </sql> <sql id= "values" > < if test= "categoryId!=null" >#{categoryId}</ if > < if test= "userId!=null" >,#{userId}</ if > </sql> <sql id= "set" > <set> <trim prefix= "" prefixOverrides= "," > < if test= "categoryId!=null" >,CATEGORY_ID=#{categoryId}</ if > < if test= "userId!=null" >,USER_ID=#{userId}</ if > </trim> </set> </sql> <sql id= "where" > <where> <trim prefix= "" prefixOverrides= "and|or" > < if test= "categoryId!=null" >AND CATEGORY_ID=#{categoryId}</ if > < if test= "userId!=null" >AND USER_ID=#{userId}</ if > </trim> </where> </sql> </mapper> |
以上所述是小編給大家介紹的Mybatis增刪改查mapper文件寫法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/menglinjie/article/details/58624060