数据库

方式一:XML

按照下面类型建立article表

Article.java

package com.ij34.model;

public class Article {
private int id;
private User user;
private String title;
private String content; public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }

UserMapper.xml

颜色aid重点标出部分很重要,必须使用别名,不要最后结果获得article的id全是输入的id

<?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.ij34.mybatis.UserMapper">
<select id="selectUser" parameterType="int" resultType="com.ij34.model.User">
select * from users where id=#{id};
</select>
<update id="updateUser" keyProperty="id">
update users set name=#{name},age=#{age} where id=#{id}
</update> <insert id="insertUser" >
insert into users(name,age)values(#{name},#{age})
</insert>
<delete id="deleteUser">
delete from users where name=#{name}
</delete>
<resultMap type="Article" id="resultAticleList">
<id property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="user" javaType="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
<select id="selectarticle" parameterType="int" resultMap="resultAticleList">
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{id}
</select>
</mapper>

Test.java

package com.ij34.bean;

import java.io.IOException;
import java.io.InputStream;
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 com.ij34.model.*;
public class Test {
public static void main(String[] args) throws IOException {
String resource ="com/ij34/mybatis/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
try { List<Article> articles=session.selectList("com.ij34.mybatis.UserMapper.selectarticle",1);
for(Article article:articles){
System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser());
}
} finally {
// TODO: handle finally clause
session.close();
}
}
}

结果

方式二:通过接口

结构

xml

mybatis-config.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 type="com.ij34.model.User" alias="User"/>
<typeAlias type="com.ij34.model.Article" alias="Article"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ij34/mybatis/UserMapper.xml"/>
<!-- <mapper class="com.ij34.model.UserMapper"/> -->
</mappers>
</configuration>

UserMapper.xml

  <mapper  namespace="com.ij34.model.UserMapper">

对应于接口UserMapper

<?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.ij34.model.UserMapper">
<select id="selectUser" parameterType="int" resultType="com.ij34.model.User">
select * from users where id=#{id};
</select>
<update id="updateUser" keyProperty="id">
update users set name=#{name},age=#{age} where id=#{id}
</update> <insert id="insertUser" >
insert into users(name,age)values(#{name},#{age})
</insert>
<delete id="deleteUser">
delete from users where name=#{name}
</delete> <resultMap type="Article" id="resultAticleList">
<id property="id" column="aid"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<association property="user" javaType="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</association>
</resultMap>
<select id="selectarticle" parameterType="int" resultMap="resultAticleList">
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{id}
</select>
</mapper>

model

User.java

package com.ij34.model;

public class User {
private int id;
private String name;
private int age; 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 String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

Article.java

package com.ij34.model;

public class Article {
private int id;
private User user;
private String title;
private String content; public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
} }

UserMapper.java

package com.ij34.model;

import java.util.List;

public interface UserMapper {
/*// @Select("select * from User where id=#{id}")
public User selectUser(int id);
// @Update("update User set name=#{name},age=#{age} where id=#{id}")
public void updateUser(User user);
*/
public List<Article> selectarticle(int id);
}

测试

package com.ij34.bean;

import java.io.IOException;
import java.io.InputStream;
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 com.ij34.model.Article;
import com.ij34.model.UserMapper; public class Test {
public static void main(String[] args) throws IOException {
String resource ="com/ij34/mybatis/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();
try {
UserMapper mapper=session.getMapper(UserMapper.class);
List<Article> articles=mapper.selectarticle(1);
for(Article article:articles){
System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser());
}
} finally {
// TODO: handle finally clause
session.close();
}
}
}

结果

