解决mybatis一对多查询问题时的只显示一条数据的问题(查询部门的同时把所属部门的员工信息查出来)关联查询
程序员文章站
2024-03-20 11:25:16
...
博客里的一些文章过于杂乱萌生了自己写一篇的想法
出现该错误的原因是两张表的主键名称一致导致的比如下面
两张表的主键名称都是id,解决方法就是写查询语句的的时候给主键id取别名就行了(我用的查询方式是关联查询,先查询在集中映射)
下面是我的项目框架:
我的maven依赖:
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- juint测试框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--log4j依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--slf4j依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.28</version>
</dependency>
</dependencies>
我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="MANAGED" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/jtsys?useSSL=true" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- <mappers> <mapper resource="mappers/UserMapper.xml" /> </mappers> -->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
<mapper resource="mappers/DeptMapper.xml"/>
</mappers>
</configuration>
User实体:
@Data //这个注解相当于setter/getter/toString
public class User {
private Integer id;
private String username;
private String password;
private String salt;
private Integer deptId;
private String email;
private String mobile;
private Integer valid=1;
private Date createdTime;
private Date modifiedTime;
private String modifiedUser;
private String createdUser;
// 查询用户的时候,将部门的查询结果映射到部门对象
private Dept dept;
}
Dept实体:
@Data
public class Dept {
private Integer id;
private String name;
private Integer parentId;
private Integer sort;
private String note;
private Date createdTime;
private Date modifiedTime;
private String createdUSer;
private String modifiedUser;
//查询部门的时候将用户的结果映射到User对象
private List<User> users;
}
DeptDao接口:
public interface DeptDao {
List<Dept> selectDeptUsers();
}
DeptMapper.xml文件:
<mapper namespace="com.cn.dao.DeptDao">
<select id="selectDeptUsers" resultMap="selectdepts">
SELECT d.*,
u.id as uid,**//这里是用户表主键给起了别名uid**
u.username,
u.password,
u.salt,
u.deptId,
u.email,
u.mobile,
u.valid,
u.createdTime,
u.modifiedTime,
u.modifiedUser,
u.createdUser
FROM sys_depts d
RIGHT JOIN
sys_users u ON d.id=u.deptId
</select>
<resultMap type="com.cn.entity.Dept" id="selectdepts">
<id property="id" column="id" />
//你如果想要部门表里的所有的数据,就要全部映射
<result property="name" column="name"/>
<result property="parentId" column="parentId"/>
<result property="sort" column="sort"/>
<result property="note" column="note"/>
<result property="createdTime" column="createdTime"/>
<result property="modifiedTime" column="modifiedTime"/>
<result property="createdUSer" column="createdUSer"/>
<result property="modifiedUser" column="modifiedUser"/>
<collection property="users" ofType="com.cn.entity.User">
// id这里用的起的别名uid
<id property="id" column="uid" />
//用户表只要id和用户名和邮箱和手机号
<result property="username" column="username" />
<result property="email" column="email" />
<result property="mobile" column="mobile" />
</collection>
</resultMap>
</mapper>
测试:
public class DeptTests {
public static SqlSessionFactory sqlSessionFactory;
public SqlSession sqlSession;
//添加UserMapper接口
public DeptMapper dp;
//通过SqlSessionFactoryBuilder构造SqlSessionFactory实例
@BeforeClass
public static void testBefore() throws IOException {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
// 每个单元测试方法(添加@Test注解的方法)在执行前,创建一个新的SqlSession实例,并获得UserMapper接口的动态代理对象
@Before
public void before() {
sqlSession = sqlSessionFactory.openSession();
dp = sqlSession.getMapper(DeptMapper.class);
}
@After
public void after() {
sqlSession.close();
}
@Test
public void selectUsersDept() {
List<Dept> deptsusers = dp.selectDeptUsers();
deptsusers.forEach(System.out::println);
}
}
控值台结果:
Dept(id=2, name=设计部, parentId=2, sort=1, note=设计, createdTime=Thu Apr 19 18:59:09 CST 2018, modifiedTime=Thu Apr 19 18:59:09 CST 2018, createdUSer=admin, modifiedUser=admin, users=[User(id=4, username=王五, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=6, username=王花, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=16, username=李华, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null)])
Dept(id=3, name=营销部, parentId=1, sort=2, note=营销, createdTime=Thu Apr 19 19:15:05 CST 2018, modifiedTime=Thu Apr 19 19:15:05 CST 2018, createdUSer=null, modifiedUser=null, users=[User(id=1, username=admin, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=3, username=李四, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=12, username=user-001, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=13, username=user-c, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null), User(id=15, username=user-b, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null)])
Dept(id=4, name=人事部, parentId=2, sort=1, note=管理, createdTime=Sun Apr 22 18:10:58 CST 2018, modifiedTime=Sun Apr 22 22:11:47 CST 2018, createdUSer=null, modifiedUser=null, users=[User(id=2, username=张三, password=null, salt=null, deptId=null, email=aaa@qq.com, mobile=null, valid=1, createdTime=null, modifiedTime=null, modifiedUser=null, createdUser=null, dept=null)])
打完收工!!!
还有一种分段查询的方式,其他不变只是代码不同,同样也能查出来在我下一篇博客
下一篇: 寒假宅喵java学习