任务描述
通过上一关中的学习,相信大家对关联查询都有了基本的了解。今天我将为大家介绍一对多的关联查询。
本关任务:根据用户名称,查询这个用户创建了哪些博客。
相关知识
为了完成本关任务,你需要掌握:一对多查询。
一对多
上一关中,我们学会了一对一,其实一对多的实现思路也是一样的。只是一对一返回的是一个结果,而一对多返回的是一个结果集。
mn/src/main/resources/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>
<!--********** Begin **********-->
<!--给pojo包起别名-->
<typeAliases>
<package name="com.test.pojo"/>
</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/mydb?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123123"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件(mapper接口文件) -->
<mappers>
<mapper class="com.test.mapper.UserMapper"/>
<mapper class="com.test.mapper.BlogMapper"/>
</mappers>
<!--********** End **********-->
</configuration>
mn/src/main/java/com/test/mapper/BlogMapper.java
package com.test.mapper;
import com.test.pojo.Blog;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BlogMapper {
/*********** Begin **********/
//查询
@Select("SELECT * FROM t_blog WHERE userId=#{userId}")
List<Blog> selectByBlogId(int userId);
/*********** End **********/
}
mn/src/main/java/com/test/mapper/UserMapper.java
package com.test.mapper;
import com.test.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
public interface UserMapper {
/*********** Begin **********/
//关联查询
@Select("select * from t_user where userName = #{userName}")
@Results(id = "UserMap1", value = {
@Result(id = true, property = "userId", column = "userId"),
@Result(property = "userName", column = "userName"),
@Result(property = "passWord", column = "passWord"),
@Result(property = "blogs", column = "userId", many = @Many(select = "com.test.mapper.BlogMapper.selectByBlogId", fetchType = FetchType.EAGER))
})
User findBlog(String userName);
/*********** End **********/
}
mn/src/main/java/com/test/test/UserTest.java
package com.test.test;
import com.test.mapper.UserMapper;
import com.test.pojo.User;
import com.test.pojo.Blog;
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 java.io.IOException;
import java.io.Reader;
import java.util.List;
public class UserTest {
/*********** Begin **********/
private static SqlSessionFactory sqlSessionFactory;
//初始化
public static void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis_config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
/*********** End **********/
//查询数据
public static User selectBlog(String userName) {
/*********** Begin **********/
init();
SqlSession sqlSession = null;
User user = null;
try {
sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
user = mapper.findBlog(userName);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
return user;
/*********** End **********/
}
}