位置:在接口对应的映射文件里面配置
1.对多 1)关联表的slq需要自己写 2)配置 1)collection a)property:一的一方中保存多一方的集合名称 b)ofType:多的一方的类型 2.对一 1)关联表的sql需要自己写 2)配置 1)association a)property:多的一方中保存一的方的属性名称 b)javaType:一的一方的类型 3.细节问题 1)关联查询的时候两种表的id都一样要取别名 2)在做批量插入的时候不要在循环里面访问数据库
例子:
0.数据库
1.创建项目
2.建包建配置文件
com.qf.entity
com.qf.dao
com.qf.mapper
com.qf.utils
mybatis-config.xml
3.con.qf.entity包中创建实体类
package com.pxf.entity; import java.util.ArrayList; import java.util.List; public class Customer { private Integer id; private String name; private List<OrderInfo> orderInfos; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<OrderInfo> getOrderInfos() { return orderInfos; } public void setOrderInfos(List<OrderInfo> orderInfos) { this.orderInfos = orderInfos; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", orderInfos=" + orderInfos + "]"; } }
package com.pxf.entity; public class OrderInfo { private Integer id; private String orderName; private Customer customer; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Override public String toString() { return "OrderInfo [id=" + id + ", orderName=" + orderName + ", customer=" + customer + "]"; } }
4.con.qf.dao包中创建接口
package com.pxf.dao; import com.pxf.entity.Customer; public interface ICustomerDao { public int add(Customer customer); public Customer getCustomerById(Integer id); }
package com.pxf.dao; import java.util.List; import com.pxf.entity.OrderInfo; public interface IOrderInfoDao { public int add(List<OrderInfo> orderInfos); public OrderInfo getOrderInfoById(Integer id); }
5.配置jdbc.properties配置文件
jdbc.url =jdbc:mysql://localhost:3306/1708_mybatis jdbc.name=root jdbc.password=root jdbc.driver=com.mysql.jdbc.Driver
配置mybatis-config.xml配置文件
<?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> <!-- 加载属性文件 resource:本地资源文件 --> <properties resource="jdbc.properties"></properties> <typeAliases> <package name="com.pxf.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.name}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> </configuration>
6.com.qf.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.pxf.dao.ICustomerDao"> <resultMap type="customer" id="customerMap"> <id column="cid" property="id" /> <result column="name" property="name" /> <!-- 配置一对多关系 --> <!-- property:一的一方中保存多一方的集合名称 ofType:多的一方的类型 --> <collection property="orderInfos" ofType="orderInfo"> <id column="oid" property="id" /> <result column="order_name" property="orderName" /> <!-- 配置多对一的关系 --> <!-- property:多的一方中保存一的方的属性名称 javaType:一的一方的类型 --> <association property="customer" javaType="customer"> <id column="cid" property="id"/> <result column="name" property="name"/> </association> </collection> </resultMap> <insert id="add" parameterType="customer" useGeneratedKeys="true" keyProperty="id"> insert into t_customer(name) values(#{name}) </insert> <select id="getCustomerById" resultMap="customerMap"> SELECT c.id AS cid, c. NAME, o.id AS oid, o.order_name FROM t_customer c LEFT JOIN t_order o ON (c.id = o.customer_id) WHERE c.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.pxf.dao.IOrderInfoDao"> <insert id="add" parameterType="orderInfo"> insert into t_order(order_name,customer_id) values <foreach collection="list" item="o" separator=","> (#{o.orderName},#{o.customer.id}) </foreach> </insert> </mapper>
7.在mybatis-config.xml里面读取Mapper文件
<!-- Mapper文件 --> <mappers> <mapper resource="com/pxf/mapper/ICustomerDao.xml" /> <mapper resource="com/pxf/mapper/IOrderInfoDao.xml" /> </mappers>
8.com.qf.util工具类
package com.pxf.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtils { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() { if (sqlSessionFactory == null) { initSqlSessionFactory(); } return sqlSessionFactory; } private static void initSqlSessionFactory() { InputStream inputStream = null; try { // 1.指定配置文件 inputStream = Resources.getResourceAsStream("mybatis-config.xml"); // 2.赋值操作o synchronized (SqlSessionFactoryUtils.class) { if (sqlSessionFactory == null) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } } } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
9.测试类
package com.qf.dao; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import com.pxf.dao.ICustomerDao; import com.pxf.dao.IOrderInfoDao; import com.pxf.entity.Customer; import com.pxf.entity.OrderInfo; import com.pxf.utils.SqlSessionFactoryUtils; public class CustomerDaoTest { @Test public void testCustomerAdd() { SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); ICustomerDao customerDao = sqlSession.getMapper(ICustomerDao.class); IOrderInfoDao orderInfoDao = sqlSession.getMapper(IOrderInfoDao.class); Customer customer =new Customer(); customer.setName("李相爱"); OrderInfo orderInfo =new OrderInfo(); orderInfo.setOrderName("订单A"); OrderInfo orderInfo2 =new OrderInfo(); orderInfo.setOrderName("订单B"); customerDao.add(customer); orderInfo.setCustomer(customer); orderInfo.setCustomer(customer); List<OrderInfo> orderInfos=new ArrayList<OrderInfo>(); orderInfos.add(orderInfo); orderInfos.add(orderInfo2); orderInfoDao.add(orderInfos); sqlSession.commit(); sqlSession.close(); } @Test public void testGetCustomerById() { SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); ICustomerDao customerDao = sqlSession.getMapper(ICustomerDao.class); Customer customer = customerDao.getCustomerById(3); System.out.println(customer); sqlSession.commit(); sqlSession.close(); } }