<?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.sxt.dao.UserDao">
<select id="findAll" resultType="com.sxt.model.User">
select * from tb_user
</select>
<select id="findByID" parameterType="java.lang.Integer" resultType="com.sxt.model.User">
select * from tb_user where id=#{id}
</select>
<insert id="add" parameterType="com.sxt.model.User" >
insert into tb_user (username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address})
</insert>
<update id="update" parameterType="com.sxt.model.User" >
update tb_user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}
</update>
<delete id="delete" parameterType="com.sxt.model.User">
delete from tb_user where id=#{id}
</delete>
<select id="findByName" parameterType="java.lang.String" resultType="com.sxt.model.User">
select * from tb_user where username like '%${value}%'
</select>
<select id="findByNameAndSex" resultType="com.sxt.model.User" parameterType="com.sxt.vo.UserQuery">
select * from tb_user where username like '%${user.username}%' and sex=#{user.sex}
</select>
<select id="findAll2" resultType="com.sxt.model.User2">
select * from tb_user
</select>
<!-- 结果映射 -->
<resultMap type="com.sxt.model.User2" id="userResultMap">
<id property="id" javaType="java.lang.Integer" column="id"/>
<result property="_username" column="username"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday"/>
<result property="address" column="address"/>
</resultMap>
<select id="findAll2ResultMap" resultMap="userResultMap">
select * from tb_user
</select>
<select id="findByNameAndAddress" parameterType="java.util.HashMap" resultType="com.sxt.model.User" >
select * from tb_user
<!-- <where>
<if test="unam!=null and uname!=''">
and username like'%${uname}%'
</if>
<if test="address!=null and address!=''">
and address=#{address}
</if>
</where> -->
<include refid="userNameAndAddress"></include>
</select>
<sql id="userNameAndAddress">
<where>
<if test="unam!=null and uname!=''">
and username like'%${uname}%'
</if>
<if test="address!=null and address!=''">
and address=#{address}
</if>
</where>
</sql>
<select id="findUserByAddress" parameterType="com.sxt.vo.UserQuery" resultType="com.sxt.model.User">
select * from tb_user
<foreach collection="addresses" item="addr" open="where address in (" separator="," close=")">
#{addr}
</foreach>
</select>
</mapper>
mybatis映射文件详解
最新推荐文章于 2024-06-01 20:31:56 发布