springboot + mybatis +druid

Druid Spring Boot Starter

mybatis-spring-boot-autoconfigure

新建spring boot工程,添加pom依赖

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.6</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-web</artifactId>
        </dependency>

新增User类

public class User {
    private Integer id;
    private String name;
    private Integer sex;
    private Integer age;

    public User(String name, Integer sex, Integer age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public User(Integer id, String name, Integer sex, Integer age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public User() {
    }

    public Integer getId() {

        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

新增UserMapper接口

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select * from person where id=#{id}")
    User findById(@Param("id") Integer id);
}

新增UserController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index() {
        User user=userMapper.findById(1);
        return user.getName();
    }
}

配置文件

spring.datasource.url= jdbc:mysql://192.168.31.146:3306/mydb
spring.datasource.username= root
spring.datasource.password= weiwei1207

spring.datasource.druid.url= jdbc:mysql://192.168.31.146:3306/mydb
spring.datasource.druid.username= root
spring.datasource.druid.password= pass

spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=60000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#spring.datasource.druid.max-open-prepared-statements=
spring.datasource.druid.validation-query=select 1 from dual
#spring.datasource.druid.validation-query-timeout=
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
#spring.datasource.druid.max-evictable-idle-time-millis=
#配置多个英文逗号分隔
spring.datasource.druid.filters=stat,wall,log4j

启动应用后打开浏览器:http://localhost:8080/druid/index.html

再打开:http://localhost:8080/hello

在druid界面查看sql

原文地址:https://www.cnblogs.com/uptothesky/p/8213537.html

时间: 2024-07-28 17:50:33

springboot + mybatis +druid的相关文章

springboot+mybatis+Druid配置多数据源(mysql+postgre)

springboot+mybatis+Druid配置多数据源(mysql+postgre)引入pom依赖设置application多数据源config配置db1config配置(主数据库配置)db2config配置(其他数据库)事务处理mapper层 springboot+mybatis+Druid配置多数据源(mysql+postgre) 参考资料: 第八章 springboot + mybatis + 多数据源 springboot + mybatis + druid + 多数据源 spri

基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Spring MVC+Mybatis),但是发现配置实在过于复杂,好多东西配置起来麻烦,虽然最终是配置出来了,但是还是感觉开发速度跟不上,本来打算切换到jfianl,但是后来发现需要用的几个框架不支持jfianl,如Swagger2(根据代码中的注解生成接口文档和测试页面,非常的方便):同时我也不愿意放弃SpringMVC强大的验证参数模块,jfianl中好像只能手动验证(当然我对jfianl只处于简单的开发,并不是特别熟),而Sp

SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页

前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了,这里就不过多说明了.重点是讲述在多数据源下的如何配置使用Druid和PageHelper . Druid介绍和使用 在使用Druid之前,先来简单的了解下Druid. Druid是一个数据库连接池.Druid可以说是目前最好的数据库连接池!因其优秀的功能.性能和扩展性方面,深受开发人员的青睐.Dr

springboot+mybatis+druid数据库连接池

参考博客https://blog.csdn.net/liuxiao723846/article/details/80456025 1.先在pom.xml中引入druid依赖包 <!-- 连接池 --> <!-- Druid 数据连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>

SpringBoot+Mybatis+Druid批量更新 multi-statement not allow异常

本文链接:https://blog.csdn.net/weixin_43947588/article/details/90109325 注:该文是本博主记录学习之用,没有太多详细的讲解,敬请谅解! 在日常的开发过程中难免会有批量操作的功能,Mybatis集成Druid批量更新时经常会出现Error updating database. Cause: java.sql.SQLException: sql injection violation, multi-statement not allow

第五章 springboot + mybatis(转载)

本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml 1 <!-- 与数据库操作相关的依赖 --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4

第五章 springboot + mybatis

springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成.集成方式相当简单. 1.项目结构 2.pom.xml 1 <!-- 与数据库操作相关的依赖 --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-jdbc</artifact

xlauch 1.0 基于springboot + mybatis + beetls 快速开发脚手架

xlauch xlauch 是基于springboot + mybatis + beetls 快速开发脚手架, 包含了用户管理,组织机构管理,角色管理,功能点管理,菜单管理,权限分配,数据权限分配,代码生成,二次开发等功能 系统基于Spring Boot 1.5技术,前端采用了easyUI.数据库以MySQL为实例 . QQ 交流群(224708661) gitee下载地址:[email protected]:huangxy3/xlauch.git 1 使用说明 1.1 安装说明 1.1.1 导

SpringBoot+Mybatis集成搭建

本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了xml的其实也可以用xml来实现的,实现上具体用什么方式并不重要,主要是搭建一遍,对框架的运转就比较清晰.本博客还是用xml的方式来实现Mybatis的sql编写,不用注解方式. maven配置 <!-- springboot mybatis--> <dependency> <gr