MyBatis Plus 2.3 个人笔记-02-基本注解
- 实体类注解
/* * MybatisPlus会默认使用实体类的类名到数据中找对应的表. * */ @TableName("tbl_employee") public class Employee extends Model<Employee> { private static final long serialVersionUID = 1L; /* * @TableId: * value: 指定表中的主键列的列名, 如果实体属性名与列名一致,可以省略不指定. * type: 指定主键策略. ID_WORKER 全局唯一ID,内容为空自动填充(默认配置) */ @TableId(value = "id", type = IdType.ID_WORKER) private Long id; private String lastName; private String email; private String gender; private Integer age; /** * 声明该属性不是数据库中字段 */ @TableField(exist = false) private String notExist; } /* 省略get set */
- 使用通用CURD方法
- 删除
/** * 通用 删除操作 */ @Test public void testCommonDelete() { //1 .根据id进行删除 Integer result = employeeMapper.deleteById(13);
/*
==> Preparing: DELETE FROM tbl_employee WHERE id=?
==> Parameters: 13(Long)
<== Updates: 0
*/
//2. 根据 条件进行删除 // Map<String,Object> columnMap = new HashMap<>(); // columnMap.put("last_name", "MP"); // columnMap.put("email", "mp@atguigu.com"); // Integer result = employeeMapper.deleteByMap(columnMap); // System.out.println("result: " + result );
/*
==> Preparing: DELETE FROM tbl_employee WHERE last_name = ? AND email = ?
==> Parameters: MP(String), mp@atguigu.com(String)
<== Updates: 0
*/
//3. 批量删除 // List<Integer> idList = new ArrayList<>(); // idList.add(3); // idList.add(4); // idList.add(5); // Integer result = employeeMapper.deleteBatchIds(idList); // System.out.println("result: " + result ); }
/*
==> Preparing: DELETE FROM tbl_employee WHERE id IN ( ? , ? , ? )
==> Parameters: 35(Integer), 45(Integer), 55(Integer)
<== Updates: 0
*/
条件构造器
实体包装器,用于处理 sql 拼接,排序,实体参数查询等!
补充说明: 使用的是数据库字段,不是Java属性!
/** * 条件构造器 删除操作 * */ @Test public void testEntityWrapperDelete() { employeeMapper.delete( new EntityWrapper<Employee>() .eq("last_name", "Tom") .eq("age", 22) ); }
/* ==> Preparing: DELETE FROM tbl_employee WHERE (last_name = ? AND age = ?) ==> Parameters: Tom(String), 22(Integer) <== Updates: 0 */
-
- 修改
- 修改
/** * 通用 更新操作 */ @Test public void testCommonUpdate() { //初始化修改对象 Employee employee = new Employee(); employee.setId(7L); employee.setLastName("小泽老师"); employee.setEmail("xz@sina.com"); employee.setGender("0");
// updateById 实体类为空的列 自动跳出,不修改 Integer result = employeeMapper.updateById(employee);
/* ==> Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE id=? ==> Parameters: 小泽老师(String), xz@sina.com(String), 0(String), 7(Long) <== Updates: 0 */ // updateAllColumnById 所有列都会修改 Integer result = employeeMapper.updateAllColumnById(employee);
/*
==> Preparing: UPDATE tbl_employee SET last_name=?,email=?,gender=?,age=?,version=? WHERE id=?
==> Parameters: 小泽老师(String), xz@sina.com(String), 0(String), null, null, 7(Long)
<== Updates: 0
*/
}
/** * 条件构造器 修改操作 */ @Test public void testEntityWrapperUpdate() { Employee employee = new Employee(); employee.setLastName("苍老师"); employee.setEmail("cls@sina.com"); employee.setGender("0"); employeeMapper.update(employee, new EntityWrapper<Employee>() .eq("last_name", "Tom") .eq("age", 44) );
/*
==> Preparing: UPDATE tbl_employee SET last_name=?, email=?, gender=? WHERE (last_name = ? AND age = ?)
==> Parameters: 苍老师(String), cls@sina.com(String), 0(String), Tom(String), 44(Integer)
<== Updates: 0
*/
}
-
- 新增
/** * 通用 插入操作 */ @Test public void testCommonInsert() { //初始化Employee对象 Employee employee = new Employee(); employee.setLastName("MP"); employee.setEmail("mp@atguigu.com"); //employee.setGender(1); //employee.setAge(22); //employee.setSalary(2"0""0""0""0"."0"); //插入到数据库 // insert方法在插入时, 会根据实体类的每个属性进行非空判断,只有非空的属性对应的字段才会出现到SQL语句中 //Integer result = employeeMapper.insert(employee);
/*
==> Preparing: INSERT INTO tbl_employee ( id, last_name, email ) VALUES ( ?, ?, ? )
==> Parameters: 1107953008443265026(Long), MP(String), mp@atguigu.com(String)
<== Updates: 1
*/
//insertAllColumn方法在插入时, 不管属性是否非空, 属性所对应的字段都会出现到SQL语句中. Integer result = employeeMapper.insertAllColumn(employee);
/*
==> Preparing: INSERT INTO tbl_employee ( id,last_name,email,gender,age,version ) VALUES ( ?,?,?,?,?,? )
==> Parameters: 1107952717069209602(Long), MP(String), mp@atguigu.com(String), null, null, null
<== Updates: 1
*/
System.out.println("result: " + result ); //获取当前数据在数据库中的主键值 mybatis plus自动会回填id Long key = employee.getId(); System.out.println("key:" + key ); }
-
- 查询
/** * 通用 查询操作 */ @Test public void testCommonSelect() { //1. 通过id查询 // Employee employee = employeeMapper.selectById(7); // System.out.println(employee); //2. 通过多个列进行查询 id + lastName // Employee employee = new Employee(); // //employee.setId(7); // employee.setLastName("小泽老师"); // employee.setGender("0"); // // Employee result = employeeMapper.selectOne(employee); // System.out.println("result: " +result ); //3. 通过多个id进行查询 <foreach> // List<Integer> idList = new ArrayList<>(); // idList.add(4); // idList.add(5); // idList.add(6); // idList.add(7); // List<Employee> emps = employeeMapper.selectBatchIds(idList); // System.out.println(emps); //4. 通过Map封装条件查询 // Map<String,Object> columnMap = new HashMap<>(); // columnMap.put("last_name", "Tom"); // columnMap.put("gender", 1); // // List<Employee> emps = employeeMapper.selectByMap(columnMap); // System.out.println(emps); //5. 分页查询 List<Employee> emps = employeeMapper.selectPage(new Page(1, 2), null); System.out.println(emps); }
/** * 条件构造器 查询操作 */ @Test public void testEntityWrapperSelect() { //我们需要分页查询tbl_employee表中,年龄在18~5"0"之间且性别为男且姓名为Tom的所有用户 // List<Employee> emps =employeeMapper.selectPage(new Page<Employee>(1, 2), // new EntityWrapper<Employee>() // .between("age", 18, 5"0") // .eq("gender", 1) // .eq("last_name", "Tom") // ); // System.out.println(emps);
/*
==> Preparing: SELECT COUNT(1) FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)
==> Parameters: 18(Integer), 50(Integer), 1(Integer), Tom(String)
<== Columns: COUNT(1)
<== Row: 0
==> Preparing: SELECT id AS id,last_name AS lastName,email,gender,age,version FROM tbl_employee WHERE (age BETWEEN ? AND ? AND gender = ? AND last_name = ?)
==> Parameters: 18(Integer), 50(Integer), 1(Integer), Tom(String)
<== Total: 0
*/
// 查询tbl_employee表中, 性别为女并且名字中带有"老师" 或者 邮箱中带有"a" // List<Employee> emps = employeeMapper.selectList( // new EntityWrapper<Employee>() // .eq("gender", "0") // .like("last_name", "老师") // //.or() // SQL: (gender = ? AND last_name LIKE ? OR email LIKE ?) // .orNew() // SQL: (gender = ? AND last_name LIKE ?) OR (email LIKE ?) // .like("email", "a") // ); // System.out.println(emps);
/*
==> Preparing: SELECT id AS id,last_name AS lastName,email,gender,age,version FROM tbl_employee WHERE (gender = ? AND last_name LIKE ?) OR (email LIKE ?)
==> Parameters: 0(String), %老师%(String), %a%(String)
<== Columns: id, lastName, email, gender, age, version
<== Row: 1107952717069209602, MP, mp@atguigu.com, null, null, null
<== Row: 1107953008443265026, MP, mp@atguigu.com, null, null, null
<== Total: 2
*/
// 查询性别为女的, 根据age进行排序(asc/desc), 简单分页 // List<Employee> emps = employeeMapper.selectList( // new EntityWrapper<Employee>() // .eq("gender", "0") // .orderBy("age") // //.orderDesc(Arrays.asList(new String [] {"age"})) // .last("desc limit 1,3") // ); // System.out.println(emps); }
/*
==> Preparing: SELECT id AS id,last_name AS lastName,email,gender,age,version FROM tbl_employee WHERE (gender = ?) ORDER BY age desc limit 1,3
==> Parameters: 0(String)
<== Total: 0
*/