First, create a project and build environment:
1) Create maven coordinate the project needed
This mybatis the starter is out mybatis official adapt springboot
FIG be learned by this dependence is introduced which packages
<!--引入druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
Spring:
DataSource:
# basic configuration of the data source
username: the root
password: 123
Driver-class-name: com.mysql.jdbc.Driver
URL: JDBC: MySQL: // localhost: 3306 / springbootmybatis
type: com.alibaba.druid.pool .DruidDataSource
# data sources other configuration
initialSize:. 5
minIdle:. 5
for maxActive: 20 is
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: the SELECT the FROM. 1 the DUAL
testWhileIdle: to true
testOnBorrow: to false
testOnReturn: to false
poolPreparedStatements: to true
# configure the monitoring statistics interception filters after removing the monitoring interface sql not statistics, 'wall' for firewall
Filters: STAT, Wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
4, the data source is added to the vessel, only the configuration data from the configuration of monitoring control effect +
@Configuration public class DruidConfig { @ConfigurationProperties (prefix = " spring.datasource " ) @Bean public the DataSource Druid () { return new new DruidDataSource (); } // arranged to monitor data sources Durid // 1, a configuration management background of the Servlet @ bean public ServletRegistrationBean statViewServlet () { ServletRegistrationBean the bean = new new ServletRegistrationBean ( new new StatViewServlet (), "/ Druid / *" ); the Map <String, String> = the InitParameters new newThe HashMap <> (); initParameters.put ( "loginUsername", "the root" ); initParameters.put ( "loginPassword", "123" ); // allow access, the default can access all initParameters.put ( "allow", ""); // default is to allow all access // to prevent access initParameters.put ( "deny", "192.168.15.21" ); // set the initialization parameters bean.setInitParameters (the InitParameters); return bean; } // 2, a configuration monitoring the Filter @Bean public FilterRegistrationBean webStatFilter () { FilterRegistrationBean the bean =new new FilterRegistrationBean (); bean.setFilter ( new new WebStatFilter ()); the Map <String, String> = the InitParameters new new the HashMap <> (); // excluded intercepted request . initParameters.put ( "exclusions", " * js, * CSS, / Druid / * " ); // set the initialization parameters bean.setInitParameters (the InitParameters); // intercepted request bean.setUrlPatterns (Arrays.asList (" / * " )); return the bean; } }
5. After the above configuration to run these applications to access druid of login.html page to see if there is configured successfully
6, create a database table
application.yml configuration ( the red part followed by a need to add springboot2.x )
Spring:
DataSource:
# basic configuration of the data source
username: the root
password: 123
Driver-class-name: com.mysql.cj.jdbc.Driver
URL: JDBC: MySQL: // localhost: 3306 / springbootmybatis serverTimezone = UTC?
type: COM .alibaba.druid.pool.DruidDataSource
# data sources other configuration
initialSize:. 5
minIdle:. 5
for maxActive: 20 is
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: the SELECT the FROM. 1 the DUAL
testWhileIdle: to true
testOnBorrow: to false
testOnReturn: to false
poolPreparedStatements: to true
# Configure monitoring statistics intercepted filters, after removing the monitoring interface sql not statistics, 'wall' for Firewall
Filters: STAT, Wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: to true
ConnectionProperties: druid.stat.mergeSql = to true; druid.stat.slowSqlMillis = 500 Schema:
- CLASSPATH: SQL / department.sql
- CLASSPATH: SQL / employee.sql Initialization-MODE: Always
sql file will be built into the table specified path
At this point after starting the project has been built database tables
After put application.yml table built in the schema: configuration commented out; otherwise every time you start the project will then delete the original table New Table
7, create the corresponding javabean class
public class Employee { private Integer id; private String lastName; private Integer gender; private String email; private Integer dId; ..... }
public class Department { private Integer id; private String departmentName; ... }