configuration 配置
-properties 屬性
-settings 設(shè)置
-typeAliases 類型命名
-typeHandlers 類型處理器
-objectFactory 對象工廠
-plugins 插件
-environments 環(huán)境
--environment 環(huán)境變量
--transactionManager 事務(wù)管理器
-dataSource 數(shù)據(jù)源
-mappers 映射器
<configuration>
<!-- 配置全局屬性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys獲取數(shù)據(jù)庫自增主鍵值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列別名替換列名 默認(rèn):true -->
<setting name="useColumnLabel" value="true" />
<!-- 開啟駝峰命名轉(zhuǎn)換: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
這個(gè)類可以被實(shí)例化、使用和丟棄传轰,一旦創(chuàng)建了 SqlSessionFactory膘格,就不再需要它了。
因此 SqlSessionFactoryBuilder 實(shí)例的最佳作用域是方法作用域(也就是局部方法變量)萝嘁。
SqlSessionFactory
SqlSessionFactory 一旦被創(chuàng)建就應(yīng)該在應(yīng)用的運(yùn)行期間一直存在梆掸,沒有任何理由對它進(jìn)行清除或重建。
因此 SqlSessionFactory 的最佳作用域是應(yīng)用作用域牙言。有很多方法可以做到酸钦,最簡單的就是使用單例模式或者靜態(tài)單例模式。
SqlSession
每個(gè)線程都應(yīng)該有它自己的 SqlSession 實(shí)例咱枉。SqlSession 的實(shí)例不是線程安全的卑硫,因此是不能被共享的徒恋,所以它的最佳的作用域是請求或方法作用域。
動(dòng)態(tài)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>