配置一对多
1.创建用户实体类,映射一对多关系(省略set,get方法)
public class User {
private Integer id;
private String username;
private String password;
//一个用户对应多个博客
private List<Blog> listblog;
}
2.创建博客实体类
public class Blog {
private Integer id;
private Integer userid;
private String title;
private String description;
}
3.编写mapper接口
package com.springboot.mapper;
/**
* @author TANGSHUAI
* @date 2021年3月15日 下午9:25:08
*
*/
import java.util.List;
import com.springboot.po.User;
public interface UserMapper {
List<User> selectAllUser();
}
4.mapper接口关联mapper.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="com.springboot.mapper.UserMapper">
<select id="selectAllUser" parameterType="com.springboot.po.User" resultMap="resultMapUser">
select
u.id as
uid,u.username,u.password,b.id as bid,b.title,b.user_id as userid,b.description from m_user
u LEFT JOIN m_blog b on
u.id=b.user_id
</select>
<resultMap type="com.springboot.po.User" id="resultMapUser">
<id column="uid" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
<!-- 返回对象<association property="" javaType=""></association> -->
<!-- 返回集合<collection property="" ofType="" /> -->
<collection property="listblog" ofType="com.springboot.po.Blog" column="user_id">
<!-- 这里的column对应的是下面查询的别名,而不是表字段名 -->
<id column="bid" property="id" />
<!-- property对应JavaBean中的属性名 -->
<result column="title" property="title" />
<result column="userid" property="userid" />
<result column="description" property="description" />
</collection>
</resultMap>
</mapper>
5.编写service层(此处省略了service接口)
package com.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.mapper.UserMapper;
import com.springboot.po.User;
/**
* @author TANGSHUAI
* @date 2021年3月15日 下午9:33:49
*
*/
@Service
public class TestService {
@Autowired
private UserMapper userMapper;
public List<User> selectAllUser(){
List<User> selectAllUser = this.userMapper.selectAllUser();
return selectAllUser;
}
}
6.编写controller
/**
* @author TANGSHUAI
* @date 2021年3月15日 下午9:56:28
*
*/
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/selectAllUser")
public @ResponseBody List<User> selectAllUser(){
return this.testService.selectAllUser();
}
}
配置一对一
1.实体类关联一对一
public class Blog {
private Integer id;
private Integer userid;
private String title;
private String description;
//一个博客只属于一个用户,配置一对一
private User user;
public class User {
private Integer id;
private String username;
private String password;
}
2.编写Mapper
public interface BlogMapper {
/**
* 查询全部博客
* @return
*/
List<Blog> selectAllBlog();
}
3.关联mapper.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="com.springboot.mapper.BlogMapper">
<select id="selectAllBlog" resultMap="resultBlogMap">
select u.id as
uid,u.username,u.password,b.id as bid,b.title,b.user_id as userid,b.description from m_blog b LEFT JOIN m_user u on b.user_id=u.id where b.id=1
</select>
<resultMap type="com.springboot.po.Blog" id="resultBlogMap">
<result column="bid" property="id"/>
<result column="title" property="title"/>
<result column="description" property="description"/>
<association property="user" javaType="com.springboot.po.User">
<result column="uid" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</association>
</resultMap>
</mapper>