随笔 - 1162  文章 - 0  评论 - 16  阅读 - 59万 

一、基本查询

1、selectOne(T record)

说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

案例:

    /**
     * SELECT emp_id,emp_name,emp_salary,emp_age FROM tabple_emp WHERE emp_id = ?
     */
    @Test
    public void testSelectOne() {
        //1.创建封装查询条件的实体类对象
        Employee employeeQueryCondition = new Employee(1nullnull,null);
        //2. 执行查询
        Employee result = employeeService.getOne(employeeQueryCondition);
        //3.打印结果
        System.out.println("result = " + result);
    }
    public Employee getOne(Employee employeeQueryCondition) {
        return employeeMapper.selectOne(employeeQueryCondition);
    }

selectOne() 方法

通用Mapper替我们自动生成SQL语句情况

实体类封装查询条件生成的where子句的规则

① 使用非空的值生成 where 子句;

② 在条件表达式中使用 "=" 进行比较

要求必须返回一个实体类结果,如果有多个,则会抛出异常

2、select(T record)

说明:根据实体中的属性值进行查询,查询条件使用等号

案例:

    /**
     * SELECT emp_id,emp_name,emp_salary,emp_age FROM tabple_emp
     */
    @Test
    public void testSelect() {
        //1.创建封装查询条件的实体类对象
        Employee employeeQueryCondition = new Employee(nullnullnull,null);
        //2. 执行查询
        List<Employee> list = employeeService.select(employeeQueryCondition);
        //3.打印结果
        list.forEach(System.out::println);
    }
    public List<Employeeselect(Employee employeeQueryCondition) {
        return employeeMapper.select(employeeQueryCondition);
    }

3、selectAll()

说明:查询全部结果

案例:

    /**
     * SELECT emp_id,emp_name,emp_salary,emp_age FROM tabple_emp
     */
    @Test
    public void testSelectAll() {
        //1.查询所有的记录
        List<Employee> list = employeeService.getAll();
        //2.打印结果
        list.forEach(System.out::println);
    }
    public List<EmployeegetAll() {
        return employeeMapper.selectAll();
    }

4、selectCount(T record)

说明:根据实体中的属性查询总数,查询条件使用等号 案例:

    /**
     * SELECT COUNT(emp_id) FROM tabple_emp WHERE emp_id = ?
     */
    @Test
    public void testSelectCount() {
        //1.创建封装查询条件的实体类对象
        Employee employeeQueryCondition = new Employee(1nullnull,null);
        //2. 执行查询
        Integer count = employeeService.getCount(employeeQueryCondition);
        //3.打印结果
        System.out.println("count = " + count);
    }
    public Integer getCount(Employee employeeQueryCondition) {
        return employeeMapper.selectCount(employeeQueryCondition);
    }

5、selectByPrimaryKey(Object key)

说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

案例:

    /**
     * SELECT emp_id,emp_name,emp_salary,emp_age FROM tabple_emp WHERE emp_id = ?
     */
    @Test
    public void testSelectByPrimaryKey() {
        //1.提供id值
        Integer id = 1;
        //2. 执行根据主键进行的查询
        Employee result = employeeService.getEmployeeById(id);
        //3.打印结果
        System.out.println("result = " + result);
    }
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

注意:需要使用@Id 主键明确标记和数据库主键字段对应的实体类字段,否则通用Mapper会将所有实体类字段作为联合主键。

6、existWithPrimaryKey(Object key)

说明:根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号

案例:

    /**
     * SELECT CASE WHEN COUNT(emp_id) > 0 THEN 1 ELSE 0 END AS result FROM tabple_emp WHERE emp_id = ?
     */
    @Test
    public void testExistsWithPrimaryKey() {
        //1.提供id值
        Integer id = 1;
        //2. 执行根据主键进行的查询
        boolean exists = employeeService.isExists(id);
        //3.打印结果
        System.out.println("exists = " + exists);
    }
    public boolean isExists(Integer id) {
        return employeeMapper.existsWithPrimaryKey(id);
    }

