mybatis三表联合查询测试类
时间: 2023-09-23 09:08:05 浏览: 108
在Mybatis中,进行多表联合查询可以使用多对一关联查询的方式。多对一关联查询的实现方式与一对一关联查询相同。在配置多对一关联时,需要在“多方”的映射文件中使用<association>属性关联“一方”。
以下是一个使用Mybatis进行三表联合查询的测试类范例:
```java
package com.example.test;
import com.example.dao.UserMapper;
import com.example.pojo.User;
import com.example.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class MybatisThreeTableQueryTest {
private SqlSession session;
private UserMapper userMapper;
@Before
public void setUp() throws Exception {
session = MybatisUtil.getConnection();
userMapper = session.getMapper(UserMapper.class);
}
@Test
public void testThreeTableQuery() {
User user = userMapper.getThreeTableQueryResult(1);
System.out.println(user);
}
@After
public void tearDown() throws Exception {
MybatisUtil.closeConnection();
}
}
```
在这个测试类中,首先获取到SqlSession对象,并通过该对象获取到UserMapper接口的实例。然后,在`testThreeTableQuery`方法中调用`getThreeTableQueryResult`方法进行三表联合查询,将查询结果打印出来。最后,在`tearDown`方法中关闭SqlSession对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [MyBatis 多表关联查询](https://blog.csdn.net/hh867308122/article/details/129005131)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Mybatis多表关联查询(一对多关联查询)](https://blog.csdn.net/qq_58258855/article/details/128647406)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文