<?xml version="1.1" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace: 代表xml的名称,类似java包名-->
<mapper namespace="com.liang.dao.ProductDao">
<!--
select: 代表进行查询操作。
id: 之前的方法名称,具有唯一性。
resultType: 返回值类型。
如果返回的是对象,直接书写对象类型的的完整名。
如果是集合,书写的是集合的泛型
parameterType: 参数类型,可以省略。
-->
<select id="selectAll" resultType="product" >
select * from product
</select>
<select id="selectOne" resultType="product" >
select * from product where id=2
</select>
<select id="selectOne2" resultType="product" >
select * from product where id=#{param1} and name=#{param2}
</select>
<select id="selectOne3" resultType="product" >
select * from product where id=${param1} and name=#{param2}
</select>
<select id="selectLike1" resultType="product" >
select * from product where name like concat ('%','芒果','%')
</select>
<sql id="myColumn">
id,name,address,categoryId
</sql>
<select id="selectLike2" resultMap="myresultMap1">
select <include refid="myColumn"/>from product where name like concat ('%','芒果','%')
</select>
<resultMap id="myresultMap1" type="com.liang.model.Product">
</resultMap>
<select id="select" resultMap="myresultMap1">
select id,name,address,categoryId from product
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="ids!=null">
and id in
<foreach collection="ids" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="categoryId != null">
and categoryId= #{categoryId}
</if>
<if test="categoryIds != null">
and categoryId in
<foreach collection="categoryIds" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="name != null">
and name like concat('%',#{name},'%')
</if>
</where>
</select>
<insert id="insert">
insert into product (name,address) values (#{name},#{address})
<selectKey resultType="integer" keyColumn="newId" keyProperty="product" order="AFTER">
select last_insert_id() as newId
</selectKey>
</insert>
<update id="update">
insert into product (name,address) values (#{name},#{address})
</update>
<delete id="delete">
delete from product where id=#{param1}
</delete>
</mapper>
mapper.xml书写模板
于 2023-03-23 20:54:59 首次发布