1.动态sql
通过mybatis提供的各种标签方法实现动态拼接sql
如根据性别和名字查询用户。
User.xml
1 <!--根据姓名和名字查询 --> 2 <select id="selectUserBySexAndUserName" 3 parameterType="com.me.mybatis.pojo.User" 4 resultType="com.me.mybatis.pojo.User"> 5 select * from user 6 where 7 <if test="sex !=null and sex !=‘‘"> 8 sex = #{sex} 9 </if> 10 <if test="username !=null and username !=‘‘"> 11 and username =#{username} 12 </if> 13 </select>
UserMapper.java
1 package com.me.mybatis.mapper; 2 3 import java.util.List; 4 5 import com.me.mybatis.pojo.QueryVo; 6 import com.me.mybatis.pojo.User; 7 8 public interface UserMapper { 9 //遵循四个原则 10 //1.接口方法名==user.xml文件中的id名 11 //2.返回值类型与mapper.xml文件中的返回值类型要一致 12 //3.方法的入参类型与mapper.xml中入参的类型一致 13 //4.mapper.xml文件的namespace与接口绑定在一起 14 public User findUserById(Integer id); 15 public List<User> findUserByQueryVo(QueryVo vo); 16 public Integer countUsers(); 17 public List<User> selectUserBySexAndUserName(User user); 18 }
测试文件:
1 package com.me.mybatis.junit; 2 3 import java.io.InputStream; 4 import java.util.List; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.jupiter.api.Test; 11 12 import com.me.mybatis.mapper.OrderMapper; 13 import com.me.mybatis.mapper.UserMapper; 14 import com.me.mybatis.pojo.Orders; 15 import com.me.mybatis.pojo.QueryVo; 16 import com.me.mybatis.pojo.User; 17 18 public class MybatisMapperTest { 19 20 @Test 21 void testMapper() throws Exception { 22 // 加载核心配置文件 23 String resource = "SqlMapConfig.xml"; 24 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 25 // 创建sqlsessionFactory 26 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 27 // 创建sqlsession 28 SqlSession openSession = sqlSessionFactory.openSession(); 29 //sqlsession生成一个实现类 30 UserMapper mapper = openSession.getMapper(UserMapper.class); 31 //利用接口调方法 32 User user = mapper.findUserById(10); 33 System.out.println(user); 34 } 35 36 @Test 37 void testQueryVo() throws Exception { 38 // 加载核心配置文件 39 String resource = "SqlMapConfig.xml"; 40 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 41 // 创建sqlsessionFactory 42 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 43 // 创建sqlsession 44 SqlSession openSession = sqlSessionFactory.openSession(); 45 //sqlsession生成一个实现类 46 UserMapper mapper = openSession.getMapper(UserMapper.class); 47 //利用接口调方法 48 QueryVo vo=new QueryVo(); 49 User user=new User(); 50 user.setUsername("五"); 51 vo.setUser(user); 52 List<User> u = mapper.findUserByQueryVo(vo); 53 for (User user2 : u) { 54 System.out.println(user2); 55 } 56 } 57 //查询用户总数 58 @Test 59 void testCountUser() throws Exception { 60 // 加载核心配置文件 61 String resource = "SqlMapConfig.xml"; 62 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 63 // 创建sqlsessionFactory 64 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 65 // 创建sqlsession 66 SqlSession openSession = sqlSessionFactory.openSession(); 67 //sqlsession生成一个实现类 68 UserMapper mapper = openSession.getMapper(UserMapper.class); 69 //利用接口调方法 70 Integer users = mapper.countUsers(); 71 72 System.out.println(users); 73 74 } 75 //查询所有订单 76 @Test 77 void testFindAllOrders() throws Exception { 78 // 加载核心配置文件 79 String resource = "SqlMapConfig.xml"; 80 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 81 // 创建sqlsessionFactory 82 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 83 // 创建sqlsession 84 SqlSession openSession = sqlSessionFactory.openSession(); 85 //sqlsession生成一个实现类 86 OrderMapper mapper = openSession.getMapper(OrderMapper.class); 87 //利用接口调方法 88 List<Orders> orders = mapper.findAllOrders(); 89 for (Orders orders2 : orders) { 90 91 System.out.println(orders2); 92 } 93 94 } 95 //根据姓名和性别查询 96 @Test 97 void testFindUserBySexAndName() throws Exception { 98 // 加载核心配置文件 99 String resource = "SqlMapConfig.xml"; 100 InputStream resourceAsStream = Resources.getResourceAsStream(resource); 101 // 创建sqlsessionFactory 102 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); 103 // 创建sqlsession 104 SqlSession openSession = sqlSessionFactory.openSession(); 105 //sqlsession生成一个实现类 106 UserMapper mapper = openSession.getMapper(UserMapper.class); 107 //利用接口调方法 108 User user=new User(); 109 user.setSex("1"); 110 user.setUsername("张小明"); 111 List<User> users = mapper.selectUserBySexAndUserName(user); 112 for (User user1 : users) { 113 114 System.out.println(user1); 115 } 116 117 } 118 119 }
发现可以查询出来:
注释掉名字的set方法
发现爱你结果可以查五个用户
但是注释掉关于名字的set方法,则会报错
这时便可以用if where来解决问题,因为
原文:https://www.cnblogs.com/tkg1314/p/12285107.html