mybatis基础入门测试时出现问题

Cause: java.lang.ClassNotFoundException:Cannot find class:com.mysql.jdbc.Driver

使用IDEA 的maven项目,实现mybatis的入门案例
mybatis的主配置文件SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的主配置文件-->
<configuration>
    <!--配置环境-->
    <environments default="development">
        <!--配置mysql的环境-->
        <environment id="development">
            <!--配置事务的类型-->
            <transactionManager type="JDBC"/>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置连接数据库的4个基本信息 -->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
        <mapper resource="mapper/UserDao.xml"/>
    </mappers>
</configuration>

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com itheima</groupId>
    <artifactId>day01_easy_01mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>

        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>

        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>

        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>



    </dependencies>


</project>

mapper配置文件 sql映射文件

<?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="userDao">
    <!-- 根据id获取用户信息 -->
    <select id="findUserById" parameterType="int"
            resultType="com.zxj.entity.User">
        select * from user where id = #{id}
    </select>
</mapper>

接口UserDao

public interface UserDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    User findUserById(int id);
}

接口实现类UserDaoImpl

public class UserDaoImpl implements UserDao {
    private SqlSessionFactory sqlSessionFactory;
    //注入SqlSessionFactory
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory = sqlSessionFactory;
    }
    @Override
    public User findUserById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = null;

        //通过sqlsession调用selectOne方法获取一条结果集 
        // 参数1:指定定义的statement的id,参数2:指定向statement中传递的参数
        user = sqlSession.selectOne("userDao.findUserById", id);
        return user;
    }

}

测试类

public class MybatisTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void init() throws IOException {
        SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        sqlSessionFactory=sessionFactoryBuilder.build(inputStream);
    }
    @Test
    public void testFindUserById(){
        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        User user = userDao.findUserById(1);
        System.out.println(user);
    }
}

测试后结果

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
Cause: java.lang.ClassNotFoundException:Cannot find class:com.mysql.jdbc.Driver

分析过程
1.首先思考,MySQL版本 与 驱动版本不符合
我的MySQL版本 为 8.0.19
而我导入的mysql-connector-java包为5.1.32包,于是将mysql-connector-java包改为8.0.16

<!-- mysql驱动坐标 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
            <scope>runtime</scope>
        </dependency>

继续执行,还是报相同的错,因为高版本MySQL,driver要修改为对应的依赖包,url后面要加时区,修改配置文件

 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8"/>

执行后显示结果

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
User{id=1, username='张三', birthday=Fri Feb 02 20:54:30 CST 1990, sex='男', address='北京'}

posted on 2020-11-22 22:21  小菜鸟攻城狮  阅读(182)  评论(0编辑  收藏  举报

导航