Mybatis ResultType如何处理返回类型
作者:JoneClassMate
这篇文章主要介绍了Mybatis ResultType如何处理返回类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
1. 使用resultType返回List<T>
Mapper.xml
<select id="resultType2" resultType="String"> select book_name from tb_book </select>
service层
List<String> resultType2();
实现类
//创建对象 private BookMapper bookMapper; /** * 给bookMapper set,get方法 * @return */ public BookMapper getBookMapper() { return bookMapper; } public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public List<String> resultType2() { return bookMapper.resultType2(); }
使用junit测试
package com.jmh.mybatis.service.impl; import com.jmh.mybatis.mapper.BookMapper; import com.jmh.mybatis.model.Book; import com.jmh.mybatis.model.BookVo; import com.jmh.mybatis.service.IBookService; import com.jmh.mybatis.utils.MybatisSqlSessionFactory; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; import java.util.Map; import java.util.Set; import static org.junit.Assert.*; public class DemoTest { //获取对象 private Book book; private IBookService iBookService; private SqlSession sqlSession; @Before public void setUp() throws Exception { book=new Book(); sqlSession = MybatisSqlSessionFactory.getSqlsession(); BookMapper mapper = sqlSession.getMapper(BookMapper.class); BookServiceImpl iml=new BookServiceImpl(); iml.setBookMapper(mapper); iBookService= iml; } @After public void tearDown() throws Exception { sqlSession.commit(); MybatisSqlSessionFactory.closeSqlsession(); } @Test public void resultType2() { List<String> books = iBookService.resultType2(); for (String b : books) { System.out.println(b); } } }
测试如下
2. 使用resultType返回单个对象
Mapper.xml
<select id="resultType3" resultType="com.jmh.mybatis.model.Book"> select <include refid="Base_Column_List" /> from tb_book where book_id=306 </select>
service层
Book resultType3();
实现类
//创建对象 private BookMapper bookMapper; /** * 给bookMapper set,get方法 * @return */ public BookMapper getBookMapper() { return bookMapper; } public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public Book resultType3() { return bookMapper.resultType3(); }
使用junit测试
package com.jmh.mybatis.service.impl; import com.jmh.mybatis.mapper.BookMapper; import com.jmh.mybatis.model.Book; import com.jmh.mybatis.model.BookVo; import com.jmh.mybatis.service.IBookService; import com.jmh.mybatis.utils.MybatisSqlSessionFactory; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; import java.util.Map; import java.util.Set; import static org.junit.Assert.*; public class DemoTest { //获取对象 private Book book; private IBookService iBookService; private SqlSession sqlSession; @Before public void setUp() throws Exception { book=new Book(); sqlSession = MybatisSqlSessionFactory.getSqlsession(); BookMapper mapper = sqlSession.getMapper(BookMapper.class); BookServiceImpl iml=new BookServiceImpl(); iml.setBookMapper(mapper); iBookService= iml; } @After public void tearDown() throws Exception { sqlSession.commit(); MybatisSqlSessionFactory.closeSqlsession(); } @Test public void resultType3() { Book book = iBookService.resultType3(); System.out.println(book); } }
3. 使用resultType返回List<Map>,适用于多表查询返回结果集
mapper接口
需要使用注解才能使用map其实这个主要功能也不大 将就着用吧
@MapKey("id") List<Map<String,Object>> resultType4();
mapper.xml
<select id="resultType4" resultType="java.util.Map"> select book_id,price,book_name from tb_book </select>
service层
List<Map<String,Object>> resultType4();
实现类
//创建对象 private BookMapper bookMapper; /** * 给bookMapper set,get方法 * @return */ public BookMapper getBookMapper() { return bookMapper; } public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public List<Map<String, Object>> resultType4() { return bookMapper.resultType4(); }
使用junit测试
package com.jmh.mybatis.service.impl; import com.jmh.mybatis.mapper.BookMapper; import com.jmh.mybatis.model.Book; import com.jmh.mybatis.model.BookVo; import com.jmh.mybatis.service.IBookService; import com.jmh.mybatis.utils.MybatisSqlSessionFactory; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; import java.util.Map; import java.util.Set; import static org.junit.Assert.*; public class DemoTest { //获取对象 private Book book; private IBookService iBookService; private SqlSession sqlSession; @Before public void setUp() throws Exception { book=new Book(); sqlSession = MybatisSqlSessionFactory.getSqlsession(); BookMapper mapper = sqlSession.getMapper(BookMapper.class); BookServiceImpl iml=new BookServiceImpl(); iml.setBookMapper(mapper); iBookService= iml; } @After public void tearDown() throws Exception { sqlSession.commit(); MybatisSqlSessionFactory.closeSqlsession(); } @Test public void resultType4() { List<Map<String, Object>> maps = iBookService.resultType4(); for (Map<String, Object> map : maps) { System.out.println(map); } } }
4. 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
mapper接口
@MapKey("id") Map<String,Object> resultType5();
mapper.xml
<select id="resultType5" resultType="java.util.Map"> select book_id,price from tb_book </select>
service层
Map<String,Object> resultType5();
实现类
//创建对象 private BookMapper bookMapper; /** * 给bookMapper set,get方法 * @return */ public BookMapper getBookMapper() { return bookMapper; } public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public Map<String, Object> resultType5() { return bookMapper.resultType5(); }
使用junit测试
package com.jmh.mybatis.service.impl; import com.jmh.mybatis.mapper.BookMapper; import com.jmh.mybatis.model.Book; import com.jmh.mybatis.model.BookVo; import com.jmh.mybatis.service.IBookService; import com.jmh.mybatis.utils.MybatisSqlSessionFactory; import org.apache.ibatis.annotations.MapKey; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.List; import java.util.Map; import java.util.Set; import static org.junit.Assert.*; public class DemoTest { //获取对象 private Book book; private IBookService iBookService; private SqlSession sqlSession; @Before public void setUp() throws Exception { book=new Book(); sqlSession = MybatisSqlSessionFactory.getSqlsession(); BookMapper mapper = sqlSession.getMapper(BookMapper.class); BookServiceImpl iml=new BookServiceImpl(); iml.setBookMapper(mapper); iBookService= iml; } @After public void tearDown() throws Exception { sqlSession.commit(); MybatisSqlSessionFactory.closeSqlsession(); } @Test public void resultType5() { Map<String, Object> map = iBookService.resultType5(); System.out.println(map); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。