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;
}