目录
3. 使用resultType返回List,适用于多表查询返回结果集
4. 使用resultType返回Map,适用于多表查询返回单个结果集,object>
- <select id="resultType2" resultType="String">
- select
- book_name
- from tb_book
- select>
List resultType2();
- //创建对象
- private BookMapper bookMapper;
-
-
- /**
- * 给bookMapper set,get方法
- * @return
- */
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
-
- @Override
- public List
resultType2() { - return bookMapper.resultType2();
- }
- 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
books = iBookService.resultType2(); - for (String b : books) {
- System.out.println(b);
- }
- }
-
- }
- <select id="resultType3" resultType="com.jmh.mybatis.model.Book">
- select
- <include refid="Base_Column_List" />
- from tb_book where book_id=306
- select>
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();
- }
- 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);
- }
-
-
-
- }
需要使用注解才能使用map其实这个主要功能也不大 将就着用吧
- @MapKey("id")
- List
- <select id="resultType4" resultType="java.util.Map">
- select book_id,price,book_name from tb_book
- select>
List
- //创建对象
- private BookMapper bookMapper;
-
-
- /**
- * 给bookMapper set,get方法
- * @return
- */
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
- @Override
- public List
- return bookMapper.resultType4();
- }
- 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
- for (Map
map : maps) { - System.out.println(map);
- }
- }
-
- }
- @MapKey("id")
- Map
resultType5();
-
- select book_id,price from tb_book
-
Map resultType5();
- //创建对象
- private BookMapper bookMapper;
-
-
- /**
- * 给bookMapper set,get方法
- * @return
- */
- public BookMapper getBookMapper() {
- return bookMapper;
- }
-
- public void setBookMapper(BookMapper bookMapper) {
- this.bookMapper = bookMapper;
- }
-
- @Override
- public Map
resultType5() { - return bookMapper.resultType5();
- }
- 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
map = iBookService.resultType5(); - System.out.println(map);
- }
- }