• Mybatis ResultType处理返回类型


    目录

     1. 使用resultType返回List

     2. 使用resultType返回单个对象

    3. 使用resultType返回List,适用于多表查询返回结果集 

     4. 使用resultType返回Map,适用于多表查询返回单个结果集,object>


     1. 使用resultType返回List

    • Mapper.xml 
    1. <select id="resultType2" resultType="String">
    2. select
    3. book_name
    4. from tb_book
    5. select>
    •  service层
    List resultType2();
    • 实现类
    1. //创建对象
    2. private BookMapper bookMapper;
    3. /**
    4. * 给bookMapper set,get方法
    5. * @return
    6. */
    7. public BookMapper getBookMapper() {
    8. return bookMapper;
    9. }
    10. public void setBookMapper(BookMapper bookMapper) {
    11. this.bookMapper = bookMapper;
    12. }
    13. @Override
    14. public List resultType2() {
    15. return bookMapper.resultType2();
    16. }
    •  使用junit测试
    1. package com.jmh.mybatis.service.impl;
    2. import com.jmh.mybatis.mapper.BookMapper;
    3. import com.jmh.mybatis.model.Book;
    4. import com.jmh.mybatis.model.BookVo;
    5. import com.jmh.mybatis.service.IBookService;
    6. import com.jmh.mybatis.utils.MybatisSqlSessionFactory;
    7. import org.apache.ibatis.annotations.MapKey;
    8. import org.apache.ibatis.session.SqlSession;
    9. import org.junit.After;
    10. import org.junit.Before;
    11. import org.junit.Test;
    12. import java.util.List;
    13. import java.util.Map;
    14. import java.util.Set;
    15. import static org.junit.Assert.*;
    16. public class DemoTest {
    17. //获取对象
    18. private Book book;
    19. private IBookService iBookService;
    20. private SqlSession sqlSession;
    21. @Before
    22. public void setUp() throws Exception {
    23. book=new Book();
    24. sqlSession = MybatisSqlSessionFactory.getSqlsession();
    25. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    26. BookServiceImpl iml=new BookServiceImpl();
    27. iml.setBookMapper(mapper);
    28. iBookService= iml;
    29. }
    30. @After
    31. public void tearDown() throws Exception {
    32. sqlSession.commit();
    33. MybatisSqlSessionFactory.closeSqlsession();
    34. }
    35. @Test
    36. public void resultType2() {
    37. List books = iBookService.resultType2();
    38. for (String b : books) {
    39. System.out.println(b);
    40. }
    41. }
    42. }
    •  测试如下

     

     2. 使用resultType返回单个对象

    • Mapper.xml 
    1. <select id="resultType3" resultType="com.jmh.mybatis.model.Book">
    2. select
    3. <include refid="Base_Column_List" />
    4. from tb_book where book_id=306
    5. select>
    •  service层
    Book resultType3();
    •  实现类
    1. //创建对象
    2. private BookMapper bookMapper;
    3. /**
    4. * 给bookMapper set,get方法
    5. * @return
    6. */
    7. public BookMapper getBookMapper() {
    8. return bookMapper;
    9. }
    10. public void setBookMapper(BookMapper bookMapper) {
    11. this.bookMapper = bookMapper;
    12. }
    13. @Override
    14. public Book resultType3() {
    15. return bookMapper.resultType3();
    16. }
    •  使用junit测试
    1. package com.jmh.mybatis.service.impl;
    2. import com.jmh.mybatis.mapper.BookMapper;
    3. import com.jmh.mybatis.model.Book;
    4. import com.jmh.mybatis.model.BookVo;
    5. import com.jmh.mybatis.service.IBookService;
    6. import com.jmh.mybatis.utils.MybatisSqlSessionFactory;
    7. import org.apache.ibatis.annotations.MapKey;
    8. import org.apache.ibatis.session.SqlSession;
    9. import org.junit.After;
    10. import org.junit.Before;
    11. import org.junit.Test;
    12. import java.util.List;
    13. import java.util.Map;
    14. import java.util.Set;
    15. import static org.junit.Assert.*;
    16. public class DemoTest {
    17. //获取对象
    18. private Book book;
    19. private IBookService iBookService;
    20. private SqlSession sqlSession;
    21. @Before
    22. public void setUp() throws Exception {
    23. book=new Book();
    24. sqlSession = MybatisSqlSessionFactory.getSqlsession();
    25. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    26. BookServiceImpl iml=new BookServiceImpl();
    27. iml.setBookMapper(mapper);
    28. iBookService= iml;
    29. }
    30. @After
    31. public void tearDown() throws Exception {
    32. sqlSession.commit();
    33. MybatisSqlSessionFactory.closeSqlsession();
    34. }
    35. @Test
    36. public void resultType3() {
    37. Book book = iBookService.resultType3();
    38. System.out.println(book);
    39. }
    40. }

    3. 使用resultType返回List,适用于多表查询返回结果集 

    • mapper接口 

        需要使用注解才能使用map其实这个主要功能也不大 将就着用吧

    1. @MapKey("id")
    2. List> resultType4();
    • mapper.xml 
    1. <select id="resultType4" resultType="java.util.Map">
    2. select book_id,price,book_name from tb_book
    3. select>
    •  service层
    List> resultType4();
    •  实现类
    1. //创建对象
    2. private BookMapper bookMapper;
    3. /**
    4. * 给bookMapper set,get方法
    5. * @return
    6. */
    7. public BookMapper getBookMapper() {
    8. return bookMapper;
    9. }
    10. public void setBookMapper(BookMapper bookMapper) {
    11. this.bookMapper = bookMapper;
    12. }
    13. @Override
    14. public List> resultType4() {
    15. return bookMapper.resultType4();
    16. }
    •  使用junit测试
    1. package com.jmh.mybatis.service.impl;
    2. import com.jmh.mybatis.mapper.BookMapper;
    3. import com.jmh.mybatis.model.Book;
    4. import com.jmh.mybatis.model.BookVo;
    5. import com.jmh.mybatis.service.IBookService;
    6. import com.jmh.mybatis.utils.MybatisSqlSessionFactory;
    7. import org.apache.ibatis.annotations.MapKey;
    8. import org.apache.ibatis.session.SqlSession;
    9. import org.junit.After;
    10. import org.junit.Before;
    11. import org.junit.Test;
    12. import java.util.List;
    13. import java.util.Map;
    14. import java.util.Set;
    15. import static org.junit.Assert.*;
    16. public class DemoTest {
    17. //获取对象
    18. private Book book;
    19. private IBookService iBookService;
    20. private SqlSession sqlSession;
    21. @Before
    22. public void setUp() throws Exception {
    23. book=new Book();
    24. sqlSession = MybatisSqlSessionFactory.getSqlsession();
    25. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    26. BookServiceImpl iml=new BookServiceImpl();
    27. iml.setBookMapper(mapper);
    28. iBookService= iml;
    29. }
    30. @After
    31. public void tearDown() throws Exception {
    32. sqlSession.commit();
    33. MybatisSqlSessionFactory.closeSqlsession();
    34. }
    35. @Test
    36. public void resultType4() {
    37. List> maps = iBookService.resultType4();
    38. for (Map map : maps) {
    39. System.out.println(map);
    40. }
    41. }
    42. }

     4. 使用resultType返回Map,适用于多表查询返回单个结果集

    •  mapper接口
    1. @MapKey("id")
    2. Map resultType5();
    • mapper.xml 
    •  service层
    Map resultType5();
    •  实现类
    1. //创建对象
    2. private BookMapper bookMapper;
    3. /**
    4. * 给bookMapper set,get方法
    5. * @return
    6. */
    7. public BookMapper getBookMapper() {
    8. return bookMapper;
    9. }
    10. public void setBookMapper(BookMapper bookMapper) {
    11. this.bookMapper = bookMapper;
    12. }
    13. @Override
    14. public Map resultType5() {
    15. return bookMapper.resultType5();
    16. }
    •  使用junit测试
    1. package com.jmh.mybatis.service.impl;
    2. import com.jmh.mybatis.mapper.BookMapper;
    3. import com.jmh.mybatis.model.Book;
    4. import com.jmh.mybatis.model.BookVo;
    5. import com.jmh.mybatis.service.IBookService;
    6. import com.jmh.mybatis.utils.MybatisSqlSessionFactory;
    7. import org.apache.ibatis.annotations.MapKey;
    8. import org.apache.ibatis.session.SqlSession;
    9. import org.junit.After;
    10. import org.junit.Before;
    11. import org.junit.Test;
    12. import java.util.List;
    13. import java.util.Map;
    14. import java.util.Set;
    15. import static org.junit.Assert.*;
    16. public class DemoTest {
    17. //获取对象
    18. private Book book;
    19. private IBookService iBookService;
    20. private SqlSession sqlSession;
    21. @Before
    22. public void setUp() throws Exception {
    23. book=new Book();
    24. sqlSession = MybatisSqlSessionFactory.getSqlsession();
    25. BookMapper mapper = sqlSession.getMapper(BookMapper.class);
    26. BookServiceImpl iml=new BookServiceImpl();
    27. iml.setBookMapper(mapper);
    28. iBookService= iml;
    29. }
    30. @After
    31. public void tearDown() throws Exception {
    32. sqlSession.commit();
    33. MybatisSqlSessionFactory.closeSqlsession();
    34. }
    35. @Test
    36. public void resultType5() {
    37. Map map = iBookService.resultType5();
    38. System.out.println(map);
    39. }
    40. }

     


     

  • 相关阅读:
    corn表达式 具体详解与案例
    【开源库推荐】go-linq 强大的语言集成查询库如,ORM一般丝滑处理内存数据
    spring boot configuration annotation processor notconfigured解决方法
    【wordPress】WordPress删除index.php后缀【亲测有效】(手把手教学)
    一文解读功率放大器(功率放大器如何选型)
    大模型算法(一):从Transformer到ViT再到LLaMA
    2022鹏城杯web
    5G试题_1
    Swift--字符、字符串与集合类型
    单机运行多个独立浏览器
  • 原文地址:https://blog.csdn.net/m0_63300795/article/details/126204931