框架之MyBatis核心配置

configuration 配置
-properties 属性
-settings 设置
-typeAliases 类型命名
-typeHandlers 类型处理器
-objectFactory 对象工厂
-plugins 插件
-environments 环境
--environment 环境变量
--transactionManager 事务管理器
-dataSource 数据源
-mappers 映射器

<configuration>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>
mapper.xml[insert、update、select、delete]
<?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.neusoft.flycity.dao.BillDao">
    <insert id="addBill">
        INSERT INTO bill(ticketNum,ticketPrice,totalPrice,bookingID)
        VALUES (#{bookNum},#{price},#{price}*#{bookNum},#{bookingID})
    </insert>

    <delete id="deleteBill" parameterType="int">
        DELETE FROM bill where bookingID = #{bookingID}
    </delete>

    <select id="getById" parameterType="int" resultType="Bill">
        SELECT * FROM bill WHERE bookingID = #{bookingID}
    </select>
</mapper>

核心对象

SqlSessionFactory

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

SqlSession

SqlSession session = sqlSessionFactory.openSession();

SqlSessionFactoryBuilder
这个类可以被实例化、使用和丢弃,一旦创建了 SqlSessionFactory,就不再需要它了。
因此 SqlSessionFactoryBuilder 实例的最佳作用域是方法作用域(也就是局部方法变量)。
SqlSessionFactory
SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,没有任何理由对它进行清除或重建。
因此 SqlSessionFactory 的最佳作用域是应用作用域。有很多方法可以做到,最简单的就是使用单例模式或者静态单例模式。
SqlSession
每个线程都应该有它自己的 SqlSession 实例。SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域。

动态SQL

  • if
<if test="title != null">
    AND title like #{title}
</if>
  • choose (when, otherwise)
<choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
</choose>
  • trim (where, set)
<where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
</where>

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>
<set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
</set>

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>
  • foreach
<foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
</foreach>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,172评论 0 4
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,492评论 11 349
  • 官方文档 简介 入门 XML配置 XML映射文件 动态SQL Java API SQL语句构建器 日志 一、 JD...
    拾壹北阅读 8,861评论 0 52
  • 一、安装 使用MyBatis,首先需要下载其jar包,然后将jar包导入lib中即可,下载地址:github.co...
    涉务阅读 1,859评论 0 0
  • 广告大师约翰·沃纳梅克曾经提出过著名的广告营销界“哥德巴赫猜想”:“我知道我的广告费有一半是浪费的,但我不知道浪费...
    9c42a2489e1d阅读 5,229评论 3 7

友情链接更多精彩内容