zoukankan      html  css  js  c++  java
  • mybatis之一对一关联

    MapperAsso.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="bean.User">
     7     <!--一对一关联-->
     8     <select id="selectOneAssoObj" parameterType="int" resultMap="OneAssoObjRM">
     9         select * from user where id = #{id}
    10     </select>
    11     <resultMap type="association.AssoObj" id="OneAssoObjRM">
    12         <result property="id" column="id"/>
    13         <result property="name" column="name"/>
    14         <result property="age" column="age"/>
    15         <association  property="salary" column="name" select="selectOneSalary"/>
    16         <!--一对多关联配置备注-->
    17         <!--<collection property="salarys" ofType="bean.Salary" column="name" select="selectFewSalarys">-->
    18     </resultMap>
    19 
    20     <select id="selectOneSalary" parameterType="String" resultType="bean.Salary">
    21         select * from salary where name = #{name}
    22     </select>
    23 
    24     <!--id:名称; parameterType传入参数类型; resultType:返回数据类型-->
    25     <select id="selectOneUser" parameterType="int" resultType="bean.User">
    26         select * from user where id = #{id}
    27     </select>
    28 
    29     <!--批量查询-->
    30     <select id="selectAllUser" resultMap="userList">
    31         select * from user
    32     </select>
    33     <resultMap type="first.UserBean" id="userList">
    34         <result property="id" column="id"/>
    35         <result property="name" column="name"/>
    36         <result property="age" column="age"/>
    37     </resultMap>
    38 
    39 
    40 </mapper>

    AssoObj.java

     1 package association;
     2 
     3 import bean.Salary;
     4 
     5 /**
     6  * 数据封装类
     7  * user class with Salary object
     8  * 一对一关联Javabean
     9  */
    10 public class AssoObj {
    11     private int id;
    12     private String name;
    13     private String age;
    14     private Salary salary;
    15 
    16     public AssoObj() {
    17 
    18     }
    19 
    20     public AssoObj(String name, String age) {
    21         this.name = name;
    22         this.age = age;
    23     }
    24 
    25     public AssoObj(int id, String name, String age, Salary salary) {
    26         this.id = id;
    27         this.name = name;
    28         this.age = age;
    29         this.salary = salary;
    30     }
    31 
    32     public String getName() {
    33         return name;
    34     }
    35 
    36     public void setName(String name) {
    37         this.name = name;
    38     }
    39 
    40     public String getAge() {
    41         return age;
    42     }
    43 
    44     public void setAge(String age) {
    45         this.age = age;
    46     }
    47 
    48     public int getId() {
    49         return id;
    50     }
    51 
    52     public void setId(int id) {
    53         this.id = id;
    54     }
    55 
    56     public Salary getSalary() {
    57         return salary;
    58     }
    59 
    60     public void setSalary(Salary salary) {
    61         this.salary = salary;
    62     }
    63 
    64     @Override
    65     public String toString() {
    66         return "AssoObj{id:" + id + "; 姓名:" + name +
    67                 "; 年龄:" + age + "; Salary对象:" + salary + "}";
    68     }
    69 }

    TestAsso.java

     1 package association;
     2 
     3 import bean.Salary;
     4 import bean.User;
     5 import bean.UserSalary;
     6 import first.UserBean;
     7 import org.apache.ibatis.io.Resources;
     8 import org.apache.ibatis.session.SqlSession;
     9 import org.apache.ibatis.session.SqlSessionFactory;
    10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    11 import org.junit.After;
    12 import org.junit.Before;
    13 import org.junit.Ignore;
    14 import org.junit.Test;
    15 import zhujie.UserMapperInterface;
    16 
    17 import java.io.IOException;
    18 import java.io.InputStream;
    19 import java.util.List;
    20 
    21 /*
    22 * 一对一关联举例
    23 *
    24 * */
    25 public class TestAsso {
    26     String resource = "mybatis-config-assosciation.xml";
    27     SqlSessionFactory sqlSessionFactory = null;
    28     SqlSession session = null;
    29 
    30     @Before
    31     public void before() {
    32 //        System.out.println("Before");
    33         try {
    34             InputStream inputStream = Resources.getResourceAsStream(resource);
    35 //            创建工厂
    36             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    37 //            创建session对象
    38             session = sqlSessionFactory.openSession();
    39 
    40         } catch (IOException e) {
    41             e.printStackTrace();
    42         }
    43     }
    44 
    45     @After
    46     public void close() {
    47         session.close();
    48 //        System.out.println("After");
    49     }
    50 
    51     //    一对一关联测试用例
    52     @Test
    53     public void testSelectOneAssoObj() {
    54         AssoObj assoObj = session.selectOne("bean.User.selectOneAssoObj", 1);
    55         System.out.println("查询单个AssoObj:" + assoObj);
    56     }
    57 
    58     @Test
    59     public void testSelectOneUser() {
    60         User user = session.selectOne("bean.User.selectOneUser", 1);
    61         System.out.println("查询单个User:" + user);
    62     }
    63 
    64     @Test
    65     public void testSelectAllUser() {
    66         List<User> listUser = session.selectList("bean.User.selectAllUser");
    67         System.out.println("记录个数:" + listUser.size());
    68         System.out.println(listUser);
    69     }
    70 
    71     @Test
    72     public void testSelectOneSalary() {
    73         Salary salary = session.selectOne("bean.User.selectOneSalary", "Tom");
    74         System.out.println("查询单个Salary:" + salary);
    75     }
    76 
    77 
    78 }

    mybatis-config-assosciation.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6     <!--从外部配置文件导入jdbc信息-->
     7     <properties resource="jdbc.properties"></properties>
     8 
     9     <environments default="development">
    10         <environment id="development">
    11             <transactionManager type="JDBC"/>
    12             <dataSource type="POOLED">
    13                 <property name="driver" value="${driver}"/>
    14                 <property name="url" value="${url}"/>
    15                 <property name="username" value="${username}"/>
    16                 <property name="password" value="${password}"/>
    17             </dataSource>
    18         </environment>
    19     </environments>
    20 
    21     <!--指定映射资源文件-->
    22     <mappers>
    23         <mapper resource="association/MapperAsso.xml"/>
    24 
    25     </mappers>
    26 </configuration>
  • 相关阅读:
    MQTT协议探究(三)
    MQTT协议探究(二)
    MQTT协议探究(一)
    MQTT图形化客户端比较
    Ubuntu编写开机启动脚本
    线程阻塞释放的5种方法
    (转)Linux vmstat命令实战详解
    (转)MongoDB 3.0 WT引擎参考配置文件
    (转)mongodb常用命令脚本化-自动化运维
    (转)MongoDB 实现currentOp定时捕获
  • 原文地址:https://www.cnblogs.com/gongxr/p/8288301.html
Copyright © 2011-2022 走看看