Springboot整合Mybatis通过xml配置一对多,一对一

配置一对多

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>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值