spring boot mybatis 一对一 一对多
Posted xiaomaha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot mybatis 一对一 一对多相关的知识,希望对你有一定的参考价值。
pox配置
<!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- jack-json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
目录结构
数据库结构
基本配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/bet
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.bo
mapper-locations: mybatis配置文件扫描路径
type-aliases-package:数据库映射关系对象扫描路径(可不设置,如果不设置需要在<resultMap type="com.bo.User" id="bookResult"> type写入完整路径)
Bo
public class User {
private Integer id;
private String name;
private String sex;
private List<Book> books;
// get set .....
}
public class Book {
private String id;
private String name;
// get set............
}
UserMapper.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.dao.UserDao"> <resultMap type="com.bo.User" id="userResult"> <id property="id" column="ID" /> <result property="name" column="U_NAME" /> </resultMap> <resultMap type="com.bo.User" id="userResult1"> <id property="id" column="ID" /> <result property="name" column="U_NAME" /> <collection property="books" ofType="com.bo.Book"> <id property="id" column="B_ID" /> <result property="name" column="NAME" /> </collection> </resultMap> <select id="getUserAll" resultMap="userResult"> SELECT * FROM user </select> <select id="getUserById" resultMap="userResult1"> SELECT a.*,b.ID as B_ID,b.NAME FROM user a , books b WHERE a.ID=b.U_ID AND a.ID = #{id} </select> </mapper>
重点,如果用户表和书表,中主键名字一样比如例子中都为ID,那么1对多的情况下,会发现查询的book对象只会有一条数据,所以SQL使用别名来处理,结果就正常了!
上面代码中倒数第三行
DAO接口
package com.dao; import java.util.List; import com.bo.User; public interface UserDao { public List<User> getUserAll(); public User getUserById(Integer id); }
Test测试
package com.test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.bo.Book; import com.bo.User; import com.dao.UserDao; @RunWith(SpringRunner.class) @SpringBootTest public class Test1 { @Autowired private UserDao userDao; @Test public void test(){ List<User> list = userDao.getUserAll(); for(User u : list){ System.out.println(u.getId()+":"+u.getName()); } System.out.println("-----------------"); User u = userDao.getUserById(1); System.out.println(u.getId()+":"+u.getName()); List<Book> books = u.getBooks(); System.out.println(books.size()); for(Book book : books){ System.out.println(book.getName()); } } }
结果
以上是关于spring boot mybatis 一对一 一对多的主要内容,如果未能解决你的问题,请参考以下文章
spring-boot-mybatis-plus-layui 自定义代码生成完整多对一
spring-boot-mybatis-plus-layui 自定义代码生成完整多对一
spring boot + MyBatisPlus 一对多、多对一、多对多的解决方案