数据库
CREATE DATABASE test;
USE test;
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL COMMENT '用户名',
`create_time` DATETIME DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户';
项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.dyp dyp-test 0.0.1-SNAPSHOTdyp-testDemo project for Spring BootjarUTF-8UTF-81.8org.springframework.boot spring-boot-starter-parent 2.0.0.BUILD-SNAPSHOTorg.springframework.boot spring-boot-starter-web com.baomidou mybatisplus-spring-boot-starter 1.0.4com.baomidou mybatis-plus 2.0.7org.apache.velocity velocity 1.7org.freemarker freemarker 2.3.28org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java com.alibaba druid 1.1.9org.projectlombok lombok 0.10.1providedorg.springframework.boot spring-boot-maven-plugin spring-snapshotshttp://repo.spring.io/snapshottruespring-milestoneshttp://repo.spring.io/milestonespring-snapshotshttp://repo.spring.io/snapshotspring-milestoneshttp://repo.spring.io/milestone
application.yml 相关的配置
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true username: root password: root druid: initial-size: 5 min-idle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 web-stat-filter: enabled: true url-pattern: "/*" exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" # DruidStatViewServlet stat-view-servlet: url-pattern: "/druid/*" allow: 127.0.0.1,192.168.1.1 deny: 192.168.1.1 reset-enable: false login-username: admin login-password: 123456mybatis-plus: mapper-locations: classpath:/com/dyp/dyptest/mapper/*/*.xml typeAliasesPackage: com.dyp.dyptest.entity global-config: id-type: 2 field-strategy: 2 db-column-underline: true refresh-mapper: true configuration: map-underscore-to-camel-case: true cache-enabled: falseserver: port: 8082
com.dyp.dyptest.config相关配置
package com.dyp.dyptest.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/** * 数据源配置 */@Configurationpublic class DataSourcesConfig { @Bean(name="dataSource") @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource(){ return new DruidDataSource(); } // 配置事物管理器 @Bean(name="transactionManager") public DataSourceTransactionManager transactionManager(){ return new DataSourceTransactionManager(dataSource()); }}
package com.dyp.dyptest.config;import com.alibaba.druid.support.http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class DruidConfiguration { @Bean public ServletRegistrationBean startViewServlet(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); // IP白名单 servletRegistrationBean.addInitParameter("allow