MyBatis基本入門篇

No.1 基礎

框架核心

一、 mybatis配置文件,包括Mybatis全局配置文件和Mybatis映射文件,其中全局配置文件配置了數據源、事務等信息;映射文件配置了SQL執行相關的 信息。java

二、 mybatis經過讀取配置文件信息(全局配置文件和映射文件),構造出SqlSessionFactory,即會話工廠。mysql

三、 經過SqlSessionFactory,能夠建立SqlSession即會話。Mybatis是經過SqlSession來操做數據庫的。git

四、 SqlSession自己不能直接操做數據庫,它是經過底層的Executor執行器接口來操做數據庫的。Executor接口有兩個實現類,一個是普通執行器,一個是緩存執行器(默認)。github

五、 Executor執行器要處理的SQL信息是封裝到一個底層對象MappedStatement中。該對象包括:SQL語句、輸入參數映射信息、輸出結果集映射信息。其中輸入參數和輸出結果的映射類型包括HashMap集合對象、POJO對象類型。web

MyBatis 入門

引入 jarsql

mybatis-3.2.7.jar數據庫

mysql-connector-java-5.1.7-bin.jarapache

建立實體緩存

public class User {
    private int id;
    private String name;
    private String dept;
    private String phone;
    private String website;
    // 省略 get set 方法
}

建立配置session

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 配置別名 -->
    <typeAliases>
        <typeAlias alias="User" type="com.kernel.bean.User" />
    </typeAliases>

    <!-- 配置環境 -->
    <environments default="development">
        <environment id="development">
            <!-- 事務管理類型 -->
            <transactionManager type="JDBC" />
            <!--
                數據源類型:
                    UNPOOLED:爲每一個數據操做建立一個鏈接
                    POOLED:建立一個數據庫鏈接池,鏈接池中的每一個鏈接將被用於數據庫操做,一旦數據操做完成,MyBatis將鏈接返回給鏈接池
                    JNDI:向配置好的JNDI數據源獲取數據庫鏈接,生產環境有限考慮此方式
            -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>

    <!-- 配置映射文件 -->
    <mappers>
        <mapper resource="com/kernel/bean/User.xml" />
    </mappers>
</configuration>

建立映射

<?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.kernel.bean">
    <select id="getUserById" parameterType="int" resultType="User">
        select * from user where id = #{id}
    </select>
</mapper>

測試

