设为首页 收藏本站
查看: 976|回复: 0

[经验分享] mybatis复杂查询例子

[复制链接]

尚未签到

发表于 2016-11-25 08:47:49 | 显示全部楼层 |阅读模式
最近用到了mybatis框架,对于多表联合查询将结果集转化为复杂java对象。
现在写下这个例子,基本包括了多对多,一对多,多对一的复杂情况。
 


  • 表结构和javabean


  5张表:博客表blog,作者表author,文章表post,标签表tag,文章标签关联表post_tag。
  每个博客对应一个作者,和多个文章。每个文章对应一个作者。文章和标签是多对多的关系。
  建表sql语句

CREATE TABLE `blog` (
`blog_id` int(11) NOT NULL AUTO_INCREMENT,
`blog_title` varchar(100) DEFAULT NULL,
`blog_author_id` int(11) DEFAULT NULL,
PRIMARY KEY (`blog_id`)
)
CREATE TABLE `author` (
`author_id` int(11) NOT NULL AUTO_INCREMENT,
`author_username` varchar(100) DEFAULT NULL,
`author_password` varchar(100) DEFAULT NULL,
`author_email` varchar(100) DEFAULT NULL,
`author_bio` varchar(100) DEFAULT NULL,
`author_favourite_section` varchar(100) DEFAULT NULL,
PRIMARY KEY (`author_id`)
)
CREATE TABLE `post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`post_subject` varchar(100) DEFAULT NULL,
`post_body` varchar(100) DEFAULT NULL,
`blog_id` int(11) DEFAULT NULL,
`post_author_id` int(11) DEFAULT NULL,
PRIMARY KEY (`post_id`)
)
CREATE TABLE `tag` (
`tag_id` int(11) NOT NULL AUTO_INCREMENT,
`tag_name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`tag_id`)
)
CREATE TABLE `post_tag` (
`post_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
PRIMARY KEY (`post_id`,`tag_id`)
)
  实体类,省略了所有get和set方法

public class Blog {
private int id;
private String title;
private Author author;
private ArrayList<Post> posts;
public Blog(Integer id){
this.id=id;
}
}

public class Author {
private int id;
private String username;
private String password;
private String email;
private String bio;
private String favouriteSection;
}

public class Post {
private int id;
private String subject;
private String body;
private Author author;
private ArrayList<Tag> tags;
}

public class Tag {
private int id;
private String name;
}


  • mapper接口和配置文件
  mapper接口定义

public interface BlogMapper {
public Blog findById(int id);
}
  BlogMapper.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="org.myBatis.mapper.BlogMapper">
<select id="findById" parameterType="int" resultMap="detailedBlogResultMap">
SELECT
b.blog_id,
b.blog_title,
a.author_id,
a.author_username,
a.author_password,
a.author_email,
a.author_bio,
a.author_favourite_section,
p.post_id,
p.post_subject,
p.post_body,
t.tag_id,
t.tag_name
FROM
blog b
LEFT JOIN author a ON b.blog_author_id = a.author_id
LEFT JOIN post p ON b.blog_id = p.blog_id
LEFT JOIN post_tag pt ON pt.post_id=p.post_id
LEFT JOIN tag t ON pt.tag_id=t.tag_id
WHERE b.blog_id = #{id}
</select>
<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int" />
</constructor>
<result property="title" column="blog_title" />
<association property="author" column="blog_author_id"
javaType="Author">
<id property="id" column="author_id" />
<result property="username" column="author_username" />
<result property="password" column="author_password" />
<result property="email" column="author_email" />
<result property="bio" column="author_bio" />
<result property="favouriteSection" column="author_favourite_section" />
</association>
<collection property="posts" ofType="Post">
<id property="id" column="post_id" />
<result property="subject" column="post_subject" />
<result property="body" column="post_body" />
<association property="author" column="post_author_id"
javaType="Author">
<id property="id" column="author_id" />
<result property="username" column="author_username" />
<result property="password" column="author_password" />
<result property="email" column="author_email" />
<result property="bio" column="author_bio" />
<result property="favouriteSection" column="author_favourite_section" />
</association>
<collection property="tags" column="post_id" ofType="Tag">
<id property="id" column="tag_id" />
<id property="name" column="tag_name" />
</collection>
</collection>
</resultMap>
</mapper>  
  根据id返回唯一的blog
  id为detailedBlogResultMap的resultMap将sql语句返回的结果集转换为blog对象。
  constructor标签:定义了Blog类的构造方法
  result标签:根据返回的结果集列对应blog对象的属性
  association标签:一个blog对应一个author,定义了author的映射关系
  collection标签:一个blog有多个post,定义了post的映射关系,该标签下面可以继续嵌套association和collection


  • 测试类MyBatisTest

public class MyBatisTest {
public static void main(String[] args) {
SqlSessionFactory sessionFactory = null;  
String resource = "configuration.xml";  
try {  
sessionFactory = new SqlSessionFactoryBuilder().build(Resources  
.getResourceAsReader(resource));  
} catch (IOException e) {  
e.printStackTrace();  
}  
SqlSession session = sessionFactory.openSession();  
BlogMapper mapper = session.getMapper(BlogMapper.class);  
Blog blog = mapper.findById(1);
session.close();
}
}


  • BlogMapper.xml结构简化
  可以将author、post、tag等对象单独定义成resultMap

<?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="org.myBatis.mapper.BlogMapper">
<select id="findById" parameterType="int" resultMap="detailedBlogResultMap">
SELECT
b.blog_id,
b.blog_title,
a.author_id,
a.author_username,
a.author_password,
a.author_email,
a.author_bio,
a.author_favourite_section,
p.post_id,
p.post_subject,
p.post_body,
t.tag_id,
t.tag_name
FROM
blog b
LEFT JOIN author a ON b.blog_author_id =
a.author_id
LEFT JOIN
post p ON b.blog_id = p.blog_id
LEFT JOIN post_tag
pt ON
pt.post_id=p.post_id
LEFT JOIN tag t ON pt.tag_id=t.tag_id
WHERE
b.blog_id = #{id}
</select>
<resultMap id="authorResultMap" type="Author">
<id property="id" column="author_id" />
<result property="username" column="author_username" />
<result property="password" column="author_password" />
<result property="email" column="author_email" />
<result property="bio" column="author_bio" />
<result property="favouriteSection" column="author_favourite_section" />
</resultMap>
<resultMap id="tagResultMap" type="Tag">
<id property="id" column="tag_id" />
<id property="name" column="tag_name" />
</resultMap>
<resultMap id="postResultMap" type="Post">
<id property="id" column="post_id" />
<result property="subject" column="post_subject" />
<result property="body" column="post_body" />
<association property="author" column="post_author_id"
javaType="Author" resultMap="authorResultMap" />
<collection property="tags" column="post_id" ofType="Tag"
resultMap="tagResultMap" />
</resultMap>
<resultMap id="detailedBlogResultMap" type="Blog">
<constructor>
<idArg column="blog_id" javaType="int" />
</constructor>
<result property="title" column="blog_title" />
<association property="author" column="blog_author_id"
javaType="Author" resultMap="authorResultMap" />
<collection property="posts" ofType="Post" resultMap="postResultMap" />
</resultMap>
</mapper>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305214-1-1.html 上篇帖子: MyBatis的动态SQL详解 下篇帖子: mybatis编程事务管理参考一
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表