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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值