首页 > 技术文章 > Mybatis

roxy 2017-09-21 18:13 原文

 

Mybtais:

  mybatis支持普通SQL查询,存储过程和高级映射的持久层框架

  mybatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索

  mybatis使用简单的XML或注解用于配置和原始映射,将接口和POJO影射成数据库中的记录

Mybatis的底层实现:

  参考博客:http://blog.csdn.net/column/details/mybatis-principle.html

       http://xiaohuafyle.iteye.com/blog/2142894

   使用dom4j将配置文件读取出来,使用动态代理动态创建代理对象,中间的调用方法和过程是使用java的反射机制

Maven项目中Mybatis的基本配置:

        <!-- 数据库连接,以mysql为例 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        
        <!-- Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

 

 

 

 

 

  2 实体类中的属性名和表中的字段名不一致

1 在查询语句中使用别名
  通过在查询语句中定义字段名的别名,使别名和实体类的属性名一致
  
  <select id="selectBlog2" parameterType="int" resultType="Blog">
        select
        `id`,
        `title`,
        `author_id` as authorId,
        `state`,
        `featured`,
        `style`
        from
        blog where id = #{id}
  </select>

2 使用ResultMap
  通过resultMap来映射字段名和实体类属性名的一一对应关系
  用id属性来映射主键字段
  用result属性来映射非主键字段


    <resultMap type="Blog" id="blogResultMap">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="author_id" property="authorId" jdbcType="INTEGER" />
    </resultMap>
  
   <select id="selectBlog3" parameterType="int" resultMap="blogResultMap">
        select * from blog where id = #{id}
    </select>

  3 排序时接受参数的字符选择问题

 如果使用#,那么sql不会报错,但是排序功能不能使用;所以应该使用$(参数是表名或是列名)
中文排序,需要使用mysql的转换函数 CONVERT()

    <select id="selectBySort" parameterType="string" resultMap="blogResultMap">
        select * from blog order by CONVERT(${value} USING GBK)
    </select>

  4 在mapper中如何传递多参数

1 使用索引,按照参数的位置从0开始
  
  <select id="selectByPage" resultMap="blogResultMap">
        select * from blog limit
        #{0}, #{1}
    </select>

2 使用接口注解,注意占位参数的名字要和注解参数的名字一致
  Java的反射机制并不能让框架获取到参数的名字(方法签名中只有参数类型,名字毫无意义),所以mybatis默认的命名为:param 1, pram2...
  如果想给他们指定名称,可以使用@param注解


  List<Blog> selectByPage2(
        @Param(value="offset") int offset,
        @Param(value="pagesize") int pagesize);

  <select id="selectByPage2" resultMap="blogResultMap">
        select * from blog limit
        #{offset}, #{pagesize}
    </select>

3 使用map传参,注意占位参数的名字要和map中的key一一对应

   Map<String, Object> map = new HashMap<String, Object>();
        map.put("offset", 2);
        map.put("pagesize", 2);
        
    List<Blog> blogList = blogMapper.selectByPage3(map);

   <select id="selectByPage3" resultMap="blogResultMap">
        select * from blog limit
        #{offset}, #{pagesize}
    </select>

   5 获取刚刚插入数据的id(带有自增主键)

1 配置属性useGeneratedKeys="true" keyProperty="id"

  <insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">

2 在全局配置文件中配置settings选项,并且在mapper的insert节点配置属性keyProperty="id"

  <!-- 自增主键 -->
    <settings>
      <setting name="useGeneratedKeys" value="true" />
    </settings>

  <insert id="insertBlog2" parameterType="Blog" keyProperty="id">

3 直接查询

    <insert id="insertBlogMySql">
        <selectKey resultType="java.lang.Integer" order="AFTER"
            keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

4 没有自增主键的数据库查询(oracle)

  <insert id="insertBlogOracle">
        <selectKey resultType="java.lang.Integer" order="BEFORE"
            keyProperty="id">
            select seq.nextval as id from dual
        </selectKey>
    </insert>

  

推荐阅读