代码拉取完成,页面将自动刷新
# application.yml
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: 123456
# type: com.alibaba.druid.pool.DruidDataSource
mybatis-flex:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Data
@Table("t_role")
public class Role implements Serializable {
@Id(keyType = KeyType.Auto)
private Integer id;
private String name;
@RelationManyToMany(
joinTable = "t_user_role",
joinSelfColumn = "role_id",
joinTargetColumn = "user_id")
private List<User> users;
}
@Data
@Table("t_user")
public class User implements Serializable {
@Id(keyType = KeyType.Auto)
private Integer id;
private String name;
@RelationManyToMany(
joinTable = "t_user_role",
joinSelfColumn = "user_id",
joinTargetColumn = "role_id")
private List<Role> roles;
}
CREATE TABLE `t_role` (
`id` int NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `t_user` (
`id` int NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `t_user_role` (
`role_id` int DEFAULT NULL,
`user_id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
@SpringBootTest
public class StudentTest {
@Autowired
UserMapper userMapper;
@Autowired
RoleMapper roleMapper;
@Test
public void get (){
List<Role> roles = userMapper.selectOneWithRelationsById(2).getRoles();
List<User> users = roleMapper.selectOneWithRelationsById(2).getUsers();
System.out.println(roles.size());
System.out.println(users.size());
}
}
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2cd3fc29] was not registered for synchronization because synchronization is not active
2023-08-29 13:57:47.107 INFO 16112 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-08-29 13:57:47.336 INFO 16112 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@669c2b07
2023-08-29 13:57:47.338 INFO 16112 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
==> Preparing: SELECT * FROM `t_user` WHERE `id` = ?
==> Parameters: 2(Integer)
<== Columns: id, name, username, password, sex, phone, code, address
<== Row: 2, 2, 2, 2, 2, 2, 2, 2
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2cd3fc29]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6475e778] was not registered for synchronization because synchronization is not active
==> Preparing: SELECT * FROM `t_user_role` WHERE user_id = ?
==> Parameters: 2(Integer)
<== Columns: role_id, user_id
<== Row: 1, 2
<== Row: 2, 2
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6475e778]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@fd5689d] was not registered for synchronization because synchronization is not active
==> Preparing: SELECT * FROM `t_role` WHERE `id` = ?
==> Parameters: 2(Integer)
<== Columns: id, name
<== Row: 2, 2
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@fd5689d]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@218f2f51] was not registered for synchronization because synchronization is not active
==> Preparing: SELECT * FROM `t_user_role` WHERE role_id = ?
==> Parameters: 2(Integer)
<== Columns: role_id, user_id
<== Row: 2, 1
<== Row: 2, 2
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@218f2f51]
0
0
预期结果多对多关系查询后可以获取到关联数据
正确结果
实际上 开启 sql 打印后,多对多关联查询,无法获取到关联数据
实际结果
user_role 关联表
user 表
role 表
查询结果
1.6.1