码迷,mamicode.com
首页 > 编程语言 > 详细

springboot + mybatis +druid

时间:2018-01-06 14:33:00      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:www.   return   tor   pid   sel   依赖   pos   map   idle   

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>
View Code

新增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;
    }
}
View Code

新增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);
}
View Code

新增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();
    }
}
View Code

配置文件

技术分享图片
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
View Code

启动应用后打开浏览器:http://localhost:8080/druid/index.html技术分享图片

 

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

在druid界面查看sql

技术分享图片

 

springboot + mybatis +druid

标签:www.   return   tor   pid   sel   依赖   pos   map   idle   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!