目录
1. 使用resultType返回List
2.使用resultType返回单个对象
3.使用resultType返回List,适用于多表查询返回结果集
4.使用resultType返回Map,适用于多表查询返回单个结果集,object>
1. 使用resultType返回List<T>
Mapper.xml<select id="resultType2" resultType="String">selectbook_namefrom 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;}@Overridepublic 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;@Beforepublic 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;}@Afterpublic void tearDown() throws Exception {mit();MybatisSqlSessionFactory.closeSqlsession();}@Testpublic 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;}@Overridepublic 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;@Beforepublic 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;}@Afterpublic void tearDown() throws Exception {mit();MybatisSqlSessionFactory.closeSqlsession();}@Testpublic 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;}@Overridepublic 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;@Beforepublic 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;}@Afterpublic void tearDown() throws Exception {mit();MybatisSqlSessionFactory.closeSqlsession();}@Testpublic 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;}@Overridepublic 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;@Beforepublic 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;}@Afterpublic void tearDown() throws Exception {mit();MybatisSqlSessionFactory.closeSqlsession();}@Testpublic void resultType5() {Map<String, Object> map = iBookService.resultType5();System.out.println(map);}}