JavaEE 学习专题程序员

框架之MyBatis核心配置

2017-08-01  本文已影响0人  vaneL

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 test="title != null">
    AND title like #{title}
</if>
<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>
<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 item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
</foreach>
上一篇 下一篇

猜你喜欢

热点阅读