我是靠谱客的博主 舒服泥猴桃,最近开发中收集的这篇文章主要介绍Mybatis一对多实战,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


左表为user,右表为orders,一对多的关系

实体类

package cn.itcast.mybatis.pojo;

import java.util.Date;
import java.util.List;

public class User{
	private int id;
	private String username;
	private String sex;
	private Date birthday;
	private String address;
	private List<Orders> orders;//此处存的为多的一方的集合

}
package cn.itcast.mybatis.pojo;

import java.util.Date;

public class Orders {
    private int id;
    private int user_id;
    private String number;
    private Date createtime;
    private String note;

}

user对应的dao接口

package cn.itcast.mybatis.mapper;

import cn.itcast.mybatis.pojo.User;

import java.util.List;

public interface UserMapper {

	public List<User> testOneToMany();
}

user对应的mapper.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="cn.itcast.mybatis.mapper.UserMapper">

    <resultMap id="onToMany" type="user">
        <id column="id" property="id"></id>
        <result column="username" property="username"/>
        <result column="address" property="address"/>
        <collection property="orders" ofType="orders">
            <id column="oid" property="id"></id>
            <result column="number" property="number"></result>
            <result column="createtime" property="createtime"></result>
            <result column="user_id" property="user_id"></result>
        </collection>
    </resultMap>
    <select id="testOneToMany" resultMap="onToMany">
        select u.id,u.username,u.address,o.id oid,o.user_id,o.number,o.createtime from user u left join orders o on u.id = o.user_id
    </select> 
</mapper>

测试类

package cn.itcast.mybatis.quickstart;

import cn.itcast.mybatis.mapper.UserMapper;
import cn.itcast.mybatis.pojo.User;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * mybatis入门程序
 * @author Ming Wang
 *
 */
public class QuickStartDemo {
	
	/**
	 * 获取mybatis回话SqlSession
	 * @return
	 * @throws IOException
	 */
	private SqlSession getSqlSession() throws IOException {
		// 1、创建SqlSessionFactoryBuilder加载mybatis的核心配置文件
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
		// 2、获取回话工厂SqlSessionFactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
		// 3、获取回话SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
	}

    @Test
	public void testOneToMany() throws IOException {
		SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.testOneToMany();
        System.out.println(users);
        sqlSession.close();
    }
	
}

得到的结果-->第二行和第三行为user对应的的二个orders

[User{id=1, username='王五', sex='null', birthday=null, address='null', 
orders=[Orders{id=3, user_id=1, number='1000010', createtime=Wed Feb 04 13:22:35 CST 2015, note='null'}, 
Orders{id=4, user_id=1, number='1000011', createtime=Tue Feb 03 13:22:41 CST 2015, note='null'}]}, 
User{id=10, username='张三', sex='null', birthday=null, address='北京市', orders=[Orders{id=5, user_id=10, number='1000012', createtime=Thu Feb 12 16:13:23 CST 2015, note='null'}]}, User{id=16, username='张小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=22, username='陈小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=24, username='张三丰', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=25, username='陈小明', sex='null', birthday=null, address='河南郑州', orders=[]}, User{id=26, username='王五', sex='null', birthday=null, address='null', orders=[]}, User{id=27, username='1', sex='null', birthday=null, address='2', orders=[]}, User{id=28, username='1', sex='null', birthday=null, address='2', orders=[]}, User{id=29, username='lilili', sex='null', birthday=null, address='SHHHH', orders=[]}, User{id=31, username='q', sex='null', birthday=null, address='q', orders=[]}, User{id=32, username='1', sex='null', birthday=null, address='2', orders=[]}]

数据库查询出来的结果



总结:当表关系为一对多时,比如A表对应B表,可以在A的实体类中增加一个集合,存放多个B类。通过外连接查询后返回到A表。此时A表里的集合存储为多条B。

最后

以上就是舒服泥猴桃为你收集整理的Mybatis一对多实战的全部内容,希望文章能够帮你解决Mybatis一对多实战所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部