The first time I used mybatis to report an error, Error querying database, I tried it for a while. The main reason was to separate com.mysql.jdbc.Driver.

1. Problem

Error querying database. Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource…Insert image description here


All red when the test is running! ! !

2. Investigation ideas

			1. 配置编写错误
			2. 配置文件引用错误或依赖坐标引入及版本问题

3. Report an error

3.1 Driver?

It was said that there was a problem with my driver. After checking the data, it was said that it might be an error in the SQL version. I checked the actual situation and checked it out.
Insert image description here

It’s really hard to find a needle in a haystack. Driver is written as drive, which is embarrassing.
Insert image description here

3.2 SQL query ID unique identification?

There is an error, findById is wrongly written as findId! ! ! Ah it's trueInsert image description here

Insert image description here

Insert image description here

3.3 Success

I thought it was really a small mistake and I thought I could really run away. . . . . Or did I think...Alt
Insert image description here

4. The biggest problem!

To separate com.mysql.jdbc.Driver
Insert image description here

4.1 mybatis-config.xml configuration items

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Cofig 3.0//EN"
        "//http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
        <!--引入外部的db.properties数据库配置文件-->
        <properties resource="db.properties"></properties>
        <!--环境配置-->
        <!--加载类路径下的属性文件-->
        <!--environments是用来配置mybatis环境的-->
        <environments default="development">
            <environment id="development" >
                <transactionManager type="JDBC"/>
                <!--数据库连接(数据库连接池)相关配置,db.properties 文件中的内容-->
                <dataSource type="POOLED">
                        <!-- <property name="driver" value="com.mysql.cj.jdbc.Driver"/> 出现报错的写法-->
                        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                        <property name="url" value="${mysql.url}"/>
                        <property name="username" value="${mysql.username}"/>
                        <property name="password" value="${mysql.password}"/>
                </dataSource>
            </environment>
        </environments>

       <!-- 配置xxxMapper文件的路径配置,该文件主要存放SQL语句-->
       <mappers>
           <mapper resource="mapper/UserMapper.xml"/>
       </mappers>
</configuration>

5. Successful takeoff

Insert image description here

public class UserTest {
    
    
    @Test
    public void userFindByIdTest() {
    
    
        //1. 获取核心配置文件mybatis-config.xml
        String resources = "mybatis-config.xml";

        //2. 创建流
        Reader reader = null;
        try {
    
    
            //读取mybatis-config.xml配置文件内容到reader对象中
            reader = Resources.getResourceAsReader(resources);
        }catch (IOException e){
    
    
            e.printStackTrace();
            System.out.println("输入流段错误!");
        }
        //3. 初始化mybatis数据库,创建 SqlSessionFactory
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        //4. 创建SqlSession 实例
        SqlSession sqlSession = sqlMapper.openSession();
        //5. 传入参数查询,返回结果
        User user = sqlSession.selectOne("findById",1);
        //6. 输出结果
        System.out.println(user.getUname());
        //7. 关闭流sqlSession
        sqlSession.close();
    }
    }

6. Share some configurations for using mybatis for the first time.

** The following is in random order, you will know whoever configures it first! **

6.1 pom.xml

<!--引入相关的坐标-->
    <dependencies>
     <!-- mybatis的jar包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>MyBatis</artifactId>
            <version>3.5.11</version>
        </dependency>
        <!--mysql数据库jar坐标配置-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-jdbc-java</artifactId>
            <version>8.0.28</version>
            <scope>runtime</scope>
        </dependency>

        <!-- junit 单元测试jar包坐标依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

6.2 Database db.properties configuration items

Note, db.properties, db is the file name, you can write it casually, but the suffix must be xxx.properties! properties!properties!


mysql.driver = com.mysql.cj.jdbc.Driver;
mysql.url = jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&Unicode=true&useSSL=false
mysql.username=root
mysql.password=root


6.3 mapper configuration items

xxxMapper is mainly used to store the mapping between SQL statements and java objects. It is best to create a mapper folder specifically to store xxxMapper files.

<?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: 名称空间, 由于映射文件可能有多个,
                为了防止 crud 语句的唯一标识被重复,需要设置空间名称

    mapper 为映射的根节点,namespace指定Dao接口的完整类名,mybatis会依据这个接口,动态创建一个实现类
    去实现这个接口,二这个实现类是一个Mapper 对象
-->

<mapper namespace="com.javaEElcb.pojo.User">
<!--
    select: 查询的statement (声明),用来编写查询语句
    id: 语句的唯一标识
    resultType: 配置返回结果的类型
    parameterType: 传递的参数类型,可以省略
    - - - - - - - - - - - - - - - - - - - - - - - -
    id = "接口中的方法名" parameterType= "传入的参数类型"
    resultType = "返回实体类对象,使用  包.类名"
-->
    <select id="findById"  parameterType = "int"
            resultType = "com.javaEElcb.pojo.User">
        select * from users where uid=#{id}
    </select>
</mapper>

6.4 mybatis-config.xml configuration

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Cofig 3.0//EN"
        "//http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
        <!--引入外部的db.properties数据库配置文件-->
        <properties resource="db.properties"></properties>
        <!--环境配置-->
        <environments default="development">
            <environment id="development" >
                <transactionManager type="JDBC"/>
                <!--数据库连接相关配置,db.properties 文件中的内容-->
                <dataSource type="POOLED">
                        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                        <property name="url" value="${mysql.url}"/>
                        <property name="username" value="${mysql.username}"/>
                        <property name="password" value="${mysql.password}"/>
                </dataSource>
            </environment>
        </environments>

       <!-- 配置xxxMapper文件的路径配置,该文件主要存放SQL语句-->
       <mappers>
           <mapper resource="mapper/UserMapper.xml"/>
       </mappers>
</configuration>

Insert image description here

attached

		有一段时间了,回看了一下代码.........数据库连接中的db配置文件多了一个不该出现的**<font color="red">分号</font>**!!! 删了就可以在mybatis-config中使用<font color="red">${mysql.driver}</font>了。。
/*mysql.driver = com.mysql.cj.jdbc.Driver;*/
//mysql.driver = com.mysql.cj.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&Unicode=true&useSSL=false
mysql.username=root
mysql.password=root

Guess you like

Origin blog.csdn.net/qq_24484317/article/details/127263120