SpringBoot使用Druid与Mybatis-plus整合

需要导入的依赖

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


            <!--数据库配置-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
           <!--数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
           <!--自动化配置-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>



        <!--mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
	
			<!--简化操作-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
			<!--Web测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource #Druid类型
    druid:
      web-stat-filter: #监控Web
        enabled: true
      stat-view-servlet: #后台页面 http://localhost:8080/druid/login.html
        enabled: true
        login-username: root # 用户名
        login-password: root # 密码

写一个实体类并在数据库中创建对应的表

@Data
public class Dept {
    @TableId(type = IdType.AUTO)
    private Integer deptno;
    private String dname;
    private String loc;
}

SpringBoot使用Druid与Mybatis-plus整合_mysql

对应的Mapper

@Repository
public interface DeptMapper extends BaseMapper<Dept> {
}

写一个测试接口

@RestController
public class TestController {
    @Autowired
    DeptMapper deptMapper;

    @GetMapping("/find")
    public List<Dept> Test1(){
        return deptMapper.selectList(null);
    }
}

注意启动类上要加MapperScan 扫描对应的包。

@SpringBootApplication
@MapperScan("com.herio.mapper")
public class DemoDatasourceApplication {

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

}

测试

调用接口。

SpringBoot使用Druid与Mybatis-plus整合_java_02

登录Druid监控页面访问

http://localhost:8080/druid/login.html

SpringBoot使用Druid与Mybatis-plus整合_mysql_03


End