0
点赞
收藏
分享

微信扫一扫

SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问 整合MyBatis(一) 20190221

鲤鱼打个滚 2022-02-07 阅读 19


一、Srpring MyBatis


  1. 创建项目
    SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_spring
    SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_apache_02
    SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_mysql_03
    SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_spring_04
  2. pom.xml

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_spring_05


  1. 创建数据库
    SpringBoot自学好几天 中途开始写笔记 SpringBoot数据访问  整合MyBatis(一)   20190221_apache_06
  2. 引入druid
    pom.xml

<!--druid数据源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

application.yml

spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

#数据源其他配置 默认不会使用以下属性 因为没有对应到属性文件 需要自己配置DuridConfig
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true

#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 见DuridConfig
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

编写配置类

package com.example.springboot.config;



import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
* @author LM
* @create 2019-02-19 21:44
*/
@Configuration
public class DuridConfig {


@ConfigurationProperties
@Bean
public DataSource durid(){
return new DruidDataSource();
}


//配置druid监控
//1.配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

//StatViewServlet 初始化参数 父类就有ResourceServlet需要哪些参数
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
initParams.put("allow","");//不写 默认所有
initParams.put("deny","192.168.2.111"); //拒绝本机地址访问
bean.setInitParameters(initParams);
return bean;
}
//2.配置一个web监控的Filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
//初始化参数
Map<String,String> initParams = new HashMap<>();
//哪些不拦截
initParams.put("exclusions","*js,*css,/druid/*");
bean.setInitParameters(initParams);
//拦截请求
bean.setUrlPatterns(Arrays.asList("/*"));//拦截所有请求

return bean;
}
}
  1. 建表
/*
Navicat MySQL Data Transfer

Source Server : 本地
Source Server Version : 50528
Source Host : 127.0.0.1:3306
Source Database : restful_crud

Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001

Date: 2018-03-05 10:41:40
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
/*
Navicat MySQL Data Transfer

Source Server : 本地
Source Server Version : 50528
Source Host : 127.0.0.1:3306
Source Database : restful_crud

Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001

Date: 2018-03-05 10:41:58
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`gender` int(2) DEFAULT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

报错了 记录一下

Description:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:

Property: spring.datasource.filters
Value: stat,wall,log4j
Origin: class path resource [application.yml]:27:14
Reason: org.apache.log4j.Logger
Action:

Update your application's configuration

解决方法: 引入org.apache.log4j.Logger依赖包

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
  1. 错误
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
log4j:WARN No appenders could be found for logger (druid.sql.Connection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

解决方式:

最新的mysql驱动包 引入方式不对

com.mysql.jdbc.Driver 换成 com.mysql.cj.jdbc.Driver


  1. 错误
    log4j:WARN No appenders could be found for logger (druid.sql.Connection).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    解决方式:
    配置log4j的日志配置文件log4j.properties
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log

log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

#log4j.logger.自己的包名=DEBUG
log4j.logger.com.example.springboot=DEBUG

创建bean
Department

package com.example.springboot.bean;

/**
* @author LM
* @create 2019-02-20 22:34
*/
public class Department {
private Integer id;
private String departmentName;

public Integer getId() {
return id;
}

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

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}

Employee

package com.example.springboot.bean;

/**
* @author LM
* @create 2019-02-20 22:34
*/
public class Department {
private Integer id;
private String departmentName;

public Integer getId() {
return id;
}

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

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}



举报

相关推荐

0 条评论