Mybatis + Access 开发过程注意和配置示例
最近写个桌面程序,用Access做数据库,主要持久化点数据。
用Mybatis一开始报一些错,经过实践,做一点总结:
******** 测试过程,被以下错误折腾了2个钟头! 神奇的是,尝试将 log4j.jar 移除,一切正常!!奔溃!!这是Mybatis的哪门子Bug啊 ****
** 具体环境,Jar包版本 mybatis-3.0.2.jar , log4j-1.2.15.jar , 这是个非常难察觉的原因,因为在开发时做了2个工程,一个工程好好的,一个工程总报错,于是尝试看2个工程差异,最终出,是因为工程加入 log4j 就一定错! **
** 查了下异常问题可能出在这里 :org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:31) **
** 暂时未知解决方法和根本原因,研究中 **
** 最终结果 :
经过一个通宵的努力,最终到解决方法( 2011-01-13 04:30 ):
1, 使用 log4j-1.2.15.jar 这个版本 (测试用低版本如 log4j-1.2.13.jar 必定出错)
2, 在 log4j 加上一行配置 :
log4j.logger.java.sql=OFF
终极解决方案!(注意,从mybatis官方下载的mybatis-3.0.4-bundle.zip 里面的 log4j-1.2.15.jar 不能用!才能解决这个问题!)
-------------------------
出错的信息
log4j2 appender-----------------------
log4j:WARN No appenders could be found for logger (org.apache.ibatis.datasource.pooled.PooledDataSource).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.ptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: No data found
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: java.sql.SQLException: No data found
at org.ptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:61)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:53)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:49)
dao.SessionFactory.main(SessionFactory.java:43)
Caused by: java.sql.SQLException: No data found
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.DataString(Unknown Source)
at sun.jdbc.String(Unknown Source)
at sun.jdbc.String(Unknown Source)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at flect.Method.invoke(Unknown Source)
at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:31)
at $String(Unknown Source)
at org.a
NullableResult(StringTypeHandler.java:17)
at org.Result(BaseTypeHandler.java:29)
at org.PropertyMappingValue(FastResultSetHandler.java:238)
at org.sultset.FastResultSetHandler.applyPropertyMappings(FastResultSetHandler.java:221)
at org.RowValue(FastResultSetHandler.java:196)
at org.sultset.FastResultSetHandler.handleRowValues(FastResultSetHandler.java:145)
at org.sultset.FastResultSetHandler.handleResultSet(FastResultSetHandler.java:121)
at org.sultset.FastResultSetHandler.handleResultSets(FastResultSetHandler.java:98)
at org.utor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:40)
at org.utor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:55)
at org.utor.SimpleExecutor.doQuery(SimpleExecutor.java:41)
at org.utor.BaseExecutor.query(BaseExecutor.java:95)
at org.utor.CachingExecutor.query(CachingExecutor.java:72)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:59)
... 3 more
-----------------------------------------------------------
以下为配置文件
---------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!--
<property name="driver" value="sql.jdbc.Driver" />
-->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="poolMaximumActiveConnections" value="10"/>
<property name="poolMaximumCheckoutTime" value="500"/>
<property name="poolTimeToWait" value="500"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 站点配置 -->
<mapper resource="com/l" />
</mappers>
</configuration>
------------------------------------------------------------------------------------
jdbc.properties
-
-----------------------------------------------------------------------------------
driver=sun.jdbc.odbc.JdbcOdbcDriver
url=jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=config.mdb
username=
password=
--------------------------------------------------------------------------------------
不好,会出错误,估计是Access本身的限制导致的
----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Bean">
<resultMap type="Bean" id="BeanMapper">
<result column="a" property="a" />
<result column="b" property="b" />
<result column="c" property="c" />
</resultMap>
<!--
查询列表,
注意:如果用 "SELECT * ", 则上面的 resultMap 里面必须把字段写全,否则报错
而且一定要关联resultMap,不能直接写成 resultType="ity.DedeSite",否则报错
SELECT a,b,c from DedeSite t
-->
<select id="getBeanList" resultMap="BeanMapper">
SELECT * from Table1
</select>
</mapper>
--------------------------------------------------------------------------------------------------------
SessionFactory.java
--------------------------------------------------------------------------------------------------------
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
tity.DedeSite;
public class SessionFactory {
private static SqlSessionFactory sqlMapper;
// private static SqlSession sqlSession;
static {
try {
String resource = "l";
Reader reader = ResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
SqlSession sqlSession = sqlMapper.openSession();
return sqlSession;
}
public static SqlSession getSqlSession(boolean flag) {
SqlSession sqlSession = sqlMapper.openSession(flag);
return sqlSession;
}
}
---------------------------
log4j.properties
----------------------------
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#log4j.appender.Threshold=DEBUG
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %d - %m%n
## for mybatis not to exception!!! 2011-01-13 , find by Tom!!!
## must add this line to stop the exception!!!
log4j.logger.java.sql=OFF
## config for httpclient
apache.http = ERROR
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论