SpringBoot整合MyBatis(增删改查)(三)

SpringBoot整合MyBatis
在Spring+SpringMVC中整合MyBatis步骤还是有点复杂的,要配置多个Bean,Spring Boot中对此做了进一步的简化,使MyBatis基本上可以做到开箱即用,本文就来看看在Spring Boot中MyBatis要如何使用。

  1. 工程创建
    首先springboot工程,添加web,mybatis,mysql依赖
    SpringBoot整合MyBatis(增删改查)(三)_第1张图片
    创建成功添加druid连接池
 
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.1version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.21version>
        dependency>

首先在application.properties中配置数据库的基本信息:

server.port=8080
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
#扫描xml
mybatis.mapper-locations= classpath:mapper/*.xml

项目结构
SpringBoot整合MyBatis(增删改查)(三)_第2张图片
实体类entity

package com.song.entity;

/**
 * @author song
 * @data 2020/3/1
 */
public class Dept {
    private Integer deptno;
    private String dname;
    private String loc;

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}';
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

mapper 记得加(@Mapper注解)

@Mapper
public interface DeptMapper {
    int addDept(Dept dept);
    int deleteById(Integer deptno);
    int updateDeptById(Dept dept);
    List<Dept> findAllDept();
}

service层

public interface DeptService {
    int addDept(Dept dept);
    int deleteById(Integer id);
    int updateDeptById(Dept dept);
    List<Dept> findAllDept();
}

serviceimpl(@Service注解)

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public int addDept(Dept dept) {
        return deptMapper.addDept(dept);
    }

    @Override
    public int deleteById(Integer id) {
        return deptMapper.deleteById(id);
    }

    @Override
    public int updateDeptById(Dept dept) {
        return deptMapper.updateDeptById(dept);
    }

    @Override
    public List<Dept> findAllDept() {
        return deptMapper.findAllDept();
    }
}

mapper.xml

<mapper namespace="com.song.mapper.DeptMapper">

<insert id="addDept" parameterType="com.song.entity.Dept">
    insert into dept(dname,loc)values (#{dname},#{loc})
insert>

<delete id="deleteById">
    delete from dept where deptno=#{deptno}
delete>

<update id="updateDeptById" parameterType="com.song.entity.Dept">
    update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
update>

<select id="findAllDept" resultType="com.song.entity.Dept" >
    select*from dept
select>
mapper>

测试增删改查

@SpringBootTest
class Demo01ApplicationTests {

    @Autowired
    private DeptService deptService;
    //查询
    @Test
    void select() {
        List<Dept> allDept = deptService.findAllDept();
        System.out.println(allDept);
    }
    //添加
    @Test
    void add() {
        Dept dept = new Dept();
        dept.setDname("研发部");
        dept.setLoc("10楼");
        int i = deptService.addDept(dept);
        System.out.println("添加成功");
    }
    //修改
    @Test
    void update() {
        Dept dept = new Dept();
        dept.setDeptno(1);
        dept.setDname("你好");
        dept.setLoc("hello");
        int i = deptService.updateDeptById(dept);
        System.out.println("修改成功");
    }
    //删除
    @Test
    void delete() {

        int i = deptService.deleteById(2);
        System.out.println("删除成功");
    }

}

数据库字段
SpringBoot整合MyBatis(增删改查)(三)_第3张图片
好了,到这里简单的增删改查就完成了

你可能感兴趣的:(SpringBoot)