您好,登錄后才能下訂單哦!
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文件寫法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。