zoukankan      html  css  js  c++  java
  • mybatis中的一对多

    前言:创建项目啥的就不说了,直接开始讲怎么从model开始
    我有两个实体类(model):User和City
    
    一对多的关系是,一个User可以去过很多个City。
    
    City的创建如下
    
    说明,按照表的字段来创建类
    package com.tqz.model;
    
    public class City {
        private int city_id;
        private int user_id;
        private String cityname;
        public int getCity_id() {
            return city_id;
        }
        public void setCity_id(int city_id) {
            this.city_id = city_id;
        }
        public int getUser_id() {
            return user_id;
        }
        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }
        public String getCityname() {
            return cityname;
        }
        public void setCityname(String cityname) {
            this.cityname = cityname;
        }
    }
    User的创建如下:
    
    说明:在User类里面,要写一个private List<City> cities;//指向去过的城市
    它是用来存User对应的多个City的
    package com.tqz.model;
    
    import java.util.List;
    
    public class User {
        private int id;
        private String username;
        private int tel;
        private String address;
        private List<City> cities;//指向去过的城市
        public List<City> getCities() {
            return cities;
        }
        public void setCities(List<City> cities) {
            this.cities = cities;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public int getTel() {
            return tel;
        }
        public void setTel(int tel) {
            this.tel = tel;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }
    关键的来了:User.xml文件的写法
    
    <?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.tqz.imapper.IUser">
    
      <resultMap type="User" id="UserBeenToResult">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="tel" jdbcType="INTEGER" property="tel"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <collection property="cities" ofType="City">
            <id column="city_id" jdbcType="INTEGER" property="city_id"/>
            <id column="user_id" jdbcType="INTEGER" property="user_id"/>
            <result column="cityname" jdbcType="VARCHAR" property="cityname"/>
        </collection>
      </resultMap>
    
      <select id="getUserBeenToCity" resultMap="UserBeenToResult" 
                                                      parameterType="String">
        SELECT u.id,u.username,u.tel,u.address,
        c.city_id,c.user_id,c.cityname
        FROM user u 
        inner join city c
        ON c.user_id=u.id
        WHERE u.username=#{name}<!-- 写好sql语句很关键 -->
      </select>
    
    </mapper>
    说明:用resultMap + collection来处理一堆多的关系
    用法:在resultMap标签里面再添加collection标签
    实现:resultMap的type是User,表示查询最终的结果是User对象(即一个)
    collection的ofType是City,表示要整理出的集合是City对象(即多个)
    collection的property的值是cities,这个是Uesr的属性
    collection下面的内容就是封装对象的操作了
    collection操作结束后的结果是一个集合,赋给User的cities
    
    重点:select里面的sql语句
    SELECT u.id, u.username, u.tel, u.address,
        c.city_id, c.user_id, c.cityname
    FROM user u 
    inner join city c
    ON c.user_id=u.id
    WHERE u.username=#{name}
    因为是关联两个表,所以要在查询的字段前面加上字段的标识符【 u.id | c.city_id】
    inner join city c表示关联city这个表c是给他起的别名就像u一样
    ON c.user_id=u.id
    WHERE u.username=#{name}是关联条件
    条件:user_id是存在表city里面的
    id是存在user里面的
    表city里面可以存在多个相同的user_id
    c.user_id=u.id就表示在表city里面找与user相同的id(这个id可以有多个,即:一对 多)
    
    
    测试
    public class Demo {
        private static SqlSessionFactory sqlSessionFactory;
        private static Reader reader;
        private static SqlSession session;
        static {
            try {
                reader = Resources.getResourceAsReader("com/tqz/config/Configuration.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static SqlSessionFactory getSession() {
            return sqlSessionFactory;
        }
            public static void testgetUserBeenToCity(String name) {
            //获取连接
             session = sqlSessionFactory.openSession();
             //获取Mapper
             IUser iUser = session.getMapper(IUser.class);
             User user = new User();
             user = iUser.getUserBeenToCity(name);
             System.out.println("用户id为:" + user.getId());
             System.out.println("用户名为:" + user.getUsername());
             System.out.println("用户tel为:" + user.getTel());
             System.out.println("用户address为:" + user.getAddress());
             System.out.println("*********************************");
             System.out.println("去过的城市:");
             for (City city : user.getCities()) {
                 System.out.println(city.getCityname());
             }
         }
        public static void main(String[] args) {
            
            try {
                //一对多:一个人可以去过很多城市
                testgetUserBeenToCity("田钦政");
            } finally {
                session.close();
            }
        }
    }
    可以看出,查询一个用户去过的城市,结果出来很多个他去过的城市。
    
    源代码放在我的GitHub上 下载
  • 相关阅读:
    node.js中用户密码的加密
    linux下高可用LVS搭建及配置方法
    在Centos 5.x或6.x上安装RHEL EPEL Repo
    shell脚本 expect 实现自动登陆
    platform 收集linux/windows操作系统信息
    市面上比较流行的图表展示
    django作业2
    django实现分片上传文件
    linux下大于2T硬盘格式化方法
    django之第二天
  • 原文地址:https://www.cnblogs.com/xp20170618/p/14838503.html
Copyright © 2011-2022 走看看