mybatis一些简单配置

<?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">
<!-- namespace 标识惟一的映射 -->
<!-- user为model.User的别名,其中别名的定义在sqlMapConfig中定义的,命名空间model.User不能用别名代替 -->
<mapper namespace="model.User">
    <!-- id:sql语句的惟一标识 parameterType:传入数据的参数类型 resultType:
    返回值得数据类型 #{id}:接收参数的语法,若是是一个参数,参数名任意 -->
    <resultMap type="model.User"  id="baseResultMap">
        <id column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="birthday" property="birthday" />
        <result column="user_addr" property="userAddr" />
    </resultMap>
    <select id="selectUserById" parameterType="java.lang.Integer"
        resultType="model.User">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserById1" parameterType="java.lang.Integer"
        resultMap="baseResultMap">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserCount" parameterType="java.lang.Integer"
        resultType="java.lang.Integer">
        select count(*) from user
    </select>
    <!--resultType:返回系统定义的数据类型时使用 resultMap:返回自定义实体类型使用 -->
    <select id="selectUserByIdWithMap" parameterType="java.lang.Integer"
        resultType="java.util.Map">
        select * from user t where t.user_id = #{id}
    </select>
    <select id="selectUserAll" resultMap="baseResultMap">
        select * from user
    </select>
    <!-- 多个参数查询,使用map方式#{userName},userName为Map中的key,parameterType要设置成 java.util.Map -->
    <select id="selectUserById2" parameterType="java.util.Map"
        resultMap="baseResultMap">
        select * from user t where t.user_name=#{userName} and
        t.user_addr=#{userAddr}
    </select>
    <select id="selectUserById3" parameterType="model.Condition"
        resultMap="baseResultMap">
        select * from user t where t.user_name=#{userName} and
        t.user_addr=#{userAddr}
    </select>
    <select id="selectUserByLike" parameterType="java.util.Map"
        resultMap="baseResultMap">
        select *from user t where t.user_name like '%${userName}%'
    </select>
    
    <!-- 容易有输入输出问题 -->
    <select id="selectUserByIn" parameterType="java.util.Map" resultMap="baseResultMap">
        select *from user t where t.user_id in ${ids}
    </select>
    
    <!-- (1,2,3)
        collection:集合,用于接收map中的集合,ids必须是map中的key
        open:以某种字符开始
        close:以某种字符结束
        item:循环的每一项
        separator:用什么来分割
    -->
    <select id="selectUserByInWithForEach"  resultMap="baseResultMap">
            select *from user t where t.user_id in
            <foreach collection="ids" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
    </select>
    
    <insert id="insert" parameterType="model.User" >
        <!-- keyProperty:实体类里的主键 order:是主键生成的顺序,在MySQL中是AFTER resultType:是主键在实体类中的类型
            生成主键的sql:在MySQL中select LAST_INSERT_ID() -->
        <selectKey keyProperty="userId" order="AFTER" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        insert into user(user_id,user_name,birthday,user_addr)
        value (#{userId},#{userName},#{birthday},#{userAddr})
    </insert>java

    <update id="update" parameterType="model.User">
        update user t set t.user_name=#{userName},
        t.birthday=#{birthday},
        t.user_addr=#{userAddr}
        where t.user_id=#{userId}
    </update>
    <!-- delete在MySQL中不支持别名 -->
    <delete id="deleteUserById" parameterType="java.lang.Integer">
        delete from user where user_id=#{userId}
    </delete>
    <!-- 注意,引用上面的baseResultMap 是resultMap类型而不是resultType类型 -->
    <select id="selectUserIdByCondition" parameterType="java.util.Map" resultMap="baseResultMap">
    select * from user t
    <where>
        <if test ="userName!=null">
            t.user_name like '%${userName}%'
        </if>
        <if test="birthday !=null">
            <![CDATA[
            and t.birthday<  #{birthday}
            ]]>
                
        </if>
        <if test="userAddr!=null">
            and t.user_addr like '%${userAddr}%'
        </if>
    </where>
    </select>
    <!-- 动态更新 <set>能够自动处理最后一我的逗号 -->
    <update id="dynamicUpdate" parameterType="user">
        update user t
        <set>
            <if test="userName!=null">
                t.user_name=#{userName},
            </if>
            <if test="userAddr!=null">
                t.user_addr=#{userAddr},
            </if>
            <if test="birthday!=null">
                t.birthday=#{birthday}
            </if>
            where t.user_id=#{userId}
        </set>
    </update>
    
    
</mapper>sql

相关文章
相关标签/搜索