1. 插入
?
1
2
3
4
5
6
|
<mapper namespace= "需要实现接口的全类名" >
<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 >
|
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文件写法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/menglinjie/article/details/58624060
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 37
-
2025-05-25 41
-
2025-05-25 24
-
在SpringBoot下读取自定义properties配置文件的方法
2025-05-27 97 -
2025-05-25 94
热门评论