mybatis多表关联配置
首先需要的jar包
mybatis-3.4.1
配置文件(mybatis.cfg.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> <typeAliases>
<typeAlias alias="mybatis1" type="entity.mybatis1"/>
<typeAlias alias="types" type="entity.types"/>
</typeAliases> <!-- 选择默认的连接方式 -->
<environments default="myb"> <environment id="myb">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="accp"/>
</dataSource>
</environment> </environments> <mappers>
<mapper resource="entity/mybatis1Mapper.xml"/>
</mappers>
</configuration>
Mybatis1.java
package entity;
public class mybatis1 {
private int id;
private String name;
private int age;
private types types;
public types getTypes() {
return types;
}
public void setTypes(types types) {
this.types = types;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public mybatis1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public mybatis1(){}
}
Types.java
package entity; import java.util.ArrayList;
import java.util.List; public class types { private int id;
private String tname;
private List<mybatis1> mybatis=new ArrayList<mybatis1>();
public List<mybatis1> getMybatis(){
return mybatis;
}
public void setMybatis(List<mybatis1> mybatis) {
this.mybatis = mybatis;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
mybatis1Mapper.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="entity.mybatis1"> <resultMap type="mybatis1" id="mylist">
<id property="id" column="id"></id>
<result column="name" property="name"/>
<result column="age" property="age"/>
<!-- 多对一的关系 -->
<!-- property: 指的是属性的值, javaType:指的是属性的类型-->
<association property="types" javaType="types">
<id column="id" property="id"/>
<result column="tname" property="tname"/>
</association>
</resultMap>
<select id="selectall" resultMap="mylist">
select m.*,t.tname tname from mybatis1 m ,types t
where m.typeid=t.id
</select> <insert id="addmybatis" parameterType="entity.mybatis1">
insert into mybatis1(name,age) values(#{name},#{age})
</insert> </mapper>
typesMapper.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="entity.types"> <resultMap type="types" id="mytype">
<id column="id" property="id"></id>
<result column="tname" property="tname"/>
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="mybatis" ofType="mybatis1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</collection>
</resultMap> <select id="selectalltype" resultMap="mytype">
select m.*,t.tname tname from mybatis1 m ,types t where m.typeid=t.id
</select>
</mapper>
test_mybatis.java(测试)
package test; import java.io.IOException;
import java.io.Reader;
import java.util.List; 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 entity.mybatis1; public class test_mybatis { public static void main(String[] args) {
try {
Reader r= Resources.getResourceAsReader("mybatis.cfg.xml");
SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
SqlSessionFactory sf=sfb.build(r);
SqlSession session=sf.openSession(); // mybatis1 my=new mybatis1("zs", 12);
// session.insert("entity.mybatis1.addmybatis", my);
// session.commit();
String d="d";
List<mybatis1> list=session.selectList("selectall");
System.out.println(list.size());
for(mybatis1 m:list){
System.out.println(m.getTypes().getTname()+"=="+m.getName());
}
session.close(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
mybatis多表关联配置的更多相关文章
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- JAVA入门[9]-mybatis多表关联查询
概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入 ...
- mybatis多表关联查询之resultMap单个对象
resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...
- mybatis多表关联
1.比如我有两个一个是菜单表t_menu,一个是权限表t_jurisdiction.表结构如下: 2.我想要将这两个表关联,查询特定的role_id下的菜单情况,这也是我们经常用在权限管理系统中的做法 ...
- MyBatis 多表关联查询
多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...
- Mybatis多表关联查询字段值覆盖问题
一.错误展示 1.首先向大家展示多表关联查询的返回结果集 <resultMap id="specialdayAndWorktimeMap type="com.hierway. ...
- 5.mybatis一对一表关联查询
方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集,封装联表查询的数据(去除重复的数据) SELECT * FROM class c,teacher t WHERE c.tid = t.t ...
- spring boot 2使用Mybatis多表关联查询
模拟业务关系:一个用户user有对应的一个公司company,每个用户有多个账户account. spring boot 2的环境搭建见上文:spring boot 2整合mybatis 一.mysq ...
- mybatis association表关联与rowbounds共同使用时的异常及其解决方案
按照mybatis手册中所说的,association有两种实现方式,嵌套查询和嵌套结果映射.如手册中所述,select方式会带来N+1次查询的问题,考虑到效率问题的话建议使用嵌套结果映射.但是在结合 ...
随机推荐
- javascript事件委托和jquery事件委托
元旦过后,新年第一篇. 初衷:很多的面试都会涉及到事件委托,前前后后也看过好多博文,写的都很不错,写的各有千秋,自己思前想后,为了以后自己的查看,也同时为现在找工作的前端小伙伴提供一个看似更全方位的解 ...
- IDEA下Eclipse快捷键
- 关于latch: cache buffers chains的sql优化
前段时间,优化了一些耗buffer比较多的sql,但是CPU使用率还是没下来 . 查看操作系统CPU使用率 查看awr,发现又有一条超级耗性能的sql冒出来了. 该SQL每次执行耗费3e多个buffe ...
- LAMP中添加多虚拟主机
在/etc/apache2/sites-available中默认有个default文件,其中的大致配置如下: <VirtualHost *:80> ServerAdmin xujie198 ...
- Jquery validate自定义验证
http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...
- STM32串口程序的一般配置方法
#include "stm32f10x.h" /************************************************ 该程序讲解串口程序的一般配置方法: ...
- 精帖转载(关于stock problem)
Note: this is a repost(重新投寄) of my original post here with updated solutions(解决方案) for this problem ...
- 【Codeforces 492D】Vanya and Computer Game
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 第一个人攻击一次需要1/x秒 第二个人攻击一次需要1/y秒 这两个数字显然都是小数. 我们可以二分最后用了多少时间来攻击. 显然这个是有单调性 ...
- bzoj1007 [HNOI2008]水平可见直线 - 几何 - hzwer.com
Description Input 第一行为N(0 < N < 50000),接下来的N行输入Ai,Bi Output 从小到大输出可见直线的编号,两两中间用空格隔开,最后一个数字后面也必 ...
- boot简介
目录 1:bootloader介绍2:如何启动一个机器3:工具 Bootloader介绍 MTK的bootloader 主要分为Pre-loader, LK. Pre-loader: 初始化PLL,U ...