服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Mybatis增删改查mapper文件写法详解

Mybatis增删改查mapper文件写法详解

2020-08-21 12:02real_孟林洁 Java教程

这篇文章主要介绍了Mybatis增删改查mapper文件写法的相关资料,需要的朋友可以参考下

  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>

下面看下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

延伸 · 阅读

精彩推荐