【mybatis】03-Mapper文件配置

 
一、select 
<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

说明: #{id}告诉mybatis创建PreparedStatement参数     vs     ORDER BY   ${columnName}: 不转义

select支持的参数说明,具体参考 https://mybatis.org/mybatis-3/zh/sqlmap-xml.html

   

二、insert, update 和 delete
 
支持主键自动生成的配置
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
  insert into Author (username,password,email,bio)
  values (#{username},#{password},#{email},#{bio})
</insert>
 
<update
  id="updateAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  timeout="20">

<delete
  id="deleteAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  timeout="20">

执行效果:

<mapper namespace="com.test.mapper.UserMapper">

    <insert id="save" useGeneratedKeys="true" keyProperty="user_id">
        insert into t_user (user_name, password, user_type, locked, credit, last_visit, last_ip)
        values ( #{user_name}, #{password}, #{user_type}, #{locked}, #{credit}, #{last_visit}, #{last_ip})
    </insert>
</mapper>

测试代码

User user = User.builder().user_name("aaa").password("123456").user_type(1).credit(100).last_ip("11.11.11.11").build();
userMapper.save(user);

【mybatis】03-Mapper文件配置

 【mybatis】03-Mapper文件配置 


三、sql
  定义可重用的 SQL 代码片段,以便在其它语句中使用
【mybatis】03-Mapper文件配置
 

四、ResultMap
1)基本使用: 
<!-- mybatis-config.xml 中,别名 -->
<typeAlias type="com.someapp.model.User" alias="User"/>

<!-- 解决列名不匹配 -->
<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>

<select id="selectUsers" resultMap="userResultMap">
  select user_id, user_name, hashed_password
  from some_table
  where id = #{id}
</select>

 

【mybatis】03-Mapper文件配置

上一篇:Javascript中apply、call、bind


下一篇:TabHost Tab的添加和删除