Mybatis快速上手5 模糊查询和动态SQL

其他章节及开发包:https://blog.csdn.net/XIMAX/article/details/106255196# 环境 :Eclipse+Tomcat9+JDK10+Mybatis-3.1.1、Navicat
在这里插入图片描述
test1~8分别对应八章,本章使用包test5、tool,文件config.xml、db.properties、log4j.properties

需求

实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间)

代码

建库,建表

create table d_user(  
 id int primary key auto_increment,  
 name varchar(10),
 age int(3)
); 

insert into d_user(name,age) values('Tom',12);  
insert into d_user(name,age) values('Bob',13);  
insert into d_user(name,age) values('Jack',18);

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <properties resource="db.properties"></properties>
  <typeAliases>
    <package name="com.rjxy.test5"/>
 <typeAliases>
 
 <typeAliases>
   <package name="com.rjxy.test2"/>
 <typeAliases>
 <environments default="development">
   <environment id="development">
    <transactionManager type="JDBC" />
    <dataSource type="POOLED">
     <property name="driver" value="${driver}" />
     <property name="url" value="${url}" />
     <property name="username" value="${username}" />
     <property name="password" value="${password}" />
    </dataSource>
   </environment>
  </environments>  
 
 <mappers>
   <mapper resource="com/rjxy/test5/userMapper.xml"/>
 <mappers>
</configuration>

userMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.rjxy.test5.userMapper">
 <!-- 动态SQL与模糊查询 -->
 <select id="getUser" parameterType="ConditionUser" resultType="User">
  select * from d_user where

 <if test='name!="%null%"'>
   and name like #{name}
  </if>

 age between #{minAge} and #{maxAge}
 </select>

<!-- MyBatis中可用的动态SQL标签:
   if
   choose(when,otherwise)
   trim(where,set)
   foreach
  -->
</mapper>

ConditionUser.java

package com.rjxy.test5;
public class ConditionUser {
 private String name;
 private int minAge;
 private int maxAge;

public ConditionUser() {}
public ConditionUser(String name,int minAge,int maxAge) {
  this.name = name;
  this.minAge = minAge;
  this.maxAge = maxAge;
}
//省略 get、set 及 toString 方法
}

JTest5.java

package com.rjxy.test5;

import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import com.rjxy.tool.MybatisUtils;
class JTest5 {
@Test
 void test() throws IOException {
  SqlSessionFactory factory = MybatisUtils.getFactory();
  SqlSession session = factory.openSession();
  
  String statement = "com.rjxy.test5.userMapper.getUser";
  
  String name = null;
  
  ConditionUser parameter = new ConditionUser("%"+name+"%",1,30);
  List<Object> list = session.selectList(statement, parameter);
  System.out.println(list);
  session.close();
 }
 }

其它

其它文件与该系列第一篇相同 链接: https://blog.csdn.net/XIMAX/article/details/106244701

结果

2020-05-21 11:11:13,872 [main] DEBUG [com.rjxy.test5.userMapper.getUser] - ooo Using Connection [com.mysql.cj.jdbc.ConnectionImpl@207ea13]
2020-05-21 11:11:13,872 [main] DEBUG [com.rjxy.test5.userMapper.getUser] - ==>  Preparing: select * from d_user where age between ? and ? 
2020-05-21 11:11:13,957 [main] DEBUG [com.rjxy.test5.userMapper.getUser] - ==> Parameters: 1(Integer), 30(Integer)
[User [id=1, name=Tom, age=12], User [id=2, name=Bob, age=13], User [id=3, name=Jack, age=18]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值