Automatic map level Mybatis framework -resultMap elements

resultMap automatic map level: divided into three types: NONE PARTIAL FULL

Where the default attributes are: PARTIAL: enable automatic matching will automatically match the attribute names in the database field names and entities class, if agreed, will be able to match, if not, can not be matched

mybatis-config.xml

 

 UserMapper.xml

 

 UserMapper.java

 

Writing Test Methods:

 1 @Test
 2     public void test9() {
 3         Map<String, String> map = new HashMap<String, String>();
 4         map.put("userName1", "赵");
 5         map.put("userRole1", "3");
 6     
 7         SqlSession sqlSession = null;
 8         java.util.List<User> userList2 = new ArrayList<User>();
 9         try {
10             sqlSession = MyBatisUtil.createSqlSession();
12use mapper mapping ways to achieve
//11                          // userList2 = sqlSession.selectList ( "cn.smbms.dao.user.UserMapper.getUserListByUserName", userNameString);
 13 is              // call interface manner mapper 
14              userList2 = sqlSession.getMapper (UserMapper. Class ) .getUserListByUserName5 (Map) ;
 15              int size = userList2.size ();
 16              mlogger.info ( "number of records are acquired:" + size);
 . 17  
18 is          } the catch (Exception E) {
 . 19              // the TODO: Exception handle 
20 is          } the finally {
 21 is              // Finally, we must pay attention: Closing session 
22             MyBatisUtil.closeSqlSession (SQLSESSION);
 23 is  
24          }
 25          for (the User user2: userList2) {
 26 is              mlogger.info ( "User Name:" + user2.getUserName () + " , Password:" + user2.getUserPassword () + " , user role: "+ user2.getUserRoleName () +" address: "+ user2.getAddress ());
 27          }
 28  
29      }

operation result:

1 [DEBUG] 2019-11-04 22:27:39,810 cn.smbms.dao.user.UserMapper.getUserListByUserName5 - ==>  Preparing: select a.*,r.roleName from smbms_user a,smbms_role r where username like CONCAT ('%',?,'%') and userRole = ? and a.userRole=r.id 
2 [DEBUG] 2019-11-04 22:27:39,990 cn.smbms.dao.user.UserMapper.getUserListByUserName5 - ==> Parameters: 赵(String), 3(String)
3 [INFO] 2019-11-04 22:27:40,099 cn.smbms.dao.test.UserMapperTest - 获取到的记录数是:1
4 [DEBUG] 2019-11-04 22:27:40,099 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3caa4b]
5[The DEBUG] 2019-11-04 22 is: 27: 40,099 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing the JDBC Connection [com.mysql.jdbc.JDBC4Connection@3caa4b]
 . 6 [the DEBUG] 2019-11-04 22 is: 27: 40,099 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned Connection 3,975,755 to the pool.
 7 [INFO] 2019-11-04 22: 27: 40,099 cn.smbms.dao.test.UserMapperTest - username: Zhao Yan password: 0000000, user roles: the general staff address: Haidian District, Beijing Huilongguan community Building 10

If you do not want automatic matching, simply set autoMappingBehavior NONE attribute value, put the automatic matching function turned off

mybatis-config.xml

 

UserMapper.xml

 

 UserMapper.java

 

 Writing Test Methods:

 1 @Test
 2     public void test9() {
 3         Map<String, String> map = new HashMap<String, String>();
 4         map.put("userName1", "赵");
 5         map.put("userRole1", "3");
 6     
 7         SqlSession sqlSession = null;
 8         java.util.List<User> userList2 = new ArrayList<User>();
 9         try {
10             sqlSession = MyBatisUtil.createSqlSession();
12use mapper mapping ways to achieve
//11                          // userList2 = sqlSession.selectList ( "cn.smbms.dao.user.UserMapper.getUserListByUserName", userNameString);
 13 is              // call interface manner mapper 
14              userList2 = sqlSession.getMapper (UserMapper. Class ) .getUserListByUserName5 (Map) ;
 15              int size = userList2.size ();
 16              mlogger.info ( "number of records are acquired:" + size);
 . 17  
18 is          } the catch (Exception E) {
 . 19              // the TODO: Exception handle 
20 is          } the finally {
 21 is              // Finally, we must pay attention: Closing session 
22             MyBatisUtil.closeSqlSession (SQLSESSION);
 23 is  
24          }
 25          for (the User user2: userList2) {
 26 is              mlogger.info ( "User Name:" + user2.getUserName () + " , Password:" + user2.getUserPassword () + " , user role: "+ user2.getUserRoleName () +" address: "+ user2.getAddress ());
 27          }
 28  
29      }

operation result:

1 [DEBUG] 2019-11-04 22:36:38,393 cn.smbms.dao.user.UserMapper.getUserListByUserName5 - ==>  Preparing: select a.*,r.roleName from smbms_user a,smbms_role r where username like CONCAT ('%',?,'%') and userRole = ? and a.userRole=r.id 
2 [DEBUG] 2019-11-04 22:36:38,623 cn.smbms.dao.user.UserMapper.getUserListByUserName5 - ==> Parameters: 赵(String), 3(String)
3 [INFO] 2019-11-04 22:36:38,742 cn.smbms.dao.test.UserMapperTest - 获取到的记录数是:1
4 [DEBUG] 2019-11-04 22:36:38,742 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@ed0220c]
5[The DEBUG] 2019-11-04 22 is: 36: 38,742 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing the JDBC Connection [com.mysql.jdbc.JDBC4Connection@ed0220c]
 . 6 [the DEBUG] 2019-11-04 22 is: 36: 38,742 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned 248 521 228 Connection to the pool.
 . 7 [the INFO] 2019-11-04 22 is: 36: 38,752 cn.smbms.dao.test.UserMapperTest - username: Zhao Yan password: null , user roles: the general staff address: null

 

 

 

Guess you like

Origin www.cnblogs.com/dongyaotou/p/11795502.html