mybatis-spring-boot-starter 1.3.0 操作实体类的SpringBoot例子

例程下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200428-02.zip

 

需求:使用mybatis实现对hy_emp表的CRUD。

实现步骤:

1.添加依赖

        <!-- mybatis -->
        <dependency>  
            <groupId>org.mybatis.spring.boot</groupId>  
            <artifactId>mybatis-spring-boot-starter</artifactId>  
            <version>1.3.0</version>  
        </dependency>  

注:《SpringBoot实战派》P201提供的写法,version2.0.0会报错,故而改成1.3.0. 

 

2.书写表对应的实体类。

package com.ufo.gatling.entity;

public class Emp {
    private long id;

    private String name;
    
    private int salary;
    
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
}

 

3.书写Mapper接口

package com.ufo.gatling.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.ufo.gatling.entity.Emp;

@Mapper
public interface EmpMapper {
    @Select("select * from hy_emp where id=#{id}")
    Emp findById(@Param("id") long id);
    
    @Select("select * from hy_emp")
    List<Emp> findAll();
    
    @Update("update hy_emp set name=#{name},salary=#{salary} where id=#{id}")
    int updateById(Emp emp);
    
    @Delete("delete from hy_emp where id=#{id}")
    int deleteById(Emp emp);
    
    @Insert("insert into hy_emp(id,name,salary) values(#{id},#{name},#{salary})")
    int insert(Emp emp);
    
    @SelectProvider(type=EmpSql.class,method="findHighLevel")
    List<Emp> findHighLevelEmps();
}

这种写法把SQL和接口写到了一起,比早期MyBatis版本的分两个文件明白多了(早期项目里通过Java类查找翻到接口,再去找同名xml实在是有够反人类)。如果SQL够长够复杂则可以通过SelectProvider的方式,分到一个文件里去好好写,下面就是涉及到的EmpSql类。

 

4.EmpSQl类:

package com.ufo.gatling.mapper;

public class EmpSql {
    public String findHighLevel() {
        return "select * from hy_emp where salary>15000";
    }
}

有人会说这个也不复杂,例子当然不复杂,真写起项目来想不复杂都不行。

 

5.测试类,请单个函数调试或执行。

package com.ufo.gatling;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.ufo.gatling.entity.Emp;
import com.ufo.gatling.mapper.EmpMapper;

@SpringBootTest
class GatlingApplicationTests {
    @Autowired
    private EmpMapper mapper=null;

    @Test
    void test01_findById() {
        Emp emp=mapper.findById(6);
        assertEquals("Felix", emp.getName());
        assertEquals(20000, emp.getSalary());
    }
    
    @Test
    void test02_findAll() {
        List<Emp> empList=mapper.findAll();
        assertEquals(6, empList.size());
    }
    
    @Test
    void test03_updateById() {
        Emp emp=new Emp();
        emp.setId(1);
        emp.setName("Gates");
        emp.setSalary(123456);
        
        int changedCount=mapper.updateById(emp);
        assertEquals(1, changedCount);
        
        Emp found=mapper.findById(1);
        assertEquals("Gates", found.getName());
        assertEquals(123456, found.getSalary());
    }
    
    @Test
    void test04_deleteById() {
        Emp emp=new Emp();
        emp.setId(3);
        
        int changedCount=mapper.deleteById(emp);
        assertEquals(1, changedCount);
        
        emp=mapper.findById(3);
        assertEquals(null, emp);
    }
    
    @Test
    void test05_insert() {
        Emp emp=new Emp();
        emp.setId(7);
        emp.setName("Bear");
        emp.setSalary(300000);
        
        int changedCount=mapper.insert(emp);
        assertEquals(1, changedCount);
        
        Emp found=mapper.findById(7);
        assertEquals("Bear", found.getName());
        assertEquals(300000, found.getSalary());
    }
    
    @Test
    void test06_findHighLevelEmps() {
        List<Emp> empList=mapper.findHighLevelEmps();
        assertEquals(4, empList.size());
    }
}

 

好了,全文就到这里,具体细节请下载代码。

Java无论是编码还是配置看网文或是书籍都觉得简单,自己动手一操作发现很多细节挡害,不用心解决都执行不下去。

真是纸上得来终觉浅,绝知此事要躬行。

--2020-04-28--

续篇:https://www.cnblogs.com/heyang78/p/15229609.html

posted @ 2020-04-28 13:46  逆火狂飙  阅读(574)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东