用的德鲁伊连接池,先看applicationContext-dao.xml里的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:property-placeholder location="classpath:properties/*.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
<!--
<property name="maxWait" value="5000" /> -->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.mapper" />
</bean>
</beans>
这样的话,控制台输出:
2017-12-28 12:43:58,209DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:43:58,214DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:43:58,221DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:43:58,221DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -
2017-12-28 12:43:58,222DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:43:58,317DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:43:58,335DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:43:58,366DEBUG[org.springframework.jdbc.datasource.DataSourceUtils] -
2017-12-28 12:43:58,447INFO[com.alibaba.druid.pool.DruidDataSource] - <{dataSource-1} inited>
一直显示等待,后来加了个maxWait属性,时间是5秒,然后才开始报错,如下:
2017-12-28 12:47:41,261DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:47:41,264DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:47:41,269DEBUG[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
2017-12-28 12:47:41,269DEBUG[org.springframework.beans.factory.support.DefaultListableBeanFactory] -
2017-12-28 12:47:41,270DEBUG[org.springframework.web.servlet.DispatcherServlet] -
2017-12-28 12:47:41,338DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:41,348DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:41,378DEBUG[org.springframework.jdbc.datasource.DataSourceUtils] -
2017-12-28 12:47:41,472INFO[com.alibaba.druid.pool.DruidDataSource] - <{dataSource-1} inited>
2017-12-28 12:47:46,473WARN[com.alibaba.druid.pool.DruidDataSource] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.mybatis.spring.SqlSessionUtils] -
2017-12-28 12:47:51,476DEBUG[org.springframework.jdbc.datasource.DataSourceTransactionManager] -
2017-12-28 12:47:51,478DEBUG[org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] - <Resolving exception from handler [public com.demo.pojo.TbItem com.demo.controller.TestController.getItem(java.lang.Long)]: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.alibaba.druid.pool.GetConnectionTimeoutException: wait millis 5000, active 0
The error may exist in com/demo/mapper/TbItemMapper.xml
The error may involve com.demo.mapper.TbItemMapper.selectByPrimaryKey
The error occurred while executing a query
无法连接到数据库。。。。错误显示可能在/TbItemMapper.xml,这是mybatis逆向生成的,在其他项目中测试可用,所以问题应该不在这,其他配置文件啥的也感觉没问题,但就是连接不到数据库,问题出在哪???
TbItemMapper.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="com.demo.mapper.TbItemMapper">
<resultMap id="BaseResultMap" type="com.demo.pojo.TbItem">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="sell_point" jdbcType="VARCHAR" property="sellPoint" />
<result column="price" jdbcType="BIGINT" property="price" />
<result column="num" jdbcType="INTEGER" property="num" />
<result column="barcode" jdbcType="VARCHAR" property="barcode" />
<result column="image" jdbcType="VARCHAR" property="image" />
<result column="cid" jdbcType="BIGINT" property="cid" />
<result column="status" jdbcType="TINYINT" property="status" />
<result column="created" jdbcType="TIMESTAMP" property="created" />
<result column="updated" jdbcType="TIMESTAMP" property="updated" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
id, title, sell_point, price, num, barcode, image, cid, status, created, updated
</sql>
<select id="selectByExample" parameterType="com.demo.pojo.TbItemExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from tb_item
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tb_item
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from tb_item
where id = #{id,jdbcType=BIGINT}
</delete>
<delete id="deleteByExample" parameterType="com.demo.pojo.TbItemExample">
delete from tb_item
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.demo.pojo.TbItem">
insert into tb_item (id, title, sell_point,
price, num, barcode,
image, cid, status,
created, updated)
values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR},
#{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR},
#{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.demo.pojo.TbItem">
insert into tb_item
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="title != null">
title,
</if>
<if test="sellPoint != null">
sell_point,
</if>
<if test="price != null">
price,
</if>
<if test="num != null">
num,
</if>
<if test="barcode != null">
barcode,
</if>
<if test="image != null">
image,
</if>
<if test="cid != null">
cid,
</if>
<if test="status != null">
status,
</if>
<if test="created != null">
created,
</if>
<if test="updated != null">
updated,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="title != null">
#{title,jdbcType=VARCHAR},
</if>
<if test="sellPoint != null">
#{sellPoint,jdbcType=VARCHAR},
</if>
<if test="price != null">
#{price,jdbcType=BIGINT},
</if>
<if test="num != null">
#{num,jdbcType=INTEGER},
</if>
<if test="barcode != null">
#{barcode,jdbcType=VARCHAR},
</if>
<if test="image != null">
#{image,jdbcType=VARCHAR},
</if>
<if test="cid != null">
#{cid,jdbcType=BIGINT},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
<if test="created != null">
#{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null">
#{updated,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.demo.pojo.TbItemExample" resultType="java.lang.Long">
select count(*) from tb_item
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update tb_item
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=BIGINT},
</if>
<if test="record.title != null">
title = #{record.title,jdbcType=VARCHAR},
</if>
<if test="record.sellPoint != null">
sell_point = #{record.sellPoint,jdbcType=VARCHAR},
</if>
<if test="record.price != null">
price = #{record.price,jdbcType=BIGINT},
</if>
<if test="record.num != null">
num = #{record.num,jdbcType=INTEGER},
</if>
<if test="record.barcode != null">
barcode = #{record.barcode,jdbcType=VARCHAR},
</if>
<if test="record.image != null">
image = #{record.image,jdbcType=VARCHAR},
</if>
<if test="record.cid != null">
cid = #{record.cid,jdbcType=BIGINT},
</if>
<if test="record.status != null">
status = #{record.status,jdbcType=TINYINT},
</if>
<if test="record.created != null">
created = #{record.created,jdbcType=TIMESTAMP},
</if>
<if test="record.updated != null">
updated = #{record.updated,jdbcType=TIMESTAMP},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update tb_item
set id = #{record.id,jdbcType=BIGINT},
title = #{record.title,jdbcType=VARCHAR},
sell_point = #{record.sellPoint,jdbcType=VARCHAR},
price = #{record.price,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
barcode = #{record.barcode,jdbcType=VARCHAR},
image = #{record.image,jdbcType=VARCHAR},
cid = #{record.cid,jdbcType=BIGINT},
status = #{record.status,jdbcType=TINYINT},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.demo.pojo.TbItem">
update tb_item
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
<if test="sellPoint != null">
sell_point = #{sellPoint,jdbcType=VARCHAR},
</if>
<if test="price != null">
price = #{price,jdbcType=BIGINT},
</if>
<if test="num != null">
num = #{num,jdbcType=INTEGER},
</if>
<if test="barcode != null">
barcode = #{barcode,jdbcType=VARCHAR},
</if>
<if test="image != null">
image = #{image,jdbcType=VARCHAR},
</if>
<if test="cid != null">
cid = #{cid,jdbcType=BIGINT},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
<if test="created != null">
created = #{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null">
updated = #{updated,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.demo.pojo.TbItem">
update tb_item
set title = #{title,jdbcType=VARCHAR},
sell_point = #{sellPoint,jdbcType=VARCHAR},
price = #{price,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
barcode = #{barcode,jdbcType=VARCHAR},
image = #{image,jdbcType=VARCHAR},
cid = #{cid,jdbcType=BIGINT},
status = #{status,jdbcType=TINYINT},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
上面是逆向工程的mapper,后来我自己写了个TestMapper,还是出现同样的问题:
另外navicat访问数据库没问题。我想会不会是连接池配置的问题,也许那些配置可能导致这种情况??