数据库连接池可能造成的问题

一.引例:(仍为之前连接数据库的案例)

如果出现异常NoSuchFieldError,可能是由于类不存在造成的:

在左边的target文件中classes下就缺少了User类-->导致后台无法找到对应的类


上述问题与数据库连接池有关

二.解决方案:

首先明确一点:数据库连接池与创建Springboot工程时的服务器URL有关


1.当服务器URL使用https://start.spring.io(idea默认的)时:-->无需再接数据库连接池

(但也可以更改为自己需要的数据库连接池)

**该URL是idea默认使用的URL-->因此会使用springboot工程下默认使用的Hikari数据库连接池**(追光者数据库连接池)

a.注意:

https://start.spring.io这个URL下在引入驱动包时,比如Spring Boot的3.3.0版本以上(包括3.3.0版本)会不支持Mybatis Framework这个驱动包,因此使用该驱动包时需要3.3.0以下的版本

别的驱动包根据需求选择


b.数据库连接信息:

代码:

spring.application.name=spring_Test
​
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
​
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
​
spring.datasource.username=root
​
spring.datasource.password=1234

c.pom.xml文件:如果要用lombok,只需要引入lombok依赖即可-->位置千万别放错了(放对了左侧会有蓝色 标志提醒)


d.引入lombok依赖后实体类可用注解简化:

简化的内容可通过查看左下角target文件下的User类查看

代码:

package com.springtest.spring_test.pojo;
​
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
​
@Data
@NoArgsConstructor //无参构造
@AllArgsConstructor //全参构造
public class User {
    private Integer age;
    private String name;
}

e.UserMapper接口代码:
package com.springtest.spring_test.mapper;
​
import com.springtest.spring_test.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
​
import java.util.List;
​
@Mapper
public interface UserMapper {
    @Select("select * from student")
    public List<User> list();
}


f.student数据库:

源代码:

-- auto-generated definition
create table student
(
    age  tinyint unsigned not null comment '年龄',
    name varchar(10)      not null comment '姓名'
)
    collate = utf8mb4_0900_ai_ci;
​

g.测试类:

代码:

package com.springtest.spring_test;
​
import com.springtest.spring_test.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
​
@SpringBootTest
class SpringTestApplicationTests {
​
    @Test
    void contextLoads() {
        User u=new User(12,"zhangsan");
        System.out.println(u);
    }
}
​

h.运行结果:


i.包的结构:target文件下必须要出现实体类


2.当服务器URL使用https://start.aliyun.com时:

https://start.aliyun.com为阿里服务器,使用该服务器时,最好把数据库连接池更改为Druid数据库连接池(阿里公司开发的一种高效数据库连接池),

不然可能出现不兼容的情况,导致无法正常访问类


a.建立新模块-->需要改服务器URL:


b.pom.xml文件(关键所在):

注意1:由于使用的是阿里提供的数据库,因此需要把数据库连接池更改为Druid。

代码:注意别把位置放错了;

版本也必须写上如<version>1.2.8</version>

<!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
注意2:用阿里的URL服务时如果引入lombok一定要标注版本号,且必须是最新版,旧版可能出现不兼容的情况
<!--lombok,注意目录-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.32</version>  <!--要用最新的版本,低版本可能不兼容-->
        </dependency>

c.数据库连接信息:


d.UserMapper接口:

代码:

package com.itheima.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.itheima.pojo.User;

import java.util.List;


@Mapper
public interface UserMapper {

    //定义一个方法,用于查询全部用户信息
    @Select("select * from user")
    public List<User> list();


}

e.实体类User:

代码:

package com.itheima.pojo;

import lombok.*;

//引入lombok依赖后就可以用注解简化代码了
/*@Getter
@Setter
@ToString
@EqualsAndHashCode*/

@Data
@NoArgsConstructor //无参构造
@AllArgsConstructor //全参构造
public class User {
    //在实体类中,定义属性值的类型建议用包装类型
    private Integer id;
    /*id在数据库中为int型,java中可用包装类Integer*/
    private String name;
    private Short age;
    private Short gender;//性别
    /*数据库中gender为tinyint型,java中可用short型->包装类为Short*/
    private String phone;//手机号
    /*数据库中varchar型在java中用String型*/


    
}

f.测试类:

代码:

package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.ListIterator;

@SpringBootTest  //@SpringBootTest注解代表springboot整合单元测试的注解
class SpringbootMybatisQuickstartApplicationTests {

    /*
    @Test
    void contextLoads() {
    }*/

    @Autowired //@Autowired注解用来完成依赖注入
    private UserMapper userMapper;

    @Test
    public void testListUser() {
        //调用UserMapper的list方法来查询全部用户信息
        /* 但UserMapper为接口,不能实例化
           但有了@Mapper注解,运行时会自动生成该接口的实现类对象(代理对象),并且将该对象交给IOC容器管理
           已经成为IOC容器里的bean了
           之后要在单元测试中用到这个bean对象,可以通过依赖注入的形式,将这个bean对象注入进来
           因此,在测试方法上声明一个UserMapper类型的对象,
         */

        //调用list方法
        List<User> userList = userMapper.list();

        ListIterator<User> it = userList.listIterator();
        while (it.hasNext()) {
            User u = it.next();
            System.out.println(u);
        }
    }

    /* 注:由于该单元测试是SpringBoot整合的单元测试,上面有一个注解@SpringBootTest,一旦加了这个注解,
          其中的单元测试方法(@Test注解下)在运行时会自动加载整个SpringBoot的环境,并且创建SpringBoot的IOC容器,
          IOC容器创建好后,通过依赖注入(@Autowired注解下)的形式从IOC容器中获取UserMapper这个类型的bean对象,
          之后即可调用bean对象的list方法来查询全部用户信息
     */
}

g.user数据库:

源代码:

-- auto-generated definition
create table user
(
    id     int auto_increment comment 'id'
        primary key,
    name   varchar(10)      not null comment '姓名',
    age    tinyint unsigned not null comment '年龄',
    gender tinyint unsigned not null comment '性别:1.男 , 2.女',
    phone  varchar(11)      not null comment '电话号码',
    constraint id
        unique (id),
    constraint phone
        unique (phone)
)
    comment '用户';
​
​

h.运行结果:


i.包的结构:target文件下必须要出现实体类


三.总结:

1.当服务器URL使用https://start.spring.io(idea默认的)时,此时不需要再手动书写数据库连接池了

但也可以在pom.xml文件中进行更改数据库连接池。

2.当服务器URL使用https://start.aliyun.com时,此时需要把数据库连接池改为Druid数据库连接池,为了不出问题。

(比如防止引入lombok后出现找不到类的情况)

3.创建新模块时切记关注服务器URL。

  • 55
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值