SpringBoot 学习笔记 -- [spring Boot集成阿里Druid数据源,整合Mybatis搭建一个案例试试]
Posted 小智RE0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 学习笔记 -- [spring Boot集成阿里Druid数据源,整合Mybatis搭建一个案例试试]相关的知识,希望对你有一定的参考价值。
1.springboot整合阿里数据源
首先在pom.xml文件中导入依赖坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
在application.yml
文件中进行配置;
#修改端口号
server:
port: 5277
#jdbc连接以及数据源配置;
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/firstweb_db?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
initialSize: 5
minIdle: 1
maxActive: 20
创建包config
创建配置类DruidDataSourceConfig
package com.xiaozhi.backserver.startspringboot.config;
import javax.sql.DataSource;
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 com.alibaba.druid.pool.DruidDataSource;
//该注解表示这是一个配置类;
@Configuration
public class DruidDataSourceConfig
//
//配置读取配置文件中spring.datasource下的配置值
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druid()
return new DruidDataSource();
//注册标注哪些IP可以访问druid的SQL监控功能;
@Bean
public ServletRegistrationBean druidStatViewServlet()
//ServletRegistrationBean提供类的进行注册
ServletRegistrationBean servletRegistrationBean =
new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//添加初始化参数:initParams
//白名单:
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//IP黑名单(同时存在时,deny优先于allow)
//如果满足deny,就提示:sorry,you are not permitted to view this page
servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
//登录查看信息的账号和密码
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
@Bean
public FilterRegistrationBean druidStatFilter()
FilterRegistrationBean filterRegistrationBean =
new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
//添加需要忽略的格式信息
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif," +
"*.jpg,*.png, *.css,*.ico,/druid/*");
return filterRegistrationBean;
启动这个小demo后;
注意访问http://localhost:5277/druid/login.html
,就会到达这个druid连接池的SQL监控页面;
输入配置类中设置的账户和密码;就能监控到此时项目中的动态了;用了SQL语句他就可以检测到;
比如我访问这个
就被它捕捉到了;像这样的页面可以搭配到项目的后台管理页面,达到sql监控管理的效果;
2.springboot 整合Mybatis
同样地,在pom.xml文件中加入依赖
<!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--lombok组件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
在application.yml
中进行配置,顺便将日志打印也配置进入;
注意type-aliases-package
的包扫描路径是实体类的包;
mapper-locations
的配置路径会对应着resources目录的mapper文件夹
level
的配置是持久层dao的包路径
#集成mybatis;
mybatis:
type-aliases-package: com.xiaozhi.backserver.startspringboot.model
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
# 日志打印;
logging:
level:
com.xiaozhi.backserver.startspringboot.dao: trace
file:
name: E:\\\\logs\\\\log.log
3.试试案例测试
创建model
包,存放实体类
package com.xiaozhi.backserver.startspringboot.model;
import lombok.Data;
import org.springframework.stereotype.Component;
/**
* @author by @CSDN 小智RE0
* @date 2021-12-25
* 用户类
*/
@Data
@Component
public class User
//账户和密码
private String account;
private String password;
在resources
目录下创建mapper
文件夹;
创建用户持久层映射配置文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--注意 这里对应空间为持久层映射接口-->
<mapper namespace="com.xiaozhi.backserver.startspringboot.dao.UserDao">
<!--添加用户-->
<insert id="saveUser">
insert into tb_user(account,password) values (#account,#password)
</insert>
</mapper>
创建dao
包持久层;
创建UserDao
接口
package com.xiaozhi.backserver.startspringboot.dao;
import com.xiaozhi.backserver.startspringboot.model.User;
import org.springframework.stereotype.Repository;
/**
* @author by @CSDN 小智RE0
* @date 2021-12-25
* 用户类持久层
*/
@Repository
public interface UserDao
//保存用户信息的方法;
void saveUser(User user);
创建service
服务层
创建UserService
package com.xiaozhi.backserver.startspringboot.service;
import com.xiaozhi.backserver.startspringboot.dao.UserDao;
import com.xiaozhi.backserver.startspringboot.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author by @CSDN 小智RE0
* @date 2021-12-25
*/
@Transactional
@Service
public class UserService
@Autowired
UserDao userDao;
//保存用户信息
public void saveUser(User user)
userDao.saveUser(user);
创建控制层controller
包;
创建UserController
类;
package com.xiaozhi.backserver.startspringboot.controller;
import com.xiaozhi.backserver.startspringboot.model.User;
import com.xiaozhi.backserver.startspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author by @CSDN 小智RE0
* @date 2021-12-23
*/
@RestController
@RequestMapping(value = "/hello")
public class UserController
@Autowired
UserService userService;
//测试保存用户方法;
@RequestMapping("/saveUser")
public String saveUser(User user)
userService.saveUser(user);
System.out.println("输出用户信息-->"+user);
return "用户信息保存中---!!!!";
注意启动类这里有个重要的注解
扫描指定的持久层包
@MapperScan(“com.xiaozhi.backserver.startspringboot.dao”)
package com.xiaozhi.backserver.startspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.xiaozhi.backserver.startspringboot.dao")
@SpringBootApplication
public class StartspringbootApplication
public static void main(String[] args)
SpringApplication.run(StartspringbootApplication.class, args);
启动;
访问http://127.0.0.1:5277/hello/saveUser?account=%E5%B0%8F%E6%99%BARE0&password=5271314xi
可以试试将项目打包到本地运行
比如我打包后,放到D盘试试
在D盘输入cmd
命令
回车来到命令窗口页面;
输入java -jar 5277demo.jar
这时就启动了这个demo
可在浏览器访问;可成功访问
以上是关于SpringBoot 学习笔记 -- [spring Boot集成阿里Druid数据源,整合Mybatis搭建一个案例试试]的主要内容,如果未能解决你的问题,请参考以下文章