标签:except 定义 app 标识 ltm person 类型 b16 传参
<!-- 根据id查询用户信息 --> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select>
<!-- 根据名称模糊查询用户信息 --> <select id="selectUserByName" parameterType="string" resultType="user"> select * from user where username like ‘%${value}%‘ </select>
//如果使用#{}占位符号则必须人为在传参数中加% List<User> list = userMapper.selectUserByName("%管理员%"); //如果使用${}原始符号则不用人为在参数中加% List<User>list = userMapper.selectUserByName("管理员");
<!—传递pojo对象综合查询用户信息 --> <select id="findUserByUser" parameterType="user" resultType="user"> select * from user where id=#{id} and username like ‘%${username}%‘ </select>
package com.lhx.mybatis.po; public class QueryVo { private User user; // 自定义用户扩展类 private UserCustom userCustom; private String ordercode; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } public String getOrdercode() { return ordercode; } public void setOrdercode(String ordercode) { this.ordercode = ordercode; } }
如果用户需要扩展
package com.lhx.mybatis.po; public class UserCustom extends User { private String phone; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
<select id="findUserList" parameterType="com.lhx.mybatis.po.QueryVo" resultType="com.lhx.mybatis.po.User"> select * from user where #{user.username} and sex=#{user.sex} </select>
select * from user where sex=#{user.sex} and username like ‘%${user.username}%‘
public List<User> findUserList(QueryVo queryVo) throws Exception;
<!-- 传递hashmap综合查询用户信息 -->
<select id="findUserByHashmap" parameterType="hashmap" resultType="com.lhx.mybatis.po.User">
select * from user where id=#{id} and username like ‘%${username}%‘
</select>
public void testFindUserByHashmap() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件Hashmap对象 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", 1); map.put("username", "管理员"); // 传递Hashmap对象查询用户列表 List<User> list = userMapper.findUserByHashmap(map); // 关闭session session.close(); }
<!-- 获取用户列表总数 --> <select id="findUserCount" parameterType="user" resultType="int"> select count(1) from user </select>
public int findUserCount(User user) throws Exception;
public void testFindUserCount() throws Exception{ //获取session SqlSession session = sqlSessionFactory.openSession(); //获取mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setUsername("管理员"); //传递Hashmap对象查询用户列表 int count = userMapper.findUserCount(user); //关闭session session.close(); }
<!-- 根据id查询用户信息 --> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select>
public User findUserById(int id) throws Exception;
public void testFindUserById() throws Exception { //获取session SqlSession session = sqlSessionFactory.openSession(); //获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); //通过mapper接口调用statement User user = userMapper.findUserById(1); System.out.println(user); //关闭session session.close(); }
<!-- 根据名称模糊查询用户信息 --> <select id="findUserByUsername" parameterType="string" resultType="user"> select * from user where username like ‘%${value}%‘ </select>
public List<User> findUserByUsername(String username) throws Exception;
测试:
public void testFindUserByUsername()throws Exception{ //获取session SqlSession session = sqlSessionFactory.openSession(); //获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); //如果使用占位符号则必须人为在传参数中加% //List<User> list = userMapper.selectUserByName("%管理员%"); //如果使用${}原始符号则不用人为在参数中加% List<User> list = userMapper.findUserByUsername("管理员"); //关闭session session.close(); }
<select id="findUserListResultMap" parameterType="com.lhx.mybatis.po.QueryVo" resultMap="userListMap"> select id id_,username username_ from user where #{user.username} and sex=#{user.sex} </select>
使用resultMap指定上边定义的personmap。
<resultMap type="com.lhx.mybatis.po.User" id="userListMap"> <id column="id_" property="id"></id> <result column="username_" property="username"></result> </resultMap>
<id />:此属性表示查询结果集的唯一标识,非常重要。如果是多个字段为复合唯一约束则定义多个<id />。
Property:表示类的属性。
Column:表示sql查询出来的字段名。
Column和property放在一块儿表示将sql查询出来的字段映射到指定的pojo类属性上。
<result />:普通结果,即pojo的属性。
public List<User> findUserListResultMap(QueryVo queryVo) throws Exception;
public void testFindUserListResultMap() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); QueryVo queryVo = new QueryVo(); User user = new User(); user.setUsername("张"); user.setSex("女"); queryVo.setUser(user); // 传递Hashmap对象查询用户列表 List<User> list = userMapper.findUserListResultMap(queryVo); // 关闭session session.close(); }
小结
java-mybaits-00401-Mapper-输入输出
标签:except 定义 app 标识 ltm person 类型 b16 传参
原文地址:http://www.cnblogs.com/bjlhx/p/6820593.html