亲宝软件园·资讯

展开

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

real_孟林洁 人气:0
想了解Mybatis增删改查mapper文件写法详解的相关内容吗,real_孟林洁在本文为您仔细讲解mybatis mapper写法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis,mapper写法,下面大家一起来学习吧。

  1. 插入

<mapper namespace="需要实现接口的全类名">
 <insert id="需要实现的接口里的方法名" parameterType="方法参数类型,如果是对象要写全类名">
  INSERT sql命令(命令里通过#{}获取对象属性)
  <!--注意属性名区分大小写 -->
 </insert>
<mapper>

EG:

<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. 查询

<select id="方法名" parameterType="方法参数类型" resultType="方法返回值类型,全类名">
 SELECT 表里字段名 AS 结果字段名 FROM 表名 WHERE 条件
 <!--注意:结果字段名与属性名保持一致,区分大小写-->
</select>

EG:

<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语句基本一致,直接贴代码

<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语句基本一致,直接贴代码

<delete id="deletePerson" parameterType="java.lang.Integer">
  DELETE FROM PRAC_PERSON WHERE P_ID=#{id}
</delete>

下面看下mybatis的mapper配置文件的一般写法

mapper.xml大致如下:

<?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文件写法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论