梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

Spring Boot 2.2.8 整合JUnit-JPA-Redis

0. 如果要覆盖默认配置,可以在 *-autoconfigure* 里面的 META-INF/spring-configuration-metadata.json中找

1. SpringBoot整合JUnit

 

开始@RunWith出不来,我换成了SpringBoot 2.1.12,然后再切回 2.2.8就可以识别了.

如果引导类的包和测试类的包一模一样的话,@SpringBootTest里面的classes可以省略.

 

2. SpringBoot整合JPA

 

3. SpringBoot整合Redis

 

 4. SpringBoot整合Mybatis

    引入mybatis起步依赖,添加mysql驱动

<dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</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>

   编写DataSource和MyBatis相关配置

# datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径(xml形式)
  type-aliases-package: com.itheima.springbootmybatis.domain #实体类扫描包路径

  # config-location:  # 指定mybatis的核心配置文件

    定义表和实体类

public class User {
    private int id;
    private String username;
    private String password;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

   编写dao和mapper文件(的方式)

      编写dao

@Mapper
@Repository
public interface UserXmlMapper {

    public List<User> findAll();
}

     mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.springbootmybatis.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select * from t_user
    </select>
</mapper>

   纯注解开发

@Mapper
@Repository
public interface UserMapper {

    @Select("select * from t_user")
    public List<User> findAll();
}

 

posted on 2020-07-04 13:49  梦相随1006  阅读(196)  评论(0编辑  收藏  举报