mybatis之关联(2)
MapperAssoDao.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="association.AssoDao"> 7 8 <!--关联:使用对象关联方法--> 9 <select id="selectByAsso2" parameterType="String" resultMap="selectByAsso2"> 10 SELECT u.id uid,u.name uname,u.age uage,s.name sname,s.salary ssalary FROM user u, salary s 11 WHERE u.name = s.name AND u.name = #{name} 12 </select> 13 <resultMap type="association.AssoObj" id="selectByAsso2"> 14 <result property="id" column="uid"/> 15 <result property="name" column="uname"/> 16 <result property="age" column="uage"/> 17 <result property="salary.name" column="sname"/> 18 <result property="salary.salary" column="ssalary"/> 19 </resultMap> 20 21 <!--关联:使用association关联方法--> 22 <select id="selectByAsso3" parameterType="String" resultMap="selectByAsso3"> 23 SELECT u.id uid,u.name uname,u.age uage,s.name sname,s.salary ssalary FROM user u, salary s 24 WHERE u.name = s.name AND u.name = #{name} 25 </select> 26 <resultMap type="association.AssoObj" id="selectByAsso3"> 27 <result property="id" column="uid"/> 28 <result property="name" column="uname"/> 29 <result property="age" column="uage"/> 30 <association property="salary" javaType="bean.Salary"> 31 <id property="name" column="sname"></id> 32 <id property="salary" column="ssalary"></id> 33 </association> 34 </resultMap> 35 36 </mapper>
TestAsso.java
1 package association; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 import org.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.util.List; 14 15 /* 16 * 关联举例 17 * 18 * */ 19 public class TestAsso { 20 String resource = "mybatis-config-assosciation.xml"; 21 SqlSessionFactory sqlSessionFactory = null; 22 SqlSession session = null; 23 24 // 关联:使用对象关联方法 25 @Test 26 public void testSelectByAsso2() { 27 AssoDao dao = session.getMapper(AssoDao.class); 28 List<AssoObj> assoObjs = dao.selectByAsso2("Tom"); 29 System.out.println("查询到AssoObj:" + assoObjs); 30 } 31 32 // 关联:使用association关联方法 33 @Test 34 public void testSelectByAsso3() { 35 AssoDao dao = session.getMapper(AssoDao.class); 36 List<AssoObj> assoObjs = dao.selectByAsso3("Tom"); 37 System.out.println("查询到AssoObj:" + assoObjs); 38 } 39 40 41 @Before 42 public void before() { 43 // System.out.println("Before"); 44 try { 45 InputStream inputStream = Resources.getResourceAsStream(resource); 46 // 创建工厂 47 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 48 // 创建session对象 49 session = sqlSessionFactory.openSession(); 50 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 56 @After 57 public void close() { 58 session.close(); 59 // System.out.println("After"); 60 } 61 62 }
AssoDao.java
1 package association; 2 3 import org.apache.ibatis.annotations.Select; 4 5 import java.util.List; 6 7 public interface AssoDao { 8 public List selectByAsso2(String name); 9 10 public List selectByAsso3(String name); 11 12 }