MyBatis输入映射parameterType
案例:
1.User.java以及SqlMapConfig.xml参考MyBatis实现增删改查
2.UserCustom.java继承User.java
1 package com.test.entity; 2 /** 3 * 4 * @author Mr 5 * 属于User的扩展类 6 */ 7 public class UserCustom extends User { 8 //可以添加一些属性 9 }
3.UserQueryVo.java类
1 package com.test.entity; 2 /** 3 * 这个是包装类 ,后面跟控制器有关系 4 * @author Mr 5 * 6 */ 7 public class UserQueryVo { 8 //用户的查询条件 9 private User user; 10 //附加的查询条件 11 private UserCustom userCustom; 12 13 public User getUser() { 14 return user; 15 } 16 17 public void setUser(User user) { 18 this.user = user; 19 } 20 21 public UserCustom getUserCustom() { 22 return userCustom; 23 } 24 25 public void setUserCustom(UserCustom userCustom) { 26 this.userCustom = userCustom; 27 } 28 29 }
4.UserMapper.java
1 package com.test.dao; 2 3 import java.util.List; 4 5 import com.test.entity.User; 6 import com.test.entity.UserCustom; 7 import com.test.entity.UserQueryVo; 8 /** 9 * 10 * @author Mr 11 * Mapper接口代理模式开发命名一定是:实体类名+Mapper 12 * 13 * 使用mapper开发步骤: 14 * 1.编写接口和mapper.xml要遵循4个指标 15 * 2.SqlMapConfig.xml中加载UserMapper.xml 16 */ 17 public interface UserMapper { 18 //根据用户id查询用户 19 public User findUserById(int id); 20 //添加用户 21 public void addUser(User user); 22 //这个方法对应的是包装类型的statement 23 public List<UserCustom> findUserList(UserQueryVo userQueryVo); 24 }
5.UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- namespace命名空间是UserMapper接口的全类名--> 6 <mapper namespace="com.test.dao.UserMapper"> 7 <!-- 查询 注意了 type后面是包装类--> 8 <select id="findUserList" parameterType="com.test.entity.UserQueryVo" resultType="com.test.entity.UserCustom"> 9 select * from userss WHERE sex=#{userCustom.sex} AND username LIKE '%${userCustom.username}%' 10 </select> 11 12 13 </mapper>
6.测试类
1 package com.test.demo; 2 3 import java.io.IOException; 4 import java.io.InputStream; 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 12 import com.test.dao.UserMapper; 13 import com.test.entity.UserCustom; 14 import com.test.entity.UserQueryVo; 15 16 /** 17 * 测试包装类型的pojo 18 * @author Mr 19 * 20 */ 21 public class Test4 { 22 23 public static void main(String[] args) { 24 try { 25 //mybatis核心文件 26 String resource = "SqlMapConfig.xml"; 27 //读取配置文件 28 InputStream config = Resources.getResourceAsStream(resource); 29 //获取工厂 30 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); 31 //获取数据库操作对象 32 SqlSession sqlSession = sqlSessionFactory.openSession(); 33 //获取实现类对象 34 UserMapper um = sqlSession.getMapper(UserMapper.class); 35 36 UserQueryVo userQueryVo = new UserQueryVo(); 37 //参数是userCustom的 38 UserCustom userCustom = new UserCustom(); 39 userCustom.setSex("1"); 40 userCustom.setUsername("张"); 41 //他们俩存在关系 42 userQueryVo.setUserCustom(userCustom); 43 44 List<UserCustom> list = um.findUserList(userQueryVo); 45 for (UserCustom uu : list) { 46 System.out.println(uu); 47 } 48 49 50 } catch (IOException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 55 56 } 57 58 }