ssm框架搭建之验证Dao 所遇到的问题

一:jdbc.properties

(1)com.mysql.jdbc.driver 警告

com.mysql.jdbc.Driver是mysql-connector-java 5中的,而com.mysql.cj.jdbc.Driver是mysql-connector-java 6中的。


1、JDBC连接Mysql5需用com.mysql.jdbc.Driver,例如:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root

2、JDBC连接Mysql6需用com.mysql.cj.jdbc.Driver,同时需要指定时区serverTimezone,例如:
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=root

(2)mysql 时差问题

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

设置整个数据库的时区,可以执行下面的语句来完成:

show variables like '%time_zone%'
set global time_zone='+8:00';

(3)是否使用ssl连接

MySQL 5.5.45+, 5.6.26+ and 5.7.6+版本默认要求建立SSL连接。
为了符合当前不使用SSL连接的应用程序,verifyServerCertificate属性设置为’false’。
如果你不需要使用SSL连接,你需要通过设置useSSL=false来显式禁用SSL连接。
如果你需要用SSL连接,就要为服务器证书验证提供信任库,并设置useSSL=true。 

二:mybaties-config.xml 和xxxDao.xml的头

mybaties-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">

xxxDao.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">

三.spring-dao.

<?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">  
    
    <!-- 配置整合mybatis过程 -->  
    <!-- 1.配置数据库相关参数properties的属性:${url} -->  
    <context:property-placeholder location="classpath:jdbc.properties" />  
  
    <!-- 2.数据库连接池 -->  
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <!-- 配置连接池属性 -->  
        <property name="driverClass" value="${jdbc.driver}" />  
        <property name="jdbcUrl" value="${jdbc.url}" />  
        <property name="user" value="${jdbc.root}" />  
        <property name="password" value="${jdbc.password}" />  
  
        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30" />  
        <property name="minPoolSize" value="10" /> 
    
        <!-- 关闭连接后不自动commit -->  
        <property name="autoCommitOnClose" value="false" />  
        <!-- 当获取连接失败重试次数 -->  
        <property name="acquireRetryAttempts" value="2" /> 
    
        <!-- 获取连接超时时间  当连接池用完时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,
        如果设为0则无限等待,单位是毫秒,default:0 -->
        <property name="checkoutTimeout" value="10000"/> 
    </bean>  
  
    <!-- 3.配置SqlSessionFactory对象 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <!-- 注入数据库连接池 -->  
        <property name="dataSource" ref="dataSource" />  
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->  
        <property name="configLocation" value="classpath:mybatis-config.xml" />  
        <!-- 扫描entity包 使用别名 -->  
        <property name="typeAliasesPackage" value="com.xxx.entity" />  
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->  
        <property name="mapperLocations" value="classpath:mapper/*.xml" />  
    </bean>  
  
    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <!-- 注入sqlSessionFactory -->  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
        <!-- 给出需要扫描Dao接口包 -->  
        <property name="basePackage" value="com.xxx.dao" />  
    </bean>  
  
     
  
</beans>  

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值