通用CRUD
import com.xxxx.dao.AdminDAO;
import com.xxxx.entity.Admin;
import com.baomidou.mybatisplus.plugins.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MPCRUDTest {
@Autowired
private AdminDAO adminDAO;
//添加
@Test
public void testInsert(){
//insert
Admin admin = new Admin();
admin.setId("1");
admin.setName("xiaohei");
admin.setPassword("123456");
adminDAO.insert(admin);
}
//删除
@Test
public void testDelete(){
//根据id删除 deleteById
adminDAO.deleteById("2");
//根据columnMap条件删除 deleteByMap,其中"name"对应的是数据库中的字段
Map<String,Object> map = new HashMap<>();
map.put("name","xiaopang");
adminDAO.deleteByMap(map);
//根据id批量删除 deleteBatchIds
List<String> idList = new ArrayList<>();
idList.add("1");
idList.add("2");
adminDAO.deleteBatchIds(idList);
}
//修改
@Test
public void testUpdate(){
//根据id修改指定列 updateById,没有指定的列不变
Admin admin = new Admin();
admin.setId("2");
admin.setName("xiaopang");
admin.setPassword("123456");
adminDAO.updateById(admin); // 或者 adminDAO.update(admin,null);
//修改所有的列 updateAllColumnbyId,没有指定值的列都为null
adminDAO.updateAllColumnById(admin);
}
//查询
@Test
public void testSelect(){
//根据id查询 selectById
Admin admin1 = adminDAO.selectById("1");
//根据entity条件查询 selectOne 只能查询出来一个,如果查询结果有多个会报错
Admin admin = new Admin();
admin.setName("xiaohei");
Admin admin2 = adminDAO.selectOne(admin);
System.out.println(admin2);
//根据多个Id查询 selectBatchIds
List<String> idList = new ArrayList<>();
idList.add("1");
idList.add("2");
List<Admin> admins = adminDAO.selectBatchIds(idList);
System.out.println(admins);
//根据columnMap条件查询 selectByMap 查询结果可能会有多条
Map<String, Object> map = new HashMap<>();
map.put("password","123456");
List<Admin> admins1 = adminDAO.selectByMap(map);//如果确定查询结果只有一条可以这样取出来 Admin admin = admins1.get(0)
System.out.println(admins1);
//查询总记录数 selectCount
Integer records = adminDAO.selectCount(null);
System.out.println(records);
//查询所有 selectList
List<Admin> admins2 = adminDAO.selectList(null);
System.out.println(admins2);
//分页查询 selectPage
List<Admin> admins3 = adminDAO.selectPage(new Page<>(1, 3), null);
}
}
条件构造器
注意:条件中的字段都是数据库中的字段
1. 分页查询user表中,年龄在18~30之间且性别为男性且姓名为小黑的所有用户
@Test
//分页查询user表中,年龄在18~30之间且性别为男性且姓名为小黑的所有用户
public void testSelect(){
EntityWrapper<User> entityWrapper = new EntityWrapper<>();
entityWrapper.between("age",18,30)
.eq("sex","1")
.eq("username","小黑");
List<User> users = userDAO.selectPage(new Page<User>(1, 3), entityWrapper);
System.out.println(users);
}
2. 条件构造器模糊查询
//模糊查询 可以拼接其他条件
@Test
public void selectLike(String content){
EntityWrapper<User> wrapper = new EntityWrapper<>();
wrapper.like("username",content);
List<User> users = userService.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
3. 举了两个例子,其他的还有很多可以构造的条件在下面的条件参数说明中
条件参数说明
查询方式 | 说明 |
setSqlSelect | 设置 SELECT 查询字段 |
where | WHERE 语句,拼接 + |
and | AND 语句,拼接 + |
andNew | AND 语句,拼接 + |
or | OR 语句,拼接 + |
orNew | OR 语句,拼接 + |
eq | 等于= |
allEq | 基于 map 内容等于= |
ne | 不等于<> |
gt | 大于> |
ge | 大于等于>= |
lt | 小于< |
le | 小于等于<= |
like | 模糊查询 LIKE |
notLike | 模糊查询 NOT LIKE |
in | IN 查询 |
notIn | NOT IN 查询 |
isNull | NULL 值查询 |
isNotNull | IS NOT NULL |
groupBy | 分组 GROUP BY |
having | HAVING 关键词 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 条件语句 |
notExists | NOT EXISTS 条件语句 |
between | BETWEEN 条件语句 |
notBetween | NOT BETWEEN 条件语句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last("LIMIT 1") |
ActiveRecord
前提是实体类继承Model类,然后实体类中有这样一个方法(返回的是主键)
protected Serializable pkVal() { return this.id; }
神奇,简单体会一下
//AR
@Test
public void testAR(){
//插入操作
Admin admin = new Admin();
admin.setId("88");
admin.setName("大胖墩儿");
admin.setPassword("123456");
admin.insert();
//查询操作
admin.setId("88");
Admin admin1 = admin.selectById();
System.out.println(admin1);
}