pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.henu</groupId><artifactId>mybatis03_manyTomany</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency></dependencies></project>

bean

Role

User

Dao

RoleDao

UserDao

Resources

SqlMapConfig.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><properties resource="jdbcConfig.properties"></properties><!--使用typeAliases配置别名,它只能配置bean中类的别名--><typeAliases><package name="com.henu.bean"></package></typeAliases><!-- 配置环境 --><environments default="mysql"><!--配置mysql的环境--><environment id="mysql"><!--配置事务的类型--><transactionManager type="JDBC"></transactionManager><!--配置数据源(连接池)--><dataSource type="POOLED"><!--配置连接数数据库的基本信息--><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!--配置映射文件的位置--><mappers><package name="com.henu.dao"></package></mappers></configuration>

log4j.properties

# Global logging configuration
log4j.rootLogger=error, stdout
# MyBatis logging configuration...
log4j.logger.cn.wolfcode.mybatis=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

jdbcConfig.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
username=root
password=123456

RoleDao.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.henu.dao.RoleDao"><!--定义role表的mapproperty是对应bean中的属性column是对应数据库中的列--><resultMap id="roleMap" type="role"><id property="roleId" column="id"></id><result property="role_Name" column="role_name"></result><result property="role_Desc" column="role_desc"></result><collection property="users" ofType="user"><id property="id" column="rid"></id><result property="username" column="username"></result><result property="birthday" column="birthday"></result><result property="sex" column="sex"></result><result property="address" column="address"></result></collection></resultMap><!--配置查询所有--><select id="findAll" resultMap="roleMap">select u.*,r.id as rid,r.role_name,r.role_desc from role rleft outer join user_role ur on r.id=ur.ridleft outer join user u on u.id=ur.uid</select></mapper>

UserDao.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.henu.dao.UserDao"><resultMap id="userMap" type="user"><id property="id" column="id"></id><result property="username" column="username"></result><result property="birthday" column="birthday"></result><result property="sex" column="sex"></result><result property="address" column="address"></result><collection property="roles" ofType="role"><id property="roleId" column="id"></id><result property="role_Name" column="role_name"></result><result property="role_Desc" column="role_desc"></result></collection></resultMap><!--配置查询所有--><!--id为方法名称--><select id="findAll" resultMap="userMap">select u.*,r.id as rid,r.role_name,r.role_desc from user uleft outer join user_role ur on u.id = ur.uidleft outer join role r on r.id = ur.rid</select></mapper>

test

RoleTest

package com.henu.test;import com.henu.bean.Role;
import com.henu.bean.User;
import com.henu.dao.RoleDao;
import com.henu.dao.UserDao;
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.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** @author George* @description**/
public class RoleTest {private InputStream in;private SqlSession sqlSession;private RoleDao roleDao;@Before //用于测试方法执行之前执行public void init() throws IOException {in = Resources.getResourceAsStream("SqlMapConfig.xml");//2.创建SqlSessionFactory工厂SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);//3.使用工厂生成SqlSession对象sqlSession = factory.openSession();//4.使用SqlSession创建Dao接口的代理对象roleDao = sqlSession.getMapper(RoleDao.class);}@After //用于测试方法执行之后执行public void destory() throws IOException {sqlSession.close();in.close();}/*** 查询全部*/@Testpublic void findAllTest(){List<Role> roles = roleDao.findAll();for (Role role : roles) {System.out.println("--------------每个角色的信息--------------");System.out.println(role);System.out.println(role.getUsers());}}}

UserTest

package com.henu.test;import com.henu.bean.User;
import com.henu.dao.UserDao;
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.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** @author George* @description**/
public class UserTest {private InputStream in;private SqlSession sqlSession;private UserDao userDao;@Before //用于测试方法执行之前执行public void init() throws IOException {in = Resources.getResourceAsStream("SqlMapConfig.xml");//2.创建SqlSessionFactory工厂SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);//3.使用工厂生成SqlSession对象sqlSession = factory.openSession();//4.使用SqlSession创建Dao接口的代理对象userDao = sqlSession.getMapper(UserDao.class);}@After //用于测试方法执行之后执行public void destory() throws IOException {sqlSession.close();in.close();}/*** 查询全部*/@Testpublic void findAllTest(){List<User> users = userDao.findAll();for (User user : users) {System.out.println("-------每个用户的信息---------");System.out.println(user);System.out.println(user.getRoles());}}}

mybatis实现多对多相关推荐

  1. mybatis实现多对多关联查询(超详细版)

