Mybatis框架-resultMap元素的自动映射级别

resultMap的自动映射级别:分为三种:NONE  PARTIAL  FULL

其中默认的属性是:PARTIAL:开启自动匹配,会自动匹配数据库中的字段名和实体类中的属性名,如果一致,就能匹配上,如果不一致,就不能进行匹配

mybatis-config.xml

 

 UserMapper.xml

 

 UserMapper.java

 

编写测试方法:

 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();
11             //使用mapper映射的方式实现
12             //userList2 = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserListByUserName",userNameString);
13             //调用mapper接口的方式实现
14             userList2 = sqlSession.getMapper(UserMapper.class).getUserListByUserName5(map);
15             int size = userList2.size();
16             mlogger.info("获取到的记录数是:" + size);
17 
18         } catch (Exception e) {
19             // TODO: handle exception
20         } finally {
21             // 最后一定要注意:关闭会话
22             MyBatisUtil.closeSqlSession(sqlSession);
23 
24         }
25         for (User user2 : userList2) {
26             mlogger.info("用户名:" + user2.getUserName() + ",密码:" + user2.getUserPassword()+",用户角色:"+user2.getUserRoleName()+"地址:"+user2.getAddress());
27         }
28 
29     }

运行结果:

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 [DEBUG] 2019-11-04 22:27:40,099 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@3caa4b]
6 [DEBUG] 2019-11-04 22:27:40,099 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 3975755 to pool.
7 [INFO] 2019-11-04 22:27:40,099 cn.smbms.dao.test.UserMapperTest - 用户名:赵燕,密码:0000000,用户角色:普通员工地址:北京市海淀区回龙观小区10号楼

如果不想进行自动匹配,只需设置autoMappingBehavior的属性值为NONE,就把自动匹配的功能关掉了

mybatis-config.xml

 

UserMapper.xml

 

 UserMapper.java

 

 编写测试方法:

 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();
11             //使用mapper映射的方式实现
12             //userList2 = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserListByUserName",userNameString);
13             //调用mapper接口的方式实现
14             userList2 = sqlSession.getMapper(UserMapper.class).getUserListByUserName5(map);
15             int size = userList2.size();
16             mlogger.info("获取到的记录数是:" + size);
17 
18         } catch (Exception e) {
19             // TODO: handle exception
20         } finally {
21             // 最后一定要注意:关闭会话
22             MyBatisUtil.closeSqlSession(sqlSession);
23 
24         }
25         for (User user2 : userList2) {
26             mlogger.info("用户名:" + user2.getUserName() + ",密码:" + user2.getUserPassword()+",用户角色:"+user2.getUserRoleName()+"地址:"+user2.getAddress());
27         }
28 
29     }

运行结果:

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 [DEBUG] 2019-11-04 22:36:38,742 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@ed0220c]
6 [DEBUG] 2019-11-04 22:36:38,742 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 248521228 to pool.
7 [INFO] 2019-11-04 22:36:38,752 cn.smbms.dao.test.UserMapperTest - 用户名:赵燕,密码:null,用户角色:普通员工地址:null

posted on 2019-11-04 22:41  ~码铃薯~  阅读(3188)  评论(0编辑  收藏  举报

导航