二、基本插入

1、insert(T record)

说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

案例:

    @Test
    public void testInsert() {
        //1.创建实体类对象,封装要保存到数据库的对象
        Employee emp1 = new Employee(null"emp03"1000.0023);
        //2. 执行插入保存操作
        Integer result = employeeService.saveEmp(emp1);
        //3.打印结果
        System.out.println("result = " + result);
        //4.获取 emp1 对象的主键字段
        System.out.println("EmpId=" + emp1.getEmpId());
    }
    public Integer saveEmp(Employee emp) {
        return employeeMapper.insert(emp);
    }

2、insertSelective(T record)

说明:保存一个实体,null的属性不会保存,会使用数据库默认值

案例:

    @Test
    public void testInsertSelective() {
        //1.创建实体类对象,封装要保存到数据库的对象
        Employee emp1 = new Employee(null"emp04"null23);
        //2. 执行插入保存操作
        Integer result = employeeService.saveEmpSelective(emp1);
        //3.打印结果
        System.out.println("result = " + result);
        //4.获取 emp1 对象的主键字段
        System.out.println("EmpId=" + emp1.getEmpId());
    }
    public Integer saveEmpSelective(Employee emp) {
        return employeeMapper.insertSelective(emp);
    }

注意:如果要获取实体类的主键字段需要在字段上面加上@GeneratedValue(strategy = GenerationType.IDENTITY),数据库需要支持主键自增

三、基本更新

1、updateByPrimarkKey(T record)

说明:根据主键更新实体全部字段,null也会被更新

案例:

    /**
     * UPDATE tabple_emp SET emp_id = emp_id,emp_name = ?,emp_salary = ?,emp_age = ? WHERE emp_id = ?
     */
    @Test
    public void testUpdateByPrimaryKey() {
        //1.创建用于测试的实体类
        Employee emp = new Employee(8"empNewName"100.0023);
        //2. 执行更新
        Integer result = employeeService.updateEmployee(emp);
        //3.打印结果
        System.out.println("result = " + result);
        System.out.println(emp);
    }

    public Integer updateEmployee(Employee emp) {
        return employeeMapper.updateByPrimaryKey(emp);
    }

2、updateByPrimaryKeySelective(T record)

说明:根据主键更新属性不为null的值

案例:

    /**
     *  UPDATE tabple_emp SET emp_id = emp_id,emp_name = ?,emp_age = ? WHERE emp_id = ?
     *  只会修改非空的字段信息,其他没有传递的字段信息保持不变
     */
    @Test
    public void testUpdateByPrimaryKeySelective() {
        //1.创建用于测试的实体类
        Employee emp = new Employee(9"empNewName"null23);
        //2. 执行更新
        Integer result = employeeService.updateEmployeeSelective(emp);
        //3.打印结果
        System.out.println("result = " + result);
    }
    public Integer updateEmployeeSelective(Employee emp) {
        return employeeMapper.updateByPrimaryKeySelective(emp);
    }

四、基本删除

1、delete(T record) 方法

说明:根据实体属性作为条件进行删除,查询条件使用等号

案例:

    @Test
    public void testDelete() {
        //1.声明实体类变量作为查询条件
        Employee emp = null;
        //2. 执行删除
        Integer result = employeeService.removeEmp(emp);
        //3.打印结果
        System.out.println("result = " + result);
    }
    public Integer removeEmp(Employee emp) {
        return employeeMapper.delete(emp);
    }

执行的 SQL语句:

delete from tabple_emp

2、deleteByPrimaryKey(Object key)

说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

案例:

    /**
     * DELETE FROM tabple_emp WHERE emp_id = ?
     */
    @Test
    public void testDeleteByPrimaryKey() {
        //1.提供主键值
        Integer id = 9;
        //2. 执行删除
        Integer result = employeeService.removeEmpById(id);
        //3.打印结果
        System.out.println("result = " + result);
    }

    public Integer removeEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }
posted on   格物致知_Tony  阅读(200)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示

目录导航