相关配置和代码都检查过了,还是没查出来问题在哪里
AdService接口
public interface AdService {
/**
* 新增广告
*
* @param adDto
* @return 是否新增成功:true-成功;fale-失败
*/
boolean add(AdDto adDto);
}
AdServiceImpl类
package org.imooc.service.impl;
import org.imooc.bean.Ad;
import org.imooc.dao.AdDao;
import org.imooc.dto.AdDto;
import org.imooc.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
@Service
public class AdServiceImpl implements AdService {
@Autowired
private AdDao adDao;
@Value("${adImage.SavePath}")
private String adImageSavePath;
@Override
//TODO 可以改成获取失败详细原因
public boolean add(AdDto adDto) {
Ad ad = new Ad();
ad.setTitle(adDto.getTitle());
ad.setLink(adDto.getLink());
ad.setWeight(adDto.getWeight());
if (adDto.getImgFile() != null && adDto.getImgFile().getSize() > 0) {
String fileName = System.currentTimeMillis() + "_" + adDto.getImgFile().getOriginalFilename();
File file = new File(adImageSavePath);
File fileFolder = new File(adImageSavePath);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
try {
adDto.getImgFile().transferTo(file);
ad.setImgFileName(fileName);
adDao.insert(ad);
return true;
} catch (IOException e) {
//TODO 需要添加日志
return false;
}
} else {
return false;
}
}
}
AdDao.xml
<?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="org.imooc.dao.AdDao">
<insert id="insert">
insert into ad(title,img_file_name,link,weight)
values(#{title},#{imgFileName},#{link},#{weight})
</insert>
</mapper>
dao.properties
dataSource.url=jdbc:mysql://127.0.0.1:3306/comment?useUnicode=true&characterEncoding=utf8
dataSource.username=root
dataSource.password=123456
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:property-placeholder location="classpath:properties/*.properties"/>
<import resource="applicationContext-*.xml"/>
</beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="${dataSource.url}"></property>
<property name="user" value="${dataSource.username}"></property>
<property name="password" value="${dataSource.password}"></property>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
<!-- 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="5" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="10" />
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="5" />
<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="100" />
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="3" />
<!-- 定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个显著提高测试速度。注意:
测试的表必须在初始数据源的时候就存在。Default: null -->
<property name="preferredTestQuery" value="select 1" />
<!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30-->
<property name="acquireRetryAttempts" value="3" />
<!-- 两次连接中间隔时间,单位毫秒。Default: 1000 -->
<property name="acquireRetryDelay" value="1000" />
<!-- 当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
<property name="checkoutTimeout" value="30000" />
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!--<!– 扫描mybatis核心配置文件 –>-->
<!--<property name="configLocation" value="classpath:spring/mybatis.xml"/>-->
<!-- 扫描java bean,自动使用别名 -->
<property name="typeAliasesPackage" value="org.imooc.bean"/>
<!-- 扫描mybatis的SQL配置文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 扫描Dao接口包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="org.imooc.dao"/>
</bean>
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--<context:property-placeholder location="classpath:properties/*.properties"/>-->
<!-- 扫描service包 -->
<context:component-scan base-package="org.imooc.service"/>
<!--<!– 配置事务管理器 –>-->
<!--<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> -->
<!--<property name="dataSource" ref="dataSource"/>-->
<!--</bean>-->
<!--<!– 事务采用全注解方式 –>-->
<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
applicationContext-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解映射的支持 -->
<mvc:annotation-driven/>
<!-- 允许对静态资源文件的访问 -->
<mvc:default-servlet-handler/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp"/>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="20000000"/>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="org.imooc.controller"/>
</beans>
报错信息
一月 04, 2019 11:54:44 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 3, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 30000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgeby9a055ltq019wjxa3|548ab3a7, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgeby9a055ltq019wjxa3|548ab3a7, idleConnectionTestPeriod -> 60, initialPoolSize -> 5, jdbcUrl -> ${dataSource.url}, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 60, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 100, maxStatementsPerConnection -> 3, minPoolSize -> 5, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> select 1, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
一月 04, 2019 11:54:46 上午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@77832a00 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (3). Last acquisition attempt exception:
java.sql.SQLException: No suitable driver
【毕设】SSM全面梳理,前后端分离,zTree 和复杂SQL打造权限系统,解惑MyBatis和RESTful
了解课程