1、使用spring-mybatis架构,使用的是sybase any where数据库。
驱动连接都是正常的,但是一执行select的SQL语句就会报错。使用insert语句就能正常执行。
我把SQL放到数据库直接去执行也没有问题。
2、错误代码
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: 未执行
### The error may exist in file [D:\workspace\spring-mybatis\build\classes\com\pis\entity\UserRoleMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from USER_ROLE WHERE ROLE_ID=1
### Cause: java.sql.SQLException: 未执行
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
at com.sun.proxy.$Proxy10.findOne(Unknown Source)
at com.pis.test.CusImgTest.test(CusImgTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLException: 未执行
at ianywhere.ml.jdbcodbc.jdbc3.IResultSetMetaData.getColumnClassName(IResultSetMetaData.java:222)
at org.apache.ibatis.executor.resultset.ResultSetWrapper.<init>(ResultSetWrapper.java:58)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getFirstResultSet(DefaultResultSetHandler.java:191)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:146)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:57)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
... 29 more
3、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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- Spring 在启动的时候扫描com.tarena
包中的所有子包,扫描注解,如果某个类
包含注解,就将类创建为Bean对象 -->
<util:properties id="jdbc"
location="classpath:com/pis/config/db.properties"/>
<bean id="bds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="#{jdbc.driver}"/>
<property name="url"
value="#{jdbc.url}"/>
<property name="username"
value="#{jdbc.username}"/>
<property name="password"
value="#{jdbc.password}"/>
</bean>
<!-- MyBatis - Spring -->
<!-- SqlSessionFactoryBean 创建的Bean
是sqlSessionFactory 的对象 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"
ref="bds" />
<property name="mapperLocations"
value="classpath:com/pis/entity/mapper/*.xml" />
</bean>
<!-- 配置MyBatis注解 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.pis" />
<property name="annotationClass" value="com.pis.annotation.MyBatisRepository" />
</bean>
</beans>
4、db.properties
driver=ianywhere.ml.jdbcodbc.jdbc3.IDriver
url=jdbc:ianywhere:driver=SQL Anywhere 11;ENG=pis;
username=offline_pis
password=dbwork
initsize=1
maxactive=1
maxwait=5000
maxidle=1
minidle=1
5、UserRoleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.pis.entity.UserRoleMapper">
<select id="findOne" resultType="com.pis.entity.UserRole">
select * from USER_ROLE WHERE ROLE_ID=1
</select>
</mapper>
6、UserRoleMapper.java
package com.pis.entity;
import com.pis.annotation.MyBatisRepository;
@MyBatisRepository
public interface UserRoleMapper {
public UserRole findOne(int roleId);
}
7、实体类UserRole
package com.pis.entity;
public class UserRole {
private int roleId;
private String roleName;
private String note;
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
Mybatis 的Mapper文件的映射,如果结果复杂对象需要指定属性resultMap
<mapper namespace="com.pis.entity.UserRoleMapper" >
<resultMap id="BaseResultMap" type="com.pis.entity.UserRole" >
<id column="id" property="roleId" jdbcType="INTEGER" />
<result column="rolename" property="roleName" jdbcType="VARCHAR" />
<result column="note" property="note" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
roleid, rolename, note
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from userrole
where roleid = #{roleid,jdbcType=INTEGER}
</select>
</mapper>
解释一下其中的几个属性值。<resultMap>
标签内存放的是结果集映射,如果是从javaweb基础学起的话,直接接触jdbc,应该对ResultSet这个比较了解,这里的resultMap
指定的就是一个数据库字段column 和 你自己建的 pojo/entity 对象的映射关系。其中标签内的id
是用来标识resultMap
的,就是代码里的BaseResultMap
,可以在下方select
标签selectByPrimaryKey
的resultMap
属性中指定,表示使用这个结果集去接收查询到的结果并映射为UserRole对象。resultMap
标签内的id
是主键对应的映射,result
是其他列的映射。column
属性是数据库里字段的名称,property
是UserRole
对象中对应的属性名称,jdbcType
表示的是数据库中该字段的存储类型。下边的没什么难度,有问题还可以再私信我。另外,能用 mybatis-generator
生成这些文件就最好不要在手动写这些基础性的代码,容易错误和遗漏。自行百度mybatis-generator吧