首先需要的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多表关联配置的更多相关文章

  1. 三、mybatis多表关联查询和分布查询

    前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...

  2. JAVA入门[9]-mybatis多表关联查询

    概要 本节要实现的是多表关联查询的简单demo.场景是根据id查询某商品分类信息,并展示该分类下的商品列表. 一.Mysql测试数据 新建表Category(商品分类)和Product(商品),并插入 ...

  3. mybatis多表关联查询之resultMap单个对象

    resultMap的n+1方式实现多表查询(多对一) 实体类 创建班级类(Clazz)和学生类(Student),并在Student中添加一个Clazz类型的属性,用于表示学生的班级信息. mappe ...

  4. mybatis多表关联

    1.比如我有两个一个是菜单表t_menu,一个是权限表t_jurisdiction.表结构如下: 2.我想要将这两个表关联,查询特定的role_id下的菜单情况,这也是我们经常用在权限管理系统中的做法 ...

  5. MyBatis 多表关联查询

    多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...

  6. Mybatis多表关联查询字段值覆盖问题

    一.错误展示 1.首先向大家展示多表关联查询的返回结果集 <resultMap id="specialdayAndWorktimeMap type="com.hierway. ...

  7. 5.mybatis一对一表关联查询

    方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集,封装联表查询的数据(去除重复的数据)  SELECT * FROM class c,teacher t WHERE c.tid = t.t ...

  8. spring boot 2使用Mybatis多表关联查询

    模拟业务关系:一个用户user有对应的一个公司company,每个用户有多个账户account. spring boot 2的环境搭建见上文:spring boot 2整合mybatis 一.mysq ...

  9. mybatis association表关联与rowbounds共同使用时的异常及其解决方案

    按照mybatis手册中所说的,association有两种实现方式,嵌套查询和嵌套结果映射.如手册中所述,select方式会带来N+1次查询的问题,考虑到效率问题的话建议使用嵌套结果映射.但是在结合 ...

随机推荐

  1. Linux环境下验证码不显示F12报500

     前言: 项目之前部署在linux系统上进行测试,今天重新部署的时候,重启了tomcat然后部署新的版本项目,结果登录页面验证码不显示,在浏览器F12页面显示的是500错误.网上查了很多方法,都没效果 ...

  2. 测试第一个Oracle存储过程

    存储过程语句 //简单存储过程的例子 //每调用一次打印一次hello world create or replace procedure sayhelloworld as begin dbms_ou ...

  3. 第二周习题O题

    Description   Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an or ...

  4. Codeforces Round #304 (Div. 2)-D. Soldier and Number Game,素因子打表,超时哭晕~~

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  5. docsearch & algolia

    docsearch & algolia The easiest way to add search to your documentation. https://community.algol ...

  6. Search Insert Position(二分查找)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. 打开Eclipse出现“An internal error has occurred. java.lang.NullPointerException

    今天打开eclipse出现了这个问题:下面是老外给出的办法,粘给大家: Instead of deleting the whole .metadata folder, just delete the ...

  8. 源码SDWebImage

    源码来源:https://github.com/rs/SDWebImage 版本: 3.7 SDWebImage是一个开源的第三方库,它提供了UIImageView的一个分类,以支持从远程服务器下载并 ...

  9. oracle索引简单使用

    --查询表索引 select * from user_ind_columns where table_name = upper('HY_PROJECT') and column_name = uppe ...

  10. Why we have tuple and list in python

    The most notable difference between tuple and list is that tuple is immutable and list is mutable. B ...