SpringBoot 数据访问操作

目录

一.SpringBoot整合Mybatis与Mybatis-Plus

二.SpringBoot切换druid数据源

3.1DRUID配置参数

3.2Druid监控平台


一.SpringBoot整合Mybatis与Mybatis-Plus

步骤:

1.坐标

XML 复制代码
         <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
         </dependency>

注意:mp坐标添加后,mybatis坐标移除(mp坐标包含mybatis)

2.配置数据源以及mp

先导入德鲁伊数据源

XML 复制代码
        <!--druid坐标-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
XML 复制代码
#数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/库?serverTimezone=GMT
    username: root
    password: 密码
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat,wall


mybatis:
  configuration:
    map-underscore-to-camel-case: true   #自动驼峰映射
  type-aliases-package: com.apesource.pojo  #起别名


mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true #自动驼峰映射(默认开启)
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志

3.编写注解配置实体类与关系表映射关系(truncate清空表以及主键)

@TableName(value = "关联表名称")=========================》修饰在类

@TableField(value = "关联字段名称")======================》修饰在属性 ​

exist = "忽略字段"

@TableLogic(value="原值",delval="修改值")

注解参数

value = "" 未删除的值,默认值为0

delval = "" 删除后的值,默认值为1

@TableId(type="指定主键生成策略,默认雪花算法")=============》修饰在属性

AUTO(0), ​ NONE(1), ​ INPUT(2), ​ ASSIGN_ID(3), ​ ASSIGN_UUID(4);

java 复制代码
@NoArgsConstructor//表示无参构造
@AllArgsConstructor//全参构造
@Data//所有属性setget方法以及toString()
@TableName("t_student")
public class Student {
    @TableId(value = "stu_id",type = IdType.AUTO)
    private int stuId;
    @TableField(value = "stu_name")
    private String stuName;
    @TableField(value = "stu_nick")
    private String stuNick;
    @TableField(value = "stu_hobby")
    private String stuHobby;
    @TableLogic(value = "0",delval = "1")
    private int isdeleted;
    @TableField(exist = false)
    private MultipartFile mpf;

    public Student(String stuName, String stuNick, String stuHobby) {
        this.stuName = stuName;
        this.stuNick = stuNick;
        this.stuHobby = stuHobby;
    }
}

4.使用

注入mapper:

java 复制代码
@Mapper=====>注册+注入        						   作用:在mapper类上
@MapperScan(value = "mapper所在的包")=>批量注册+注入     作用:在启动类上	

BaseMapper===========》公共的数据访问层

java 复制代码
@Mapper
public interface HotelMapper extends BaseMapper<Hotel> {
}

IService/ServiceImp==》公共的业务层

java 复制代码
public interface IHotelService extends IService<Hotel> {
}
java 复制代码
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {
}

二.SpringBoot切换druid数据源

1.导入坐标

XML 复制代码
        <!--druid坐标-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>

2.做配置

XML 复制代码
#数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/库?serverTimezone=GMT
    username: root
    password: 密码
    driver-class-name: com.mysql.cj.jdbc.Driver
    #切换数据源类型
    type: com.alibaba.druid.pool.DruidDataSource
    #Druid监控平台配置
    filters: stat,wall


mybatis:
  configuration:
    map-underscore-to-camel-case: true   #自动驼峰映射
  type-aliases-package: com.apesource.pojo  #起别名


mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true #自动驼峰映射(默认开启)
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志

3.1DRUID配置参数

3.2Druid监控平台

java 复制代码
filters: stat,wall


@Configuration
public class DruidConfig {

    //1.注入数据源
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new DruidDataSource();
    }

    //2.配置servlet
    @Bean
    public ServletRegistrationBean registrationBean(){
        //1.创建servlet注册类
        ServletRegistrationBean<StatViewServlet>  servletRegistrationBean =  new ServletRegistrationBean<StatViewServlet>();
        //2.创建制作页面的servlet
        StatViewServlet statViewServlet = new StatViewServlet();
        //3.绑定servlet
        servletRegistrationBean.setServlet(statViewServlet);
        servletRegistrationBean.setUrlMappings(Arrays.asList("/druid/*"));
        //4.参数绑定
        Map<String,String> maps = new HashMap<String,String>();
        maps.put(StatViewServlet.PARAM_NAME_USERNAME,"admin");
        maps.put(StatViewServlet.PARAM_NAME_PASSWORD,"123");
        maps.put(StatViewServlet.PARAM_NAME_ALLOW,"");//白名单
        maps.put(StatViewServlet.PARAM_NAME_DENY,"192.168.0.12");//黑名单
        servletRegistrationBean.setInitParameters(maps);

        return servletRegistrationBean;
    }

    //3.配置filter
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){

        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<WebStatFilter>();
        bean.setFilter(new WebStatFilter());
        //所有请求进行监控处理
        bean.setUrlPatterns(Arrays.asList("/*"));

        Map<String, String> initPrams = new HashMap<>();
        //添加不需要忽略的格式信息
        initPrams.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*");
        bean.setInitParameters(initPrams);

        return bean;
    }

}
相关推荐
pianmian13 小时前
类(JavaBean类)和对象
java
我叫小白菜4 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison4 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍5 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122465 小时前
JAVA内存区域划分
java·开发语言·redis
Piper蛋窝5 小时前
深入 Go 语言垃圾回收:从原理到内建类型 Slice、Map 的陷阱以及为何需要 strings.Builder
后端·go
勤奋的小王同学~5 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇5 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097625 小时前
JavaEE-Linux环境部署
java·linux·java-ee
缘来是庄6 小时前
设计模式之访问者模式
java·设计模式·访问者模式