    mybatis实现多对多关联查询XML实现 ​ 在开发过程中,持久层架mybatis为我们封装了SQL操作,只需要提供相应的SQL语句即可查询出结果,若结合逆向工程插件便可免去写一些简单SQL的繁琐工 ...

  2. MyBatis(二)——多对一、一对多

    文章目录 1. 多对一 1.1 在MySQL中创建student表.teacher表 1.2 编写实体类 1.3 编写接口 1.4 编写接口对应的配置文件 1.5 确定两个xml文件都绑定到了myba ...

  3. mybatis关联 多对多,一对多,多对一的编写

    1. 准备 数据库 以及数据 /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.7.23 : Database - test ***************** ...

  4. Mybatis实现多对多关联组合查询

    个人网站:http://xiaocaoshare.com/ 1.需求 用户信息表.标签表.用户标签关联信息表 在做用户列表查询的时候,需要查询出该用户对应的用户标签 <resultMap id= ...

  5. mybatis映射多对多查询实现

    1.同以前一样,首先给一个使用多对多的需求, 要查询用户以及用户所购买的商品信息,经过分析用户和商品数据库级别没有任何关系,用户和商品需要建立关系,要通过订单,订单明细建立关系.根据这个需求,可以分析 ...

  6. 7. MyBatis多表查询 - 一对一 - 一对多 - 多对多

    7. MyBatis多表查询 - 一对一 - 一对多 - 多对多 前言 在前面的篇章,我们已经熟悉了单表查询,下面我们来看看如何进行 多表查询. 数据准备 create database if not ...

  7. MyBatis研习录(10)——MyBatis多对多查询

    C语言自学完备手册(33篇) Android多分辨率适配框架 JavaWeb核心技术系列教程 HTML5前端开发实战系列教程 MySQL数据库实操教程(35篇图文版) 推翻自己和过往--自定义View ...

  8. mybatis手写多对多关联映射

    mybatis手写多对多关联映射 1.一对一关联查询 1.1resultType实现 1.2resultMap实现 2.一对多关联查询 3.多对多关联查询 4.resultType与resultMap ...

  9. mybatis与php,浅谈mybatis中的#和$的区别

    浅谈mybatis中的#和$的区别 发布于 2016-07-30 11:14:47 | 236 次阅读 | 评论: 0 | 来源: 网友投递 MyBatis 基于Java的持久层框架MyBatis 本 ...

最新文章

  1. PLSQL的截取函数
  2. *** FATAL ERROR: too many grib files .. 1st=F:\data\预测数据 2nd=- ***
  3. 【深度学习】深度学习之LSTM
  4. Android inline hook手记
  5. 办公OA的附件无法下载、打不开的解决办法
  6. superoneclick 2.2_一季度食品监督抽检2.2%不合格:农兽药残留超标等系主因
  7. python安装mysqldb模块,如何使用pip安装Python MySQLdb模块?
  8. Windows XP14个小技巧
  9. 计算机保研面试之机器学习
  10. 数组中除一个元素外其他所有元素出现二或三次,找到只出现一次的元素
  11. Java温习——SUN公司和Java平台
  12. TypeScript02 方法特性【参数种类、参数个数】、generate方法、析构表达式、箭头表达式、循环...
  13. c语言数字大小排序的理解,教孩子数字比大小,排序很重要
  14. LaTex(PART IV) 各级标题
  15. python朋友圈点赞_python(html 点赞+1)
  16. 中小型企业网络解决方案的设计和实施
  17. 天正提示加载lisp_使用AutoCAD2004加载一个Lisp程序后,显示加载成功,但是并未出现提示输入点的信息...
  18. 鼠标双击测试r软件,R.A.T7蜕变版游戏鼠标使用测试_Mad Catz R.A.T.7蜕变版激光游戏鼠标_键鼠评测-中关村在线...
  19. does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE),
  20. SEO基础之跳出率(用户体验连载3)

热门文章

  1. linux算法设计,红黑树的原理分析和算法设计
  2. java 妖魔道-倩女幽魂_《倩女幽魂I-妖魔道》地图新手功略
  3. python各种数据类型的常用方法_python的基本数据类型:列表的方法
  4. Linux闲时自动抢占GPU脚本
  5. Fastai-数据准备
  6. OD的hit跟踪和run跟踪
  7. COM编程之二 接口
  8. C语言程序设计 | 大端小端存储解析以及判断方法
  9. linux问题排查常用命令详解
  10. linux常用安装命令集锦