c3p0配置后报错
来源:5-1 MyBatis整合C3P0连接池
小老哥丶
2020-07-28 17:42:24
mybatis-config.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"> <!--配置项,所有配置放在这里面--> <configuration> <settings> <!-- 如果实现类的属性名称和表的字段名称不一样就无法正确获取数据,就需要配置配置驼峰命名法--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> <property name="reasonable" value="true"/> </plugin> </plugins> <!--环境配置标签 default代表默认使用哪个标识下的配置信息 environment可以多个--> <environments default="dev"> <!-- id=唯一标识--> <environment id="dev"> <!-- 用什么方式管理数据库事务commit/rollback--> <transactionManager type="JDBC"></transactionManager> <!-- 数据源type表示以什么样的方式对数据源进行管理 POOLED基于连接池的形式进行管理--> <!-- <dataSource type="POOLED">--> <dataSource type="com.imooc.mybatis.datasource.C3P0DatasourceFactory"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- useUnicode=true代表使用Unicode编码解决中文乱码问题--> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&charactEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="q123"/> <!-- initialPoolSize表示连接池初始连接数量--> <property name="initialPoolSize" value="5"/> <!-- 最大连接池连接数量--> <property name="maxPoolSize" value="20"/> <!-- 最小连接池连接数量--> <property name="minPoolSize" value="5"/> </dataSource> </environment> </environments> <mappers> <mapper resource="goods.xml"/> <mapper resource="goodsDetail.xml"/> </mappers> </configuration>
C3P0DatasourceFactory:
package com.imooc.mybatis.datasource; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory; /** * C3P0与Mybatis兼容使用的数据源工厂类 */ public class C3P0DatasourceFactory extends UnpooledDataSourceFactory { public C3P0DatasourceFactory() { // ComboPooledDataSource()是由c3p0提供的符合连接池的数据源,this.dataSource由UnpooledDataSourceFactory提供 this.dataSource=new ComboPooledDataSource(); } }
目录:
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.imooc</groupId> <artifactId>MyBatis</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>aliyun</id> <name>aliyun></name> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>3.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java --> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.15</version> </dependency> </dependencies> </project>
错误信息:
java.lang.NoClassDefFoundError: Could not initialize class com.imooc.mybatis.utils.MyBatisUtils
at com.imooc.mybatis.MyBatisTestor.testSelectTgoods(MyBatisTestor.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
2回答
同学你好,同学的问题解决了吗。如果没有解决,建议同学看一下项目启动过程中控制台有没有报错,建议同学贴一下代码。
祝学习愉快~
小老哥丶
提问者
2020-07-28
是配置信息的问题,
相似问题