public class Main {
    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSession sqlSession;
    private static Reader reader;
    static {
        try {
            reader = Resources.getResourceAsReader("config/mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void test {
        User user = sqlSession.selectOne("getUserById", 1);
        System.out.println(user);
    }
}

CURD

建立實體(同上)

建立 IUser

public interface IUser {
    public List<User> getUserList();

    public void insertUser(User user);

    public void updateUser(User user);

    public void deleteUser(int userId);

    public User getUser(int id);   
}

建立配置(同上)

建立映射

<?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.kernel.dao.IUser">

    <select id="insertUser" parameterType="User">
        insert into user (name, dept, website, phone)values (#{name}, #{dept}, #{website}, #{phone})
    </select>
    <select id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </select>
    <select id="updateUser" parameterType="User">
        update user set name = #{name}, dept = #{dept}, website = #{website}, phone = #{phone} where id = #{id}
    </select>
    <select id="getUser" parameterType="int" resultType="User">
        select * from user where id = #{id}
    </select>
    <select id="getUserList" resultType="User">
        select * from user
    </select>
</mapper>

No.2 關聯查詢

一對多

建立實體

User

public class User implements Serializable {
    private int id;
    private String username;
    private String mobile;
    private List<Post> posts;
    // 省略 get 和 set
    // 重寫 toString
}

Post

public class Post implements Serializable {
    private int id;
    private User user;
    private String title;
    private String content;
    // 省略 get 和 set
    // 重寫 toString
}

建立配置(同上)

建立映射

<?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.kernel.dao.IUser">
    <resultMap id="resultUserMap" type="User">
        <result property="id" column="user_id"/>
        <result property="username" column="username"/>
        <result property="mobile" column="mobile"/>
        <collection property="posts" ofType="Post" column="userid">
            <id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/>
            <result property="title" column="title" javaType="string" jdbcType="VARCHAR"/>
            <result property="content" column="content" javaType="string" jdbcType="VARCHAR"/>
        </collection>

    </resultMap>
    <select id="getUser" parameterType="int" resultMap="resultUserMap">
        select u.*,p.* from user u, post p where u.id = p.userid and u.id = #{id}
    </select>
</mapper>

多對一

建立實體(同上)

建立配置(同上)

建立映射

<?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.kernel.dao">
    <resultMap id="resultPostsMap" type="Post">
        <result property="id" column="post_id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <association property="user" javaType="User">
            <id property="id" column="userid"/>
            <result property="username" column="username"/>
            <result property="mobile" column="mobile"/>
        </association>

    </resultMap>
    <select id="getPosts" parameterType="int" resultMap="resultPostsMap">
        select u.*,p.* from user u, post p where u.id = p.userid and p.post_id = #{id}
    </select>
</mapper>

多對多

建立實體

OrderItem

public class OrderItem {
    private int id;
    private int number;
    private Order order;
    private Product product;
    // 省略 get 和 set
}

Order

public class Order {
    private int id;
    private String code;
    List<OrderItem> orderItems;
    // 省略 get 和 set
}

建立映射

OrderItem.xml

<?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.kernel.pojo">

    <insert id="addOrderItem" parameterType="OrderItem">
            insert into order_item_
                values(null,#{order.id},#{product.id},#{number})
        </insert>
    <insert id="deleteOrderItem" parameterType="OrderItem">
            delete from order_item_
                where oid = #{order.id} and pid = #{product.id}
        </insert>
</mapper>

Order.xml

<?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.kernel.pojo">
    <resultMap type="Order" id="orderBean">
        <id column="oid" property="id" />
        <result column="code" property="code" />

        <collection property="orderItems" ofType="OrderItem">
            <id column="oiid" property="id" />
            <result column="number" property="number" />
            <association property="product" javaType="Product">
                <id column="pid" property="id"/>
                <result column="pname" property="name"/>
                <result column="price" property="price"/>
            </association>
        </collection>
    </resultMap>

    <select id="listOrder" resultMap="orderBean">
            select o.*,p.*,oi.*, o.id 'oid', p.id 'pid', oi.id 'oiid', p.name 'pname'
                from order_ o
                left join order_item_ oi    on o.id =oi.oid
                left join product_ p on p.id = oi.pid
        </select>

    <select id="getOrder" resultMap="orderBean">
            select o.*,p.*,oi.*, o.id 'oid', p.id 'pid', oi.id 'oiid', p.name 'pname'
                from order_ o
                left join order_item_ oi on o.id =oi.oid
                left join product_ p on p.id = oi.pid
            where o.id = #{id}
        </select>
</mapper>

Product.xml

<?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.kernel.pojo">
    <resultMap type="Product" id="productBean">
        <id column="pid" property="id" />
        <result column="pname" property="name" />
        <result column="price" property="price" />

        <!-- 多對一的關係 -->
        <!-- property: 指的是屬性名稱, javaType:指的是屬性的類型 -->
        <association property="category" javaType="Category">
            <id column="cid" property="id"/>
            <result column="cname" property="name"/>
        </association>
    </resultMap>

    <select id="listProduct" resultMap="productBean">
            select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname'
                from category_ c
                left join product_ p on c.id = p.cid
        </select>
    <select id="getProduct" resultMap="productBean">
            select c.*, p.*, c.id 'cid', p.id 'pid', c.name 'cname', p.name 'pname'
                from category_ c
                left join product_ p on c.id = p.cid
            where p.id = #{id}
        </select>
</mapper>

No.3 動態 SQL

if

若是沒有傳參數 name,那麼就查詢全部,若是有 name 參數,那麼就進行模糊查詢。

<select id="listProduct" resultType="Product">
    select * from product_
    <if test="name!=null">
        where name like concat('%',#{name},'%')
    </if>           
</select>

where

若是任何條件都不成立,那麼在 sql 中不會出現 where,若是有一個條件成立,則 sql 中會去掉 and、or。

<select id="listProduct" resultType="Product">
    select * from product
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>
    </where>
</select>

set

效果與 where 相似,用於 update

<update id="updateProduct" parameterType="Product" >
    update product_
    <set>
        <if test="name != null">name=#{name},</if>
        <if test="price != null">price=#{price}</if>
    </set>
    where id=#{id}   
</update>

trim

自定義標籤

<select id="listProduct" resultType="Product">
    select * from product_
    <trim prefix="where" prefixOverrides="and">
        <if test="name!=null">
            and name like concat('%', #{name}, '%')
        </if>
        <if test="price != null and price !=0">
            and price > #{price}
        </if>
    </trim>
</select>

choose

實現 if/else 的效果

<select id="listProduct" resultType="Product">
    select * from product_
    <where>
        <choose>
            <when test="name != null">
                and name like concat('%', #{name}, '%')
            </when>
            <when test="price != null and price >0">
                and price > #{price}
            </when>
            <otherwise>
                and id = 1
            </otherwise>
        </choose>
    </where>
</select>

foreach

<select id="listProduct" resultType="Product">
    select * from product_ where id in
    <foreach item="item" index="index" collection="list"
             open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

bind

<select id="listProduct" resultType="Product">
    <bind name="likeName" value="'%' + name + '%'" />
    select * from product_ where name like #{likeName}
</select>

No.4 settings

設置參數 描述 有效值 默認值
cacheEnabled 全局地開啓或關閉配置文件中的全部映射器已經配置的任何緩存。 true | false true
lazyLoadingEnabled 延遲加載的全局開關。當開啓時,全部關聯對象都會延遲加載。 特定關聯關係中可經過設置fetchType屬性來覆蓋該項的開關狀態。 true | false false
aggressiveLazyLoading 當開啓時,任何方法的調用都會加載該對象的全部屬性。不然,每一個屬性會按需加載(參考lazyLoadTriggerMethods). true | false false (true in ≤3.4.1)
multipleResultSetsEnabled 是否容許單一語句返回多結果集(須要兼容驅動)。 true | false true
useColumnLabel 使用列標籤代替列名。不一樣的驅動在這方面會有不一樣的表現, 具體可參考相關驅動文檔或經過測試這兩種不一樣的模式來觀察所用驅動的結果。 true | false true
useGeneratedKeys 容許 JDBC 支持自動生成主鍵,須要驅動兼容。 若是設置爲 true 則這個設置強制使用自動生成主鍵,儘管一些驅動不能兼容但仍可正常工做(好比 Derby)。 true | false False
autoMappingBehavior 指定 MyBatis 應如何自動映射列到字段或屬性。 NONE 表示取消自動映射;PARTIAL 只會自動映射沒有定義嵌套結果集映射的結果集。 FULL 會自動映射任意複雜的結果集(不管是否嵌套)。 NONE, PARTIAL, FULL PARTIAL
autoMappingUnknownColumnBehavior 指定發現自動映射目標未知列(或者未知屬性類型)的行爲。NONE: 不作任何反應WARNING: 輸出提醒日誌 ('org.apache.ibatis.session.AutoMappingUnknownColumnBehavior'的日誌等級必須設置爲 WARN)FAILING: 映射失敗 (拋出 SqlSessionException) NONE, WARNING, FAILING NONE
defaultExecutorType 配置默認的執行器。SIMPLE 就是普通的執行器;REUSE 執行器會重用預處理語句(prepared statements); BATCH 執行器將重用語句並執行批量更新。 SIMPLE REUSE BATCH SIMPLE
defaultStatementTimeout 設置超時時間,它決定驅動等待數據庫響應的秒數。 任意正整數 Not Set (null)
defaultFetchSize 爲驅動的結果集獲取數量(fetchSize)設置一個提示值。此參數只能夠在查詢設置中被覆蓋。 任意正整數 Not Set (null)
safeRowBoundsEnabled 容許在嵌套語句中使用分頁(RowBounds)。若是容許使用則設置爲false。 true | false False
safeResultHandlerEnabled 容許在嵌套語句中使用分頁(ResultHandler)。若是容許使用則設置爲false。 true | false True
mapUnderscoreToCamelCase 是否開啓自動駝峯命名規則(camel case)映射,即從經典數據庫列名 A_COLUMN 到經典 Java 屬性名 aColumn 的相似映射。 true | false False
localCacheScope MyBatis 利用本地緩存機制(Local Cache)防止循環引用(circular references)和加速重複嵌套查詢。 默認值爲 SESSION,這種狀況下會緩存一個會話中執行的全部查詢。 若設置值爲 STATEMENT,本地會話僅用在語句執行上,對相同 SqlSession 的不一樣調用將不會共享數據。 SESSION | STATEMENT SESSION
jdbcTypeForNull 當沒有爲參數提供特定的 JDBC 類型時,爲空值指定 JDBC 類型。 某些驅動須要指定列的 JDBC 類型,多數狀況直接用通常類型便可,好比 NULL、VARCHAR 或 OTHER。 JdbcType 常量. 大多都爲: NULL, VARCHAR and OTHER OTHER
lazyLoadTriggerMethods 指定哪一個對象的方法觸發一次延遲加載。 用逗號分隔的方法列表。 equals,clone,hashCode,toString
defaultScriptingLanguage 指定動態 SQL 生成的默認語言。 一個類型別名或徹底限定類名。 org.apache.ibatis.scripting.xmltags.XMLLanguageDriver
defaultEnumTypeHandler 指定 Enum 使用的默認 TypeHandler 。 (從3.4.5開始) 一個類型別名或徹底限定類名。 org.apache.ibatis.type.EnumTypeHandler
callSettersOnNulls 指定當結果集中值爲 null 的時候是否調用映射對象的 setter(map 對象時爲 put)方法,這對於有 Map.keySet() 依賴或 null 值初始化的時候是有用的。注意基本類型(int、boolean等)是不能設置成 null 的。 true | false false
returnInstanceForEmptyRow 當返回行的全部列都是空時,MyBatis默認返回null。 當開啓這個設置時,MyBatis會返回一個空實例。 請注意,它也適用於嵌套的結果集 (i.e. collectioin and association)。(從3.4.2開始) true | false false
logPrefix 指定 MyBatis 增長到日誌名稱的前綴。 任何字符串 Not set
logImpl 指定 MyBatis 所用日誌的具體實現,未指定時將自動查找。 SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING Not set
proxyFactory 指定 Mybatis 建立具備延遲加載能力的對象所用到的代理工具。 CGLIB | JAVASSIST JAVASSIST (MyBatis 3.3 or above)
vfsImpl 指定VFS的實現 自定義VFS的實現的類全限定名,以逗號分隔。 Not set
useActualParamName 容許使用方法簽名中的名稱做爲語句參數名稱。 爲了使用該特性,你的工程必須採用Java 8編譯,而且加上-parameters選項。(從3.4.1開始) true | false true
configurationFactory 指定一個提供Configuration實例的類。 這個被返回的Configuration實例用來加載被反序列化對象的懶加載屬性值。 這個類必須包含一個簽名方法static Configuration getConfiguration(). (從 3.2.3 版本開始)

No.5 註解

建立接口,使用 @Insert()、@Update()、@Select()、@Delete() 替代 xml 的語句

在配置文件中增長 <mapper class=「com.kernel.mapper.CategoryMapper"/>

一對多

@Results 經過 @Result 和 @Many 中調用多端方法相結合,來獲取一對多關係

CategoryMapper

public interface CategoryMapper {
    @Select("select * from category_")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "products", 
                    javaType = List.class, 
                    column = "id", 
                    many = @Many(select = "com.kernel.pojo.ProductMapper.listByCategory"))
    })
    public List<Category> list();
}

ProductMapper

public interface ProductMapper {
    @Select(" select * from product_ where cid = #{cid}")
    public List<Product> listByCategory(int cid);
}

多對一

ProductMapper

@Select("select * from product_")
@Results(
    @Result(property = "category", column = "id", one = @One(select = "com.kernel.pojo.CategoryMapper.get"))
)
public List<Product> list();

CategoryMapper

@Select("select * from category_ where id = #{id}")
public Category get(int id);

多對多

ProductMapper

public interface ProductMapper {
    @Select("select * from product_ where id = #{id}")
    public Product get(int id);
}

OrderItemMapper

public interface OrderItemMapper {
    @Select("select * from order_item_ where oid = #{oid}")
    @Results({
            @Result(property = "product", column = "pid", one = @One(select = "com.kernel.pojo.ProductMapper.get"))
    })
    public List<OrderItem> listByOrder(int oid);
}

OrderMapper

public interface OrderMapper {
    @Select("select * from order_")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderItems", javaType = List.class, column = "id",
                    many = @Many(select = "com.kernel.pojo.OrderItemMapper.listByOrder"))
    })
    public List<Order> list();
}

動態 SQL

關於 sql 類

private String selectPersonSql() {
  return new SQL() {{
    SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");
    SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");
    FROM("PERSON P");
    FROM("ACCOUNT A");
    INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");
    INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");
    WHERE("P.ID = A.ID");
    WHERE("P.FIRST_NAME like ?");
    OR();
    WHERE("P.LAST_NAME like ?");
    GROUP_BY("P.ID");
    HAVING("P.LAST_NAME like ?");
    OR();
    HAVING("P.FIRST_NAME like ?");
    ORDER_BY("P.ID");
    ORDER_BY("P.FULL_NAME");
  }}.toString();
}

使用 SQL 類的方式構建

public class CategsoryDynaSqlProvider {
    public String list() {
        return new SQL()
                .SELECT("*")
                .FROM("category_")
                .toString();

    }
    public String get() {
        return new SQL()
                .SELECT("*")
                .FROM("category_")
                .WHERE("id=#{id}")
                .toString();
    }

