Mybatis of CRUD operations

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE Mapper 
        the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" 
        "http://mybatis.org/dtd/mybatis mapper.dtd--3 " > 
< Mapper namespace =" com.itheima.dao.IUserDao " > 

    <-! configuration query results correspondence between the attribute name and column names entity class -> 
    < the resultMap ID =" s userMap " type = "USER" > 
        <-! corresponding primary key field -> 
        < ID Property = "the userId" column = "ID" > </ ID > 
        <-! Corresponding to non-primary key field -> 
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>


    <!-- 查询所有 -->
    <select id="findAll" resultMap="userMap">
        <!--select id as userId,username as userName,address as userAddress,sex as userSex,birthday as userBirthday from user;-->
        select * from user;
    </select>

    <!-- 保存用户 -->
    <insert id="saveUser" parameterType="user">
        <!-- 配置插入操作后,获取插入数据的id -->
        <selectKey keyProperty="userId" keyColumn="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(username,address,sex,birthday)values(#{userName},#{userAddress},#{userSex},#{userBirthday});
    </insert>

    <!-- 更新用户 -->
    <update id="updateUser" parameterType="USER">
        update user set username=#{userName},address=#{userAddress},sex=#{userAex},birthday=#{userBirthday} where id=#{userId}
    </update>

    <!-- 删除用户-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id = #{uid}
    </delete>
    
    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultMap="userMap">
        select * from user where id = #{uid}
    </select>

    <!-- 根据名称模糊查询 -->
    <select id="findByName" parameterType="string" resultMap="userMap">
          select * from user where username like #{name}
        <!-- select * from user where username like '%${value}%'-->
   </select>

    <! - the total number of records acquired user -> 
    < SELECT ID = "findTotal" the resultType = "int" > 
        SELECT COUNT (ID) from User; 
    </ SELECT > 

    ! <- querying user according to the conditions of queryVo - -> 
    < SELECT ID = "findUserByVo" the parameterType = "com.itheima.domain.QueryVo" The resultMap = "s userMap" > 
        SELECT * WHERE username like from User # user.username {} 
    </ SELECT > 
</ Mapper >
  <!-- 根据名称模糊查询 -->
    <select id="findByName" parameterType="string" resultMap="userMap">
          select * from user where username like #{name}      //建议用这个

<!-- select * from user where username like '%${value}%'--> </select>
  #{}与${}的区别
     1.#{}表示一个占位符号
    通过#{}可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换,
    #{}可以有效防止 sql 注入。 #{}可以接收简单类型值或 pojo 属性值。 如果 parameterType 传输单个简单类
    型值,#{}括号中可以是 value 或其它名称。
     2.${}表示拼接 sql 串  
    通过${}可以将 parameterType 传入的内容拼接在 sql 中且不进行 jdbc 类型转换, ${}可以接收简
    单类型值或 pojo 属性值,如果 parameterType 传输单个简单类型值,${}括号中只能是 value。
 
新增用户后,同时还要返回当前新增用户的 id 值,因为 id 是由数据库的自动增长来实现的,所以就相
当于我们要在新增后将自动增长 auto_increment 的值返回。
<insert id="saveUser" parameterType="USER">
<!-- 配置保存时获取插入的 id --> 
    <selectKey keyColumn="id" keyProperty="id" resultType="int">
        select last_insert_id();
    </selectKey>
    insert into user(username,birthday,sex,address) 
    values(#{username},#{birthday},#{sex},#{address})
</insert>
        

 

 

Guess you like

Origin www.cnblogs.com/naigai/p/12031190.html