Home >Java >javaTutorial >Basics of getting started with mybatis (4) ---- input mapping and output mapping

Basics of getting started with mybatis (4) ---- input mapping and output mapping

黄舟
黄舟Original
2016-12-21 14:31:031280browse

MyBatis Getting Started Basics (4)----Input Mapping and Output Mapping 1: Input Mapping

Specify the type of input parameters through parameterType. The type can be a simple type, hashmap, or pojo packaging type.

1.1. Pass the packaging object of pojo

 1.1.1. Requirement description

  To complete the comprehensive query of user information, the query conditions that need to be passed in may be complex (may include user information, other information, such as products, orders, etc. wait).

 1.1.2. Define the packaging type pojo

   Based on the above requirements, it is recommended to use a custom packaging type pojo, and wrap complex query conditions in the packaging type pojo.

Pojo class UserQueryVo class code that wraps query conditions:

1 package com.mybatis.entity; 2 3 /**4 * 5 * @ClassName: UserQueryVo 6 * @Description: TODO (package type) 7 * @author warcaft 8 * @date 2015-6-30 12:00:41 AM 9 *10*/11 public class UserQueryVo {12 //Here to wrap other query conditions 13 // User query conditions 14 PRivate UserCustom userCustom;15 16 public UserCustom getUserCustom() {17 return userCustom;18 }19 20 public void setUserCustom(UserCustom userCustom) {21 this.userCustom = userCustom;22 }23 //Other query conditions can be packaged. .....24 25 }
View Code

Code of UserCustom class

1 package com.mybatis.entity; 2 /**3 * 4 * @ClassName: UserCustom 5 * @Description: TODO (user's extension class) 6 * @author warcaft 7 * @date 2015-6-30 12:02:37 AM 8 * 9*/10 public class UserCustom extends User{11 //Can extend the user's Information 12}
View Code

Code of UserMapper.xml

Define comprehensive query of user information in UserMapper.xml (assuming that the query conditions are complex, complex related queries are performed through advanced queries)

1 5
View Code

UserMapper.java class code

1 /**2 * 3 * @ClassName: UserMapper 4 * @Description: TODO (user management mapper interface) 5 * @author warcaft 6 * @date 2015-6-30 12:14:56 AM 7 * 8*/ 9 public interface UserMapper {10 // Comprehensive query of user information 11 public List findUserList(UserQueryVo userQueryVo);12 }
View Code

Junit unit test code

1 ackage com.mybatis.dao.test; 2  3 import java.io.InputStream; 4 import java.util.Date; 5 import java.util.List; 6  7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory;10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;11 import org.junit.Before;12 import org.junit.Test;13 14 import com.mybatis.entity.User;15 import com.mybatis.entity.UserCustom;16 import com.mybatis.entity.UserQueryVo;17 import com.mybatis.mapper.UserMapper;18 19 public class UserMapperTest {20 21     private SqlSessionFactory sqlSessionFactory;22 23     // 此方法是在执行findUserByIdTest之前执行24     @Before25     public void setUp() throws Exception {26         String resource = "SqlMapConfig.xml";27         InputStream inputStream = Resources.getResourceAsStream(resource);28         // 创建SqlSessionFcatory29         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);30     }31     32     @Test33     public void testFindUserList() {34         SqlSession sqlSession = sqlSessionFactory.openSession();35         //创造查询条件36         UserQueryVo userQueryVo = new UserQueryVo();37         UserCustom userCustom = new UserCustom();38         userCustom.setSex("2");39         userCustom.setUsername("小");40         userQueryVo.setUserCustom(userCustom);41         // 创建Usermapper对象,mybatis自动生成mapper代理对象42         UserMapper mapper = sqlSession.getMapper(UserMapper.class);43         Listlist=mapper.findUserList(userQueryVo);44         System.out.println(list);45         sqlSession.commit();46         sqlSession.close();47     }48 }
View Code二:输出映射

1.resultType

  使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。

  如果查询出来的列名和pojo中的属性名全部不一致,没有创建pojo对象。

  只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

1.1.输出简单类型

  需求:用户信息的综合查询列表,通过查询总数才能实现分页功能。‘

  UserMapper.xml的代码 

1 2 5 8 12 13     16

UserMapper.java的代码

1 public interface UserMapper {2     //用户信息综合查询总数3     public int findUserCount(UserQueryVo userQueryVo);4 }

Junit测试代码

1 package com.mybatis.dao.test; 2  3 import java.io.InputStream; 4 import java.util.Date; 5 import java.util.List; 6  7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory;10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;11 import org.junit.Before;12 import org.junit.Test;13 14 import com.mybatis.entity.User;15 import com.mybatis.entity.UserCustom;16 import com.mybatis.entity.UserQueryVo;17 import com.mybatis.mapper.UserMapper;18 19 public class UserMapperTest {20 21     private SqlSessionFactory sqlSessionFactory;22 23     // 此方法是在执行findUserByIdTest之前执行24     @Before25     public void setUp() throws Exception {26         String resource = "SqlMapConfig.xml";27         InputStream inputStream = Resources.getResourceAsStream(resource);28         // 创建SqlSessionFcatory29         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);30     }31     32     @Test33     public void findUserCountTest() {34         SqlSession sqlSession = sqlSessionFactory.openSession();35         //创造查询条件36         UserQueryVo userQueryVo = new UserQueryVo();37         UserCustom userCustom = new UserCustom();38         userCustom.setSex("2");39         userCustom.setUsername("小");40         userQueryVo.setUserCustom(userCustom);41         // 创建Usermapper对象,mybatis自动生成mapper代理对象42         UserMapper mapper = sqlSession.getMapper(UserMapper.class);43         int count=mapper.findUserCount(userQueryVo);44         System.out.println(count);45         sqlSession.commit();46         sqlSession.close();47     }48 }
View Code

小结:查询出来的结果集只有一行一列,可以使用简单类型进行输出映射。

1.2.输出pojo对象和pojo列表  

  不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resultType指定的类型是一样的。

  在mapper.java指定的方法返回值类型不一样。

 1.2.1.输出单个pojo对象,方法返回值是单个对象类型

1 public interface UserMapper {2     /**Query user information based on ID*/3     public User findUserById(int id);4 }

 1.2.2.输出pojo对象list,方法返回值是List

1 public interface UserMapper {2     /**Fuzzy query of user information based on user name*/3     public List findUserByName(String username);4 }

小结:生成的动态代理对象中是根据mapper.java方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList(返回集合对象调用)

1.3.输出类型resultMap

    mybatis中使用resultMap完成高级输出结果映射。

 1.3.1.resultMap使用方法

    (1).定义resultMap

    (2).使用resultMap作为statement的输出映射类型

 1.3.2.demo例子

  (1).需求:将下面的sql使用User完成映射

    select id id_,username username_ from t_user where id=?

    User类中属性名和上边的列名不一致。

  (2).定义resultMap   

1 2 5 8 12 13 18 19 30 31


   (3). Use resultMap as the output type mapping of statement

1 4

UserMapper.java code

public interface UserMapper { /**Query user information based on ID and use resultMap for output*/ public User findUserByIdResultMap(int id);}

Junit test code :

1 package com.mybatis.dao.test; 2 3 import java.io.InputStream; 4 import java.util.Date; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources ; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory;10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;11 import org.junit.Before;12 import org.junit .Test;13 14 import com.mybatis.entity.User;15 import com.mybatis.entity.UserCustom;16 import com.mybatis.entity.UserQueryVo;17 import com.mybatis.mapper.UserMapper;18 19 public class UserMapperTest { 20 21 private SqlSessionFactory sqlSessionFactory;22 23 // This method is executed before findUserByIdTest 24 @Before25 public void setUp() throws Exception {26 String resource = "SqlMapConfig.xml";27 InputStream input Stream = Resources.getResourceAsStream(resource); " SqlSession sqlSession = sqlSessionFactory.openSession();35 // Create Usermapper object, mybatis automatically generates mapper Proxy object36 UserMapper mapper = sqlSession.getMapper(UserMapper.class);37 User user = mapper.findUserByIdResultMap(1);38 System.out.println(user);39 sqlSession.close();40 }4 1 }
View Code

Summary:

Use resultType for output mapping. Only when the queried column name is consistent with the attribute name in the pojo can the column be mapped successfully.

 If the queried column name is inconsistent with the pojo attribute name, define a resultMap to make a mapping relationship between the column name and the pojo attribute name.

The above is the basics of getting started with mybatis (4) - the content of input mapping and output mapping. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn