搭建基于Springboot2.0,Druid 1.1,Jpa demo

使用环境

Springboot版本 springboot 2.1.2,Mysql版本 8.0.14 ,Druid版本1.1.10,Jpa版本2.0.4,Jdk版本为1.8,Idea为IntelliJ IDEA 2018.2.1 x64

数据库创建

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL,
  `password` varchar(12) NOT NULL,
  `user_name` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) 

INSERT INTO `user` VALUES ('1', 'abe1', 'abe');

创建Springboot项目

首先打开Idea,创建project,如图搭建基于Springboot2.0,Druid 1.1,Jpa demo
然后点击next按钮

搭建基于Springboot2.0,Druid 1.1,Jpa demo
输入组织名和包名;点击下一步
搭建基于Springboot2.0,Druid 1.1,Jpa demo
选择Web,再点击Web,然后点击Next按钮,在跳出页面的输入项目名和项目存放地址即可点击完成。

功能整合

打开创建后的项目,搭建基于Springboot2.0,Druid 1.1,Jpa demo
找到pom.xml文件,添加相应的依赖包

      <!--jpa依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
        <!--Druid starter包依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--Mysql8.0驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

然后点击import changes导入依赖;成功导入依赖后打开src目录,找到main目录下的resources文件夹,这里有一个application.properties文件,由于我们这里采用的YML文件,所以把这个文件改为.yml文件,然后再新建一个application-dev.yml文件,具体结果如下:
搭建基于Springboot2.0,Druid 1.1,Jpa demo
然后打开application.yml,进行编辑(这里这样设置是方便以后切换生产,开发,测试环境),

spring:
  profiles:
    active: dev

然后再打开application-dev.yml,进行编辑,这里采用的是Mysql8.0 ,数据库的链接方式跟5.7有所变化。

##端口号配置
server:
  port: 8080

---

spring:
  ##druid数据源配置
  datasource:
    druid:
      # 数据库访问配置, 使用druid数据源
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/Test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
      username: test
      password: test123
      # 连接池配置
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 连接等待超时时间
      max-wait: 30000
      # 配置检测可以关闭的空闲连接间隔时间
      time-between-eviction-runs-millis: 60000
      
##Jpa配置
  jpa:
    hibernate:
      ddl-auto: update
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true

      

配置完毕后开始创建包结构总体结构如下图
搭建基于Springboot2.0,Druid 1.1,Jpa demo
然后编写User 实体类

package com.test.demo.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="user")
public class User {
    @Id
    private Long id;
    @Column(length=32)
    @NotNull
    private  String userName;
    @Column(length=12)
    @NotNull
    private String password;
    public Long getId() {
        return id;
    }
    public void setId(Long 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;
    }

}

UserDao

package com.test.demo.Dao;

import com.test.demo.Entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User,Long> {

    User getUserByUserName(String userName);

}


UserService

package com.test.demo.Service;

import com.test.demo.Entity.User;

public interface UserService {
     User findUserByUserName(String userName);
}


UserServicImpl


package com.test.demo.Service.UserServiceImpl;

import com.test.demo.Dao.UserDao;
import com.test.demo.Entity.User;
import com.test.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public User findUserByUserName(String userName) {
        return userDao.getUserByUserName(userName);
    }
}

UserController

package com.test.demo.Controller;


import com.test.demo.Entity.User;
import com.test.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/{userName}", method = RequestMethod.GET)
    public User getUserByName(@PathVariable String userName){
        return userService.findUserByUserName(userName);
    }
}

结果:
搭建基于Springboot2.0,Druid 1.1,Jpa demo

这样一个简单的基于Springboot,JPA,Druid连接池的Demo就搭建完成了,我会在下次博客上增加
redis,swagger2的集成。
项目地址:
https://download.csdn.net/download/qq_37925580/11013116

这是本人第一篇博客,可能有很多写的不太好,希望大家能共同交流一起进步