MaBatis(5)输入/输出映射
public class UserCustomer extends User{ }
在UserView.java中:
//在这里包装所需要的查询条件 //用户综合查询查询条件 private UserCustomer userCustomer; public UserCustomer getUserCustomer() { return userCustomer; } public void setUserCustomer(UserCustomer userCustomer) { this.userCustomer = userCustomer; }
//综合查询 public List<User> findBySelect(UserView userView) throws Exception;
在UserMapper.xml文件中实现查询的代码:
<!-- 综合查询 -->
<select id="findBySelect" parameterType="com.MrChengs.po.UserView" resultType="com.MrChengs.po.UserCustomer" >
select * from user where user.id=#{userCustomer.id} and user.username like '%${userCustomer.username}%'
</select>
//综合查询 @Test public void testfindBySelect() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView(); UserCustomer userCustomer = new UserCustomer(); userCustomer.setId(16); userCustomer.setUsername("小明"); userView.setUserCustomer(userCustomer); List<User> user = mapper.findBySelect(userView); for(User u : user){ System.out.println(u); } sqlSession.close(); }
查询成功:
DEBUG [main] - ==> Preparing: select * from user where user.id=? and user.username like '%小明%' DEBUG [main] - ==> Parameters: 16(Integer) DEBUG [main] - <== Total: 1 User [id=16, username=张小明, birthday=null, sex=1, address=河南郑州]
生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象调用)还是selectList (返回集合对象调用 ).
//擦寻用户信息总数 public int findUserCount(UserView userView) throws Exception;
在UserMapper.xml文件中:
<!-- 输出映射查询用户信息总数 -->
<select id="findUserCount" parameterType="com.MrChengs.po.UserView" resultType="int">
select count(*) From user where sex=#{userCustomer.sex} and user.username like '%${userCustomer.username}%'
</select>
//查询用户信息总数 @Test public void testfindUserCount() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); UserView userView = new UserView(); UserCustomer userCustomer = new UserCustomer(); userCustomer.setSex(1); userCustomer.setUsername("小明"); userView.setUserCustomer(userCustomer); int count = mapper.findUserCount(userView); System.out.println(count); sqlSession.close(); }
结果:
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 963522361. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@396e2f39] DEBUG [main] - ==> Preparing: select count(*) From user where sex=? and user.username like '%小明%' DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1 3 DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@396e2f39]
//测试resultMap public User findByMap(int id) throws Exception;
在UserMapper.xml中:
<!-- 简单测试resultMap的使用 --> <!-- type:resultMap最终映射的java 对象类型,可以使用别名/全类名 --> <!-- id:使当前resultMap的唯一标识 --> <resultMap type="com.MrChengs.po.User" id="ByMap"> <!-- id标识查询结果的唯一标识 --> <!-- column:查询出来的列名 --> <!-- property:type指定类型的pojo类型的映射属性,最终resultMap对cloumn和property做出一对一的映射 --> <id column="_id" property="id"/> <!-- result:对普通列名的映射 --> <!-- column:查询出来的列名 --> <!-- property:和type的类型一一映射 --> <result column="_username" property="username"/> </resultMap> <select id="findByMap" parameterType="int" resultMap="ByMap"> select id _id,username _username from user where id=#{id} </select>
//测试resultMap @Test public void testfindByMap() throws Exception{ SqlSession sqlSession = getSqlSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.findByMap(1); System.out.println(user); sqlSession.close(); }
DEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 2050835901. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a3d45bd] DEBUG [main] - ==> Preparing: select id _id,username _username from user where id=? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1 User [id=1, username=王五, birthday=null, sex=0, address=null]