MyBatis笔记----多表关联查询两种方式实现的更多相关文章

  1. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

  2. Python与数据库[2] -> 关系对象映射/ORM[2] -> 建立声明层表对象的两种方式

    建立声明层表对象的两种方式 在对表对象进行建立的时候,通常有两种方式可以完成,以下是两种方式的建立过程对比 首先导入需要的模块,获取一个声明层 from sqlalchemy.sql.schema i ...

  3. ofbiz学习笔记01--多表关联查询

    不管做什么项目,肯定会用到多表关联查询数据,从网络查询得知ofbiz有三种多表关联查询方法 实现一:Screem.xml 中的 section 里,加 <action>, 加 get-re ...

  4. Mybatis控制台打印SQL语句的两种方式

    问题描述在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql就 ...

  5. Python与数据库 sqlalchemy 建立声明层表对象的两种方式

    在对表对象进行建立的时候,通常有两种方式可以完成,以下是两种方式的建立过程对比 首先导入需要的模块,获取一个声明层 1 from sqlalchemy.sql.schema import Table, ...

  6. Mybatis【15】-- Mybatis一对一多表关联查询

    注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-11-one2one,需要自取,需要配置maven ...

  7. mybatis多对一关联的两种方式

    第一个种是Address找到自己的user_id,扔给User,让User自己去再查一次,即使在有缓存的前提下,每遇到一个新的user_id,就会查一次,对比hibernate的话,相当于多对一eag ...

  8. MyBatis之多表关联查询

    1使用resultType.ResultMap处理返回结果 处理返回结果 resultType:指定返回值结果的完全限定名,处理多表查询的结果. 多表查询需要定义vo封装查询的结果. 需求:查询部门和 ...

  9. ORACLE 两表关联更新三种方式

    不多说了,我们来做实验吧. 创建如下表数据 select * from t1 ; select * from t2; 现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致. 方式1,u ...

随机推荐

  1. MySQL高可用新玩法之MGR+Consul

    前面的文章有提到过利用consul+mha实现mysql的高可用,以及利用consul+sentinel实现redis的高可用,具体的请查看:http://www.cnblogs.com/gomysq ...

  2. DHCP服务器的搭建

    dhcp笔记整理:http://services.linuxpanda.tech/DHCP/index.html 1 dhcp简介 DHCP原理 动态主机配置协议(Dynamic Host Confi ...

  3. 教你一个vue小技巧,一般人我不说的

    本文由云+社区发表 1. 需求 最近的项目中,需要实现在vue框架中动态渲染带提示框的单选/多选文本框,具体的效果如下图所示,在输入框聚焦时,前端组件通过接收的kv参数渲染出选项,用户点击选项,可以将 ...

  4. 关于JBoss7.X修改post传输数据量(max-post-size)的问题

    转自: https://blog.csdn.net/zhangyunhao108/article/details/53140569 JBoss7.X修改max-post-size在网上百度了好久,都不 ...

  5. Spring之Bean的生命周期详解

      通过前面多个接口的介绍了解了Bean对象生命周期相关的方法,本文就将这些接口的方法串起来,来了解Bean的完整的生命周期.而介绍Bean的生命周期也是面试过程中经常会碰到的一个问题,如果不注意就跳 ...

  6. python对象属性管理(2):property管理属性

    使用Property管理属性 python提供了一种友好的getter.setter.deleter类方法的属性管理工具:property. property()是一个内置函数,它返回一个Proper ...

  7. Linux文件权限与属性详解 之 一般权限

    目录 一般属性 1. iNode: 3152621 2. 文件类型 3.文件访问权限 4. 链接数目: 5. 文件所有者 6. 文件所属组 7. 文件大小 8. 修改时间 9. 文件名称 Linux文 ...

  8. python模块之sys与os

    python常用模块系列(二):sys模块与os模块 sys模块是python解释器和环境有关的一个模块: os是python用来和操作系统进行交互的一个模块. 一 sys 查看当前环境变量 查看已经 ...

  9. 【转载】ASP.NET生成图片的缩略图

    图片处理是C#程序开发中时常会涉及到的一个业务,除了图像的上传.保存以及下载等功能外,根据上传的图片生成一个缩略图也是常见业务,在C#语言中,可以通过Image类提供的相关方法对图片进行操作,如指定宽 ...

  10. Python在Office 365 开发中的应用

    我在昨天发布的文章 -- 简明 Python 教程:人生苦短,快用Python -- 中提到了Python已经在Office 365开发中全面受支持,有不同朋友留言或私信说想了解更加详细的说明,所以特 ...