6.MyBatis-Plus 通用Service
Service
package com.example.wx_test.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.wx_test.entity.Department;
public interface DepartmentService extends IService<Department> {
}
Impl
package com.example.wx_test.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
import com.example.wx_test.mapper.DepartmentMapper;
import org.springframework.stereotype.Service;
@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
}
save
package com.example.wx_test;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {
@Autowired
private DepartmentService departmentService;
@Test
public void save(){
Department department = new Department();
department.setName("测试部门");
department.setRemark("测试部门remark");
boolean result = departmentService.save(department);
System.out.println(result);
}
}
updateById
package com.example.wx_test;
import com.example.wx_test.service.DepartmentService;
import com.example.wx_test.entity.Department;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DepartmentServiceTest {
@Autowired
private DepartmentService departmentService;
@Test
public void updateById(){
Department department = new Department();
department.setId(17);
department.setName("测试部门2");
department.setRemark("测试部门remark2");
boolean result = departmentService.updateById(department);
System.out.println(result);
}
}
removeById
@Test
public void removeById(){
boolean result = departmentService.removeById(17);
System.out.println(result);
}
list
@Test
public void list(){
List<Department> list = departmentService.list();
System.out.println(list);
}
接下来是批处理
saveBatch
@Test
public void saveBatch(){
Department department1 = new Department();
department1.setName("测试部门1");
department1.setRemark("测试部门remark1");
Department department2 = new Department();
department2.setName("测试部门2");
department2.setRemark("测试部门remark2");
Department department3 = new Department();
department3.setName("测试部门3");
department3.setRemark("测试部门remark3");
boolean result = departmentService.saveBatch(
Arrays.asList(department1,department2,department3)
);
System.out.println(result);
}
updateBatchById
@Test
public void updateBatchById(){
Department department1 = new Department();
department1.setId(18);
department1.setName("测试部门11");
department1.setRemark("测试部门remark11");
Department department2 = new Department();
department2.setId(19);
department2.setName("测试部门22");
department2.setRemark("测试部门remark22");
Department department3 = new Department();
department3.setId(20);
department3.setName("测试部门33");
department3.setRemark("测试部门remark33");
boolean result = departmentService.updateBatchById(
Arrays.asList(department1,department2,department3)
);
System.out.println(result);
}
参考资料
- https://www.bilibili.com/video/BV1KV411U7pH?p=20