    public String add(){
        return new SQL()
                .INSERT_INTO("category_")
                .VALUES("name", "#{name}")
                .toString();
    }
    public String update(){
        return new SQL()
                .UPDATE("category_")
                .SET("name=#{name}")
                .WHERE("id=#{id}")
                .toString();
    }
    public String delete(){
        return new SQL()
                .DELETE_FROM("category_")
                .WHERE("id=#{id}")
                .toString();
    }
}

註解

手寫sql語句
@Insert(" insert into category_ ( name ) values (#{name}) ")  
public int add(Category category);  
使用sql類
@InsertProvider(type=CategoryDynaSqlProvider.class,method="add")  
public int add(Category category);

No.6 延遲加載

MyBatis 默認是積極加載的,開啓日誌,一對多查詢,走起

當我查詢商品分類和商品時,控制檯發送的sql以下:

DEBUG [main] - ==>  Preparing: select * from category_ 
DEBUG [main] - ==> Parameters: 
TRACE [main] - <==    Columns: id, name
TRACE [main] - <==        Row: 1, category1
TRACE [main] - <==        Row: 2, category2
DEBUG [main] - <==      Total: 2
category1
DEBUG [main] - ==>  Preparing: select * from product_ where cid = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <==    Columns: id, name, price, cid
TRACE [main] - <==        Row: 1, product a, 88.88, 1
TRACE [main] - <==        Row: 2, product b, 88.88, 1
TRACE [main] - <==        Row: 3, product c, 88.88, 1
DEBUG [main] - <==      Total: 3
product a
product b
product c
category2
DEBUG [main] - ==>  Preparing: select * from product_ where cid = ? 
DEBUG [main] - ==> Parameters: 2(Integer)
TRACE [main] - <==    Columns: id, name, price, cid
TRACE [main] - <==        Row: 4, product x, 88.88, 2
TRACE [main] - <==        Row: 5, product y, 88.88, 2
TRACE [main] - <==        Row: 6, product z, 88.88, 2
DEBUG [main] - <==      Total: 3

當我只查詢商品分類時,發送的 sql 仍是三條,因此 MyBatis 是默認開啓積極加載的

開啓延遲加載

<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

再次查詢,當我須要使用商品時,才發送對應的 sql

No.7 緩存

一級緩存

同一個 session 中查詢相同的記錄,只查詢一次,不一樣 session,發送屢次,因此一級緩存是 session 級別的。

二級緩存

二級緩存是 sessionFactory 的緩存

<settings> 
    <!-- 打開延遲加載的開關 --> 
    <setting name="lazyLoadingEnabled" value="true" /> 
    <!-- 將積極加載改成消息加載即按需加載 --> 
    <setting name="aggressiveLazyLoading" value="false"/>
    <setting name="cacheEnabled" value="true"/>
</settings>

將映射文件中 sql 包含在 cache 標籤中

<mapper namespace="com.how2java.pojo">
    <cache/>
    <insert id="addCategory" parameterType="Category" >
        insert into category_ ( name ) values (#{name})   
    </insert>
    <delete id="deleteCategory" parameterType="Category" >
        delete from category_ where id= #{id}  
    </delete>

    <select id="getCategory" parameterType="_int" resultType="Category">
        select * from   category_  where id= #{id}   
    </select>

    <update id="updateCategory" parameterType="Category" >
        update category_ set name=#{name} where id=#{id}   
    </update>
    <select id="listCategory" resultType="Category">
        select * from   category_
        <if test="start!=null and count!=null">
            limit #{start},#{count}
        </if>
    </select>    
</mapper>

No.8 逆向工程

Mybatis Generator是一個用於Mybatis逆向工程的工具,經過表建立 pojo、mapper

導包 mysql-connector-java-5.0.8-bin.jar

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">

        <commentGenerator>
            <!--去掉生成日期那行註釋-->
            <property name="suppressDate" value="true"/>
            <!--是否去除自動生成的註釋-->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!--數據庫鏈接信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///mybatis" userId="root" password="123456"/>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成pojo-->
        <javaModelGenerator targetPackage="com.kernel.pojo" targetProject="src">
            <!--是否讓schema做爲包的後綴-->
            <property name="enableSubPackages" value="true"/>
            <!--從數據庫返回的值被清理先後的空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成mapper-->
        <sqlMapGenerator targetPackage="com.kernel.pojo" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成dao-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.kernel.mapper" targetProject="src">
            <!--是否讓schema做爲包的後綴-->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--
            enableCountByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢總條數語句(用於分頁的總條數查詢)
            enableUpdateByExample(默認true):MyBatis3Simple爲false,指定是否生成動態修改語句(只修改對象中不爲空的屬性)
            enableDeleteByExample(默認true):MyBatis3Simple爲false,指定是否生成動態刪除語句;
            enableSelectByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢語句;
        -->
        <table tableName="category_" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByPrimaryKeyQueryId="false">
            <!--忽略keyColumn、keyProperty和useGeneratedKeys三個屬性的生成-->
            <property name="my.isgen.usekeys" value="true"/>
            <!--使用自動增加主鍵-->
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

測試

public class Test {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        InputStream is= Test.class.getClassLoader().getResource("generatorConfig.xml").openStream();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        is.close();
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("生成成功");
    }
}

No.9 其餘

日誌

在 src 下建立 log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.how2java=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

MyBatis 會自動打印出sql語句

分頁

xml方式

<select id="listCategory" resultType="Category">
   select * from   category_
   <if test="start!=null and count!=null">
      limit #{start},#{count}
   </if>
</select>

註解方式

@Select("select * from category_ limit #{start},#{count}")
public List<Category> listByPage(@Param("start") int start, @Param("count") int count);

事務管理

將事務提交方式設置爲 jdbc 後,將採用 jdbc 的方式提交事務,加入有一條記錄失敗,自動回滾,注意,數據表的類型必須是 INNODB

PageHelper

PageHelper 是一款 Mybatis 分頁插件。

加入jar

pagehelper-5.1.0-beta2.jar、jsqlparser-1.0.jar

配置插件

<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>

分頁查詢只須要在執行查詢以前,執行

PageHelper.offsetPage(0, 5);

獲取總數

PageInfo page = new PageInfo<>(list);

相關文章
相關標籤/搜索