您的位置:首页 > 编程语言 > Java开发

SSM之整合mybatis与Junit测试中遇到的问题

2017-04-10 21:11 337 查看
接着上次的项目,今天写了项目中的mybatis,以及加上了Junit。mybatis不像hibernate那样可以从java对象转数据表,只能通过数据表反向生成java数据模型(或许能java对象转数据表只是我不知道(逃      )

遇到的问题:

1、数据表转java对象要用到mysql-connector,mybatis-generator-core-1.3.5.jar这两个包,以及配置文件generationConfig.xml,配置文件在下面给出

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--数据库驱动-->
<classPathEntry    location="mysql-connector-java-5.0.8-bin.jar"/>
<context id="DB2Tables"    targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="lcw.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
这里感谢这个博主的文章 http://www.cnblogs.com/smileberry/p/4145872.html

用mysql-connector的6.0.6版本进行生成的时候会出现如下错误

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class
is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SP
I and manual loading of the driver class is generally unnecessary.
Mon Apr 10 18:37:04 CST 2017 WARN: Establishing SSL connection without server's
identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ an
d 5.7.6+ requirements SSL connection must be established by default if explicit
option isn't set. For compliance with existing applications not using SSL the ve
rifyServerCertificate property is set to 'false'. You need either to explicitly
disable SSL by setting useSSL=false, or set useSSL=true and provide truststore f
or server certificate verification.
java.sql.SQLException: The server time zone value '?й???????' is unrecognized o
r represents more than one time zone. You must configure either the server or JD
BC driver (via the serverTimezone configuration property) to use a more specifc
time zone value if you want to utilize time zone support.
错误原因是在mysql MySQL 5.5.45+, 5.6.26+ and 5.7.6+的版本后如果不在连接字符串中指定不使用SSL进行连接的情况下是默认使用SSL进行连接的,所以在generationConfig.xml中的数据库连接字符串要指明不使用SSL连接,或者直接使用5.1.38版本的jar,干脆明了(偷偷懒,嘿嘿嘿),这个mybatis-generator-core-1.3.5.jar可以在中央仓库中找到,不用去百度上搜索在哪下载。

转换的时候用的语句

Java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>


2、在对spring进行junit测试中,要使用

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.R
9bf8
ELEASE</version>
</dependency>
并且Junit的版本要用4.12的版本,要不然会报错

3、spring tool suite中重命名包之后XML中相关引用貌似不会被重命名,这点应该特别注意,所以重命名后多多检查

4、在写jdbc资源文件的时候不要直接使用username,password这样的字段名,写的复杂点,比如jdbc.username,要不然会出现类似这种异常

Access denied for user 'Administrator'@'localhost' (using password: YES)

具体的看这篇博客,博主分析的很好,感谢博主
http://blog.csdn.net/welling319/article/details/51531927
5、在配置junit过程中出现了这个错误

Error creating bean with name 'transactionManager' :nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/
是缺少了spring-jdbc-4.3.7.RELEASE.jar

6、在写mybatis的数据模型mapper.xml的时候,如果写的sql语句是有查询结果的,不要忘记写resultType,要不然会报类似这样的异常

A query was run and no Result Maps were found for the Mapped Statement 'indi.group.his.dao.IUserDao.selectByUserName'.  It's likely that neither a Result Type nor a Result Map was specified.


最后在寻找答案的时候看到的一篇文章关于spring+maven+mybatis+mysql+junit整合的,感觉还行
http://blog.csdn.net/lxfHaHaHa/article/details/57078994
顺便也感谢下这个博主
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  junit mybatis spring mvc