本文主要演示了使用MyBatis實現一對多和多對一的關聯關係的查詢以及緩存的使用。注意 : 這裏使用的面向接口的開發方式來演示的。
一、創建表和分析
下面是兩個表。一個是用戶表,一個是博客表。一個用戶可以發表多個博客,但是一個博客只能屬於一個用戶。
tbl_user : 用戶表,一個用戶可以發表多個博客
tbl_blog : 博客表,多個博客可以是一個人發的
1. 用戶表結構
2. 博客表結構
二、工程的創建
1. 新建Java工程,結構目錄和Jar包如下:
2. 創建對應的類
User.java
- 1
- 2
package org.blogs.bean;
import java.io.Serializable;
import java.util.List;
/**
* 用戶實體類
* @author chenkangjing
* @date 2016年11月13日下午9:31:24
* @description
*/
public class User implements Serializable {
/**
* 序列化編號
*/
private static final long serialVersionUID = -6659051528198220654L;
private Integer id; //用戶編號
private String username; //用戶名稱
private String password; //用戶密碼
private String realName; //真實姓名
private String email; //用戶郵箱
private List<Blog> blogs; //多方<博客>集合
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Blog> getBlogs() {
return blogs;
}
public void setBlogs(List<Blog> blogs) {
this.blogs = blogs;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
Blog.java
- 1
- 2
package org.blogs.bean;
import java.io.Serializable;
import java.util.Date;
/**
* 博客的實體類
* @author chenkangjing
* @date 2016年11月13日下午9:29:38
* @description
*/
public class Blog implements Serializable {
/**
* 序列化編號
*/
private static final long serialVersionUID = 7080886266782047236L;
private Integer id; //博客編號
private String title; //博客標題
private String content; //博客內容
private Date pubDate; //發佈時間
private User user; //一方<用戶>的實體類
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Blog [id=" + id + ", title=" + title + ", content=" + content + ", pubDate=" + pubDate + ", user="
+ user + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
注意:User.java裏有一個Blogs集合,Blog.java裏有一個User對象。
3. 編寫一個jdbc.properties的屬性文件,用來存儲jdbc的連接信息,寫成屬性文件,便於修改。
##jdbc.properties文件中的連接信息
driverName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/test
username = root
password = root
- 1
- 2
- 3
- 4
- 5
- 6
4. 編寫MyBatis的配置文件
<?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="config/jdbc.properties"/>
<!-- 配置延遲加載 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 映射別名,如果不映射的話需要些全限定名。這裏是指定個整個包 -->
<typeAliases>
<package name="org.blogs.bean"/>
</typeAliases>
<!-- 配置環境 -->
<environments default="mySql">
<environment id="mySql">
<!-- 事物管理器,採用jdbc方式來管理事物 -->
<transactionManager type="JDBC"/>
<!-- 配置數據源 -->
<dataSource type="POOLED">
<property name="driver" value="${driverName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 映射器,配置Sql映射文件 -->
<mappers>
<mapper resource="org/blogs/mappers/BlogMapper.xml"/>
<mapper resource="org/blogs/mappers/UserMapper.xml"/>
</mappers>
</configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
5. 編寫接口
1.UserDao.java
- 1
- 2
package org.blogs.dao;
import java.util.List;
import org.blogs.bean.User;
/**
* 用戶類的業務接口
* @author chenkangjing
* @date 2016年11月13日下午9:34:27
* @description
*/
public interface UserDao {
/*
* 這裏注意方法名稱要和sql映射文件的select標籤的id一致
*/
public User getById(Integer id);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2. BlogDao.java
- 1
- 2
package org.blogs.dao;
import java.util.List;
import java.util.Map;
import org.blogs.bean.Blog;
/**
* 博客的業務接口
* @author chenkangjing
* @date 2016年11月13日下午9:32:38
* @description
*/
public interface BlogDao {
public Blog getById(Integer id);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
6. 配置關聯關係(sql映射文件)
1.一對多關聯關係配置(第一種,可以使用延遲加載)
- 1
- 2
<?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.blogs.dao.UserDao">
<!-- 配置緩存 -->
<cache></cache>
<!-- 配置一對多關聯關係映射: 使用延遲加載的方式去加載 -->
<resultMap type="User" id="userMap" autoMapping="true">
<id property="id" column="id"/>
<!-- select裏的對應sql映射調用 -->
<collection property="blogs" javaType="List" ofType="Blog" column="id" select="org.blogs.dao.BlogDao.getListByUserId"></collection>
</resultMap>
<select id="getById" parameterType="int" resultMap="userMap">
select id,username,password,realName,email from tbl_user where id = #{id}
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2.一對多關聯關係配置(第二種,聯表的方式)
- 1
- 2
<?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.blogs.dao.UserDao">
<!-- 配置緩存 -->
<cache></cache>
<!-- 第二種 -->
<resultMap type="User" id="userMap1" autoMapping="true">
<id property="id" column="uid"/>
<collection property="Blogs" javaType="List" ofType="Blog" autoMapping="true">
<id column="id" property="id"/>
</collection>
</resultMap>
<select id="getById1" parameterType="int" resultMap="userMap1">
select u.id as uid,username,password,realName,email from tbl_user u
left join tbl_blog b on u.id = b.id
where u.uid = #{id}
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.多對一關聯關係映射(第一種,單獨發送一條sql查詢的.用於延遲加載)
- 1
- 2
<?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.blogs.dao.BlogDao">
<cache/>
<resultMap type="Blog" id="blogMap" autoMapping="true">
<id property="id" column="id"/>
<!-- 配置多對一的標籤,select需要去UserDao單獨發送一條語句 -->
<association property="user" javaType="User" column="user_id" select="org.blogs.dao.UserDao.getById"/>
</resultMap>
<select id="getById" parameterType="int" resultMap="blogMap">
select * from tbl_blog where id = #{id}
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
4.多對一關聯關係配置(第二種,發送聯表語句)
- 1
- 2
<?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.blogs.dao.BlogDao">
<cache/>
<resultMap type="Blog" id="blogMap" autoMapping="true">
<id column="id" property="id"/>
<association property="user" javaType="User">
<id column="uid" property="id"/>
</association>
</resultMap>
<select id="getById" parameterType="int" resultMap="blogMap">
select b.*,u.id as uid,u.username,u.password,u.realName,u.email from tbl_blog b
left join tbl_user u on b.user_id = u.id
where b.id = #{id}
</select>
</mapper>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16