欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 10:44:12
  1. 插入

  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文件写法,希望对大家有所帮助