目录
前言:
项目结构:
myuser类:
post类:
启动类:
controller:
mapper接口
Mapper.xml
数据库表结构:
一对多返回格式? (这里主要为 一个用户拥有多个文章):
前言:
项目结构: myuser类:就是无聊记录一下…
post类:@Data
public class MyUser {private int id; private String username; private String mobile; private Listposts; }
启动类:@Data
public class Post {private int id; private Integer userid; private String title; private String content;}
controller:@SpringBootApplication
@MapperScan(“com.luo.mybatisdemo.dao”)
public class MybatisdemoApplication {public static void main(String[] args) { SpringApplication.run(MybatisdemoApplication.class, args); }}
mapper接口@RequestMapping("/hello1")
public List hello1(){
List books = myUserMapper.getAllBooks(1);
return books;
}
Mapper.xmlList getAllBooks(Integer id);
数据库表结构:SELECT u.*,p.* FROM myuser u, post p WHERe u.id=p.userid AND u.id=#{user_id}
post表:
myuser表:
一对多返回格式 (这里主要为 一个用户拥有多个文章):[{
“id”: 1,
“username”: “yiibai”,
“mobile”: “100”,
“posts”: [{
“id”: 1,
“userid”: 1,
“title”: “MyBatis关联数据查询”,
“content”: “在实际项目中,经常使用关联表的查询,比如:多对一,一对多等。这些查询是如何处理的呢,这一讲就讲这个问题。我们首先创建一个 post 表,并初始化数据.”
}, {
“id”: 2,
“userid”: 1,
“title”: “MyBatis开发环境搭建”,
“content”: “为了方便学习,这里直接建立java 工程,但一般都是开发 Web 项目。”
}]
}]


![[Mybatis 一对多 多对多 ] 待更新 [Mybatis 一对多 多对多 ] 待更新](http://www.mshxw.com/aiimages/31/770724.png)
