配置,接口,实体类等就不再写了,可以参考上一篇
https://blog.csdn.net/qq_43560721/article/details/89487137
这里简单举几个例子:
package cn.xxs.test;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import cn.xxs.mapper.UserMapper;
import cn.xxs.pojo.Student;
public class Test {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() {
//通过输入流创建SqlSessionFactory
sqlSessionFactory = (SqlSessionFactory) new ClassPathXmlApplicationContext("bean.xml").getBean("sqlSessionFactory");
}
/**
* allEq
* @throws IOException
*/
@org.junit.Test
public void allEq() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
//map中的第一个参数指的是表中的列的名字,第二参数对应的传入的参数
Map<String,Object> columnMap = new HashMap<String, Object>();
columnMap.put("studentName", "s");
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.allEq(columnMap);
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* eq
* @throws IOException
*/
@org.junit.Test
public void eq() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.eq("studentId",1);
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* like
* @throws IOException
*/
@org.junit.Test
public void like() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.like("studentName","s");
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* gt查询结果不带第1条
* @throws IOException
*/
@org.junit.Test
public void gt() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.gt("studentId",1);
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* ge 查询结果带第1条
* @throws IOException
*/
@org.junit.Test
public void ge() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.ge("studentId",1);
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* between 查询结果带第1条和第五条
* @throws IOException
*/
@org.junit.Test
public void between() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
wrapper.between("studentId",1,5);
//使用
List<Student> stuList = userMapper.selectList(wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
/**
* 分页查询
* @throws IOException
*/
@org.junit.Test
public void pageFind() throws IOException {
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
EntityWrapper<Student> wrapper = new EntityWrapper<Student>();
//第一个参数:起始位置,第二个参数:查询条数
RowBounds rowBounds = new RowBounds(0,1);
//使用
List<Student> stuList = userMapper.selectPage(rowBounds, wrapper);
//控制台输出
for(Student student:stuList) {
System.out.println(student);
}
//释放资源
ss.close();
}
}
更多条件构造器见
https://mp.baomidou.com/guide/wrapper.html#abstractwrapper