- 插入一条记录
功能同 Mapper
层中的 insert
方法,只不过方法名不同。
- 方法声明
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
- 插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSave() {
Employee employee = new Employee();
employee.setId(5L);
employee.setName(“周七”);
employee.setEmail(“zhouqi@cunyu1943.com”);
employee.setSex(“女”);
Assert.assertTrue(employeeService.save(employee));
System.out.println(“插入成功”);
}
}
- 测试结果
- 插入数据后的数据库
- 批量插入
这里就和 Mapper
层中所有区别,Mapper
层中只支持单次插入,而 Service
层中支持批量插入,而传入的参数就是我们所要传入实体的集合,而且还可以分批次插入和统一插入。
2.1 统一插入
- 方法声明
// 插入(批量)
boolean saveBatch(Collection entityList);
- 插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSaveBatch() {
Employee employee1 = new Employee();
employee1.setId(6L);
employee1.setEmail(“zhangliang@cunyu1943.com”);
employee1.setSex(“男”);
employee1.setName(“张良”);
Employee employee2 = new Employee();
employee2.setId(7L);
employee2.setEmail(“zhouyu@cunyu1943.com”);
employee2.setName(“周瑜”);
employee2.setSex(“男”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.saveBatch(employeeList));
System.out.println(“批量插入成功”);
}
}
- 测试结果
- 统一插入后的数据库
2.2 分批次插入
- 方法声明
// 插入(批量)
boolean saveBatch(Collection entityList, int batchSize);
- 分批次插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSaveBatch() {
Employee employee1 = new Employee();
employee1.setId(8L);
employee1.setEmail(“jialuo@cunyu1943.com”);
employee1.setSex(“女”);
employee1.setName(“迦罗”);
Employee employee2 = new Employee();
employee2.setId(9L);
employee2.setEmail(“zhugeliang@cunyu1943.com”);
employee2.setName(“诸葛亮”);
employee2.setSex(“男”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.saveBatch(employeeList,2));
System.out.println(“批量插入成功”);
}
}
- 测试结果
- 分批次插入后的数据库
SaveOrUpdate
- 单条修改插入
- 方法声明
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
- 单条修改插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSaveOrUpdate() {
Employee employee = new Employee();
employee.setId(5L);
employee.setName(“周武”);
employee.setEmail(“zhouwu@cunyu1943.com”);
employee.setSex(“男”);
Assert.assertTrue(employeeService.saveOrUpdate(employee));
System.out.println(“更新成功”);
}
}
- 测试结果
- 修改插入后的数据库
- 批量修改插入
2.1 统一插入
- 方法声明
// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList);
- 统一插入实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSaveOrUpdateBatch() {
Employee employee1 = new Employee();
employee1.setId(10L);
employee1.setEmail(“zhongwuyan@cunyu1943.com”);
employee1.setSex(“女”);
employee1.setName(“钟无艳”);
Employee employee2 = new Employee();
employee2.setId(11L);
employee2.setEmail(“direnjie@cunyu1943.com”);
employee2.setName(“狄仁杰”);
employee2.setSex(“男”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList));
System.out.println(“批量修改插入成功”);
}
}
- 测试结果
- 统一插入数据后的数据库
2.2 分批次插入
- 方法声明
// 批量修改插入
boolean saveOrUpdateBatch(Collection entityList, int batchSize);
- 方法实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testSaveOrUpdateBatch() {
Employee employee1 = new Employee();
employee1.setId(12L);
employee1.setEmail(“yuji@cunyu1943.com”);
employee1.setSex(“女”);
employee1.setName(“虞姬”);
Employee employee2 = new Employee();
employee2.setId(13L);
employee2.setEmail(“sulie@cunyu1943.com”);
employee2.setName(“苏烈”);
employee2.setSex(“男”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList, 2));
System.out.println(“批量修改插入成功”);
}
}
- 测试结果
- 分批次插入数据后的数据库
Remove
- 根据 ID 删除
- 方法实例
// 根据 ID 删除
boolean removeById(Serializable id);
- 删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testRemoveById() {
Assert.assertTrue(employeeService.removeById(5));
System.out.println(“删除成功”);
}
}
- 测试结果
- 根据条件删除
- 方法声明
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
- 按条件删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testRemoveByMap() {
Map<String, Object> map = new HashMap<>();
map.put(“sex”, “女”);
Assert.assertTrue(employeeService.removeByMap(map));
System.out.println(“删除成功”);
}
}
- 测试结果
- 按条件删除后的数据库
- 根据 ID 批量删除
- 方法声明
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
- 批量删除实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testRemoveByIds() {
List ids = new ArrayList<>();
ids.add(1);
ids.add(4);
Assert.assertTrue(employeeService.removeByIds(ids));
System.out.println(“批量删除成功”);
}
}
- 测试结果
- 批量删除后的数据库
Update
- 根据 ID 选择修改
- 方法声明
// 根据 ID 选择修改
boolean updateById(T entity);
- 根据 ID 修改实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testUpdateById() {
Employee employee = new Employee();
employee.setId(3L);
employee.setName(“程咬金”);
employee.setSex(“男”);
employee.setEmail(“chengyaojin@cunyu1943.com”);
Assert.assertTrue(employeeService.updateById(employee));
System.out.println(“更新成功”);
}
}
- 测试结果
- 更新后的数据库
- 根据 ID 批量更新
2.1 统一更新
- 方法声明
// 根据ID 批量更新
boolean updateBatchById(Collection entityList);
- 批量更新实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testUpdateBatchById() {
Employee employee1 = new Employee();
employee1.setId(6L);
employee1.setName(“妲己”);
employee1.setSex(“女”);
employee1.setEmail(“daji@cunyu1943.com”);
Employee employee2 = new Employee();
employee2.setId(13L);
employee2.setName(“小乔”);
employee2.setSex(“女”);
employee2.setEmail(“xiaoqiao@cunyu1943.com”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.updateBatchById(employeeList));
System.out.println(“更新成功”);
}
}
- 测试结果
- 批量更新后的数据库
2.2 分批次更新
- 方法声明
// 根据ID 批量更新
boolean updateBatchById(Collection entityList, int batchSize);
- 分批次更新实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testUpdateBatchById() {
Employee employee1 = new Employee();
employee1.setId(7L);
employee1.setName(“武则天”);
employee1.setSex(“女”);
employee1.setEmail(“wuzetian@cunyu1943.com”);
Employee employee2 = new Employee();
employee2.setId(3L);
employee2.setName(“李元芳”);
employee2.setSex(“男”);
employee2.setEmail(“liyuanfang@cunyu1943.com”);
List employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
Assert.assertTrue(employeeService.updateBatchById(employeeList, 2));
System.out.println(“更新成功”);
}
}
- 测试结果
- 分批次更新后的数据库
Get
- 根据 ID 查询
将所要查询记录的 id
作为参数,然后将查询到的实体返回。
- 方法声明
// 根据 ID 查询
T getById(Serializable id);
- 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testGetById() {
Employee employee = employeeService.getById(9);
System.out.println(employee);
}
}
- 测试结果
List
- 查询所有
查询所有记录,然后返回到一个集合中。
- 方法声明
// 查询所有
List list();
- 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testList() {
List employeeLists = new ArrayList<>();
employeeLists = employeeService.list();
Assert.assertEquals(6, employeeLists.size());
System.out.println(“查询成功”);
}
}
- 测试结果
- 根据 ID 批量查询
讲所要查询的记录 id
传入集合,然后座位方法参数,最后返回查询到的结果到一个集合中。
- 方法声明
// 查询(根据ID 批量查询)
Collection listByIds(Collection<? extends Serializable> idList);
- 批量查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testListByIds() {
List ids = new ArrayList<>();
ids.add(6L);
ids.add(7L);
Assert.assertEquals(2, employeeService.listByIds(ids).size());
System.out.println(“查询成功”);
}
}
- 测试结果
- 根据条件查询
条件传入 Map
集合,key
对应字段,value
对应值,然后返回集合。
- 方法声明
// 查询(根据 columnMap 条件)
Collection listByMap(Map<String, Object> columnMap);
- 根据条件查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testListByMap() {
Map<String, Object> map = new HashMap<>();
map.put(“sex”, “女”);
Assert.assertEquals(3, employeeService.listByMap(map).size());
System.out.println(“查询成功”);
}
}
- 测试结果
- 查询所有列表
- 方法声明
// 查询所有列表
List<Map<String, Object>> listMaps();
- 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testListMaps() {
Assert.assertEquals(6, employeeService.listMaps());
System.out.println(“查询成功”);
}
}
- 测试结果
- 查询所有记录
总结
如果你选择了IT行业并坚定的走下去,这个方向肯定是没有一丝问题的,这是个高薪行业,但是高薪是凭自己的努力学习获取来的,这次我把P8大佬用过的一些学习笔记(pdf)都整理在本文中了
《Java中高级核心知识全面解析》
小米商场项目实战,别再担心面试没有实战项目:
m.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testListByMap() {
Map<String, Object> map = new HashMap<>();
map.put(“sex”, “女”);
Assert.assertEquals(3, employeeService.listByMap(map).size());
System.out.println(“查询成功”);
}
}
- 测试结果
- 查询所有列表
- 方法声明
// 查询所有列表
List<Map<String, Object>> listMaps();
- 查询实例
package com.cunyu.employee;
import com.cunyu.employee.entity.Employee;
import com.cunyu.employee.mapper.EmployeeMapper;
import com.cunyu.employee.service.EmployeeService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EmployeeApplicationTests {
@Autowired
private EmployeeService employeeService;
@Test
void testListMaps() {
Assert.assertEquals(6, employeeService.listMaps());
System.out.println(“查询成功”);
}
}
- 测试结果
- 查询所有记录
总结
如果你选择了IT行业并坚定的走下去,这个方向肯定是没有一丝问题的,这是个高薪行业,但是高薪是凭自己的努力学习获取来的,这次我把P8大佬用过的一些学习笔记(pdf)都整理在本文中了
《Java中高级核心知识全面解析》
[外链图片转存中…(img-SRTVAXbM-1714515641087)]
小米商场项目实战,别再担心面试没有实战项目:
[外链图片转存中…(img-0DnMmrNe-1714515641088)]