<?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="Person"> <resultMap type="entity.Person" id="Person"> <!-- resultMap标签:映射实体与表 type属性:表示实体全路径 id属性:为实体与表的映射取一个任意唯一的名字 --> <id column="id" jdbcType="INTEGER" property="id"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="age" jdbcType="INTEGER" property="age"/> <result column="remark" jdbcType="VARCHAR" property="remark"/> </resultMap> <!--添加人员信息--> <insert id="addPerson" parameterType="entity.Person"> INSERT INTO person (name, age, remark) VALUES (#{name}, #{age} ,#{remark}) </insert> <!--根据id删除人员--> <delete id="deletePersonById" parameterType="int" > DELETE FROM person WHERE id = #{id} </delete> <!--根据id修改人员信息--> <update id="updatePersonById" parameterType="entity.Person"> UPDATE person SET name = #{name} , age = #{age}, remark = #{remark} WHERE id = #{id}; </update> <!-- 根据id获取用户信息 --> <select id="getPersonById" parameterType="int" resultType="entity.Person"> SELECT * FROM person WHERE id = #{id} </select> <!--无条件分页分页查询--> <select id="getPersonList" parameterType="map" resultType="entity.Person"> SELECT id, name, age, remark FROM person LIMIT #{start}, #{limit} </select> <!--根据不同条件查询人员,动态查询--> <select id="Person.getPersonList2" parameterType="map" resultType="entity.Person"> SELECT id, name,age,remark FROM person <where> <if test="name != null"> AND name = #{name}</if> <if test="age != null">AND age = #{age}</if> <if test="remark != null">AND remark = #{remark}</if> </where> </select> <!--动态更新员工信息--> <update id="updatePerson1" parameterType="entity.Person" > UPDATE person <set> <if test="name != null"> name = #{name},</if> <if test="age != null"> age = #{age},</if> <if test="remark != null"> remark = #{remark},</if> </set> WHERE id = #{id} </update> <!--动态删除人员--> <delete id="deldetPerson1"> DELETE FROM person WHERE id IN <!--foreach用于迭代数组元素, collection:需遍历的数组 open:起始位置的标示 close:结束位置的标示 separator:元素的分隔符 item:需迭代数组的名称:可以写成任意字符,但必须与下面#{}内的字符相同 ${id}:迭代的实际元素 使用list时写法一样,只是collection="list"--> <foreach collection="array" open="(" close=")" separator="," item="id"> #{id} </foreach> </delete> <!--动态增加员工信息--> <insert id="addPerson1" parameterType="entity.Person"> <!--<include refid="key"/>和<include refid="value"/>表示引用下方的sql片段--> INSERT INTO person (<include refid="key"/>) VALUES (<include refid="value"/>) </insert> <!--书写sql片段--> <sql id="key"> <!--去掉最后一个逗号--> <trim suffixOverrides="," > <if test="name != null">name,</if> <if test="age != null">age,</if> <if test="remark != null">remark,</if> </trim> </sql> <sql id="value"> <trim suffixOverrides=","> <if test="name != null">#{name},</if> <if test="age != null">#{age},</if> <if test="remark != null">#{remark},</if> </trim> </sql> </mapper>
7.mybatis的动态查询和分页