美文网首页
spring boot (二)

spring boot (二)

作者: 席坤 | 来源:发表于2019-07-16 17:56 被阅读0次

前言

上篇我们搭建了基础项目,现在我们加入mybatis,实现以下spring-boot使用mybatis完成一个基本的增删改查。

实现

1.添加pom文件依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
   </dependencies>

2.配置yml文件

server:
  port: 8088
  servlet:
    context-path: /sell
spring:
  datasource:
    url: jdbc:mysql://localhost/sell?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: a123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  http:
    encoding:
      charset: UTF-8
      enabled: true
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
logging:
  level:
    com.example.mybatis.mapper: trace
mybatis:
  mapper-locations: classpath:mapper/*.xml
  • mapper-locations 为是配置我们mapper.xml文件的位置,我们把它配置在/src/main/resource/mybatis/mapper目录下。

3.添加扫描路径

package com.example.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.example.mybatis.mapper")
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }

}

  • com.example.mybatis.mapper 为我们配置mapper的地方,我配置在/src/main/com.example.mybatis/mapper

4.添加实体类

@Data
public class MenuModal {
    private Integer id;

    private String menuName;

    private String menuIcon;

    private String link;

    private Integer pid;

    private List<Menu> children;
}

5.编写mapper文件

@Mapper
public interface MenuMapper {

    List<Menu> selectAll();

    List<MenuModal> selectTree(Integer id);

    int deleteByPrimaryKey(Integer id);

    int insert(Menu record);

    int insertSelective(MenuModal record);

    MenuModal selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(MenuModal record);

    int updateByPrimaryKey(MenuModal record);

}

跟我们之前定义的mapper.xml文件的位置,我们在resource/mybatis/mapper目录下 新建与之对应的MenuMapper.xml


mapper.png

6.测试

这次我们用spring boot的单元测试来测试我们的接口。
在idea中 使用快捷键新建测试类ctrl+shift+t

勾选需要测试的方法,并且点击ok
这时候会在src/test/java目录下对应的包路径新建对应的测试文件
代码如下

![text.png](https://img.haomeiwen.com/i2919971/11ce6da664a31eed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:spring boot (二)

      本文链接:https://www.haomeiwen.com/subject/njmtlctx.html