MyBatis(二)mapper 代理

1.association & collection

比如同時有A.java和B.java兩個類,A.java如下:
public class A{
    private B b1;
    private List<B> b2;
}
在映射b1屬性時用association標籤, 映射b2時用collection標籤,分別是一對一,一對多的關係

<有代理的步驟>

1.mybatis-config.xml

typeAliases標籤:爲實體類起別名

<?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 type="com.entity.Bills" alias="Bills"/>
        <typeAlias type="com.entity.BillType" alias="BillType"/>
        <typeAlias type="com.page.PageUtil" alias="PageUtil"/>
    </typeAliases>

    <!--環境配置:默認開發模式-->
    <environments default="development">
        <environment id="development">
            <!--JDBC進行事務管理-->
            <transactionManager type="JDBC"/>
            <!--數據源配置:底層連接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/bills?characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>
    <!--註冊映射 XML 文件-->
    <mappers>
        <mapper resource="com/mapper/BillsMapper.xml"/>
        <mapper resource="com/mapper/BillTypeMapper.xml"/>
    </mappers>

</configuration>

2.配置 Util

package com.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by cheng on 02/09/2017.
 */
public class practice1 {
    private static SqlSessionFactory sqlSessionFactory = null;

    static {
        try {
            InputStream is = Resources.getResourceAsStream("com/entity/mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("配置解析問題");
        }
    }

    public static SqlSession getSqlSession(Boolean isCommit){
        return sqlSessionFactory.openSession(isCommit);
    }
}

3.Mapper包下的接口–與接口同名的 XML

<!--有代理:namespace必須是映射接口的全路徑-->
<mapper namespace="com.mapper.BillsMapper">

    <select id="findAll" resultMap="findBillsMap" parameterType="PageUtil">
        SELECT b.*,t.name from bills b,bill_type t
        WHERE b.type_id=t.id
        limit #{startrow},#{pagesize}
    </select>

    <resultMap type="Bills" id="findBillsMap">
    <!--多方主鍵列的配置,property 實體類屬性名,column 查詢顯示的列名 or 別名-->
    <id property="id" column="ID"/>
   <!--多方普通字段的配置-->
    <result property="title" column="TITLE"/>
    <result property="bill_time" column="BILL_TIME"/>
    <result property="explain" column="EXPLAIN"/>
    <result property="price" column="PRICE"/>
    <!--多對一關聯:外鍵列引入一方對象-->
    <association property="billtype" javaType="BillType" column="type_id">
        <id property="id" column="TYPE_ID"/>
        <result property="name" column="NAME"/>
    </association>
</resultMap>

4.IMP

public List<Bills> findAll(PageUtil pageutil) {
    // 1.獲取sqlsession對象
    SqlSession sqlSession= MyBatisUtil.getSqlSession(false);
    // 2.獲取映射接口的代理對象
    BillsMapper mapper=sqlSession.getMapper(BillsMapper.class);
    // 3.代理對象.方法
    List<Bills> list=mapper.findAll(pageutil);
    // 4.釋放資源
    sqlSession.close();
    return list;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章