springMVC整合Ibatis出错。。

Gemerl 2014-09-23 03:24:37
结构图
springMVc
src
com.spring.entity
User.java
User.xml
com.spring.dao
com.spring.service
com.spring.control
resource
jdbc.properties
sqlMapConfig.xml
Webroot
web-info
appliaction.xml
springMVC-servlet.xml
web.xml
错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in com/spring/entity/User.xml.
--- The error occurred while executing query.
--- Check the select * from t_user .
--- Check the SQL Statement (preparation failed).
--- Cause: java.sql.SQLException: Connections could not be acquired from the underlying database!; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/spring/entity/User.xml.
--- The error occurred while executing query.
--- Check the select * from t_user .
--- Check the SQL Statement (preparation failed).
--- Cause: java.sql.SQLException: Connections could not be acquired from the underlying database!

org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in com/spring/entity/User.xml.
--- The error occurred while executing query.
--- Check the select * from t_user .
--- Check the SQL Statement (preparation failed).
--- Cause: java.sql.SQLException: Connections could not be acquired from the underlying database!; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/spring/entity/User.xml.
--- The error occurred while executing query.
--- Check the select * from t_user .
--- Check the SQL Statement (preparation failed).

java.sql.SQLException: Connections could not be acquired from the underlying database!

com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


appliaction.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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-3.1.xsd" default-autowire="byName">

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<context:property-placeholder location="classpath*:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClassName}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="${initialSize}" />
<property name="maxStatements" value="${maxActive}" />
</bean>
<!-- <value>classpath:sqlMapConfig.xml</value> -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

springMVC-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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-3.1.xsd">

<context:component-scan base-package="com.spring"></context:component-scan><!-- 扫描包 -->
<mvc:annotation-driven></mvc:annotation-driven><!-- 开启注解自动注入 -->
<mvc:resources mapping="/img/**" location="/img/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>


sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings lazyLoadingEnabled="true" useStatementNamespaces="true" />

<!-- 使用spring之后,数据源的配置移植到了spring上,所以iBATIS本身的配置可以取消 -->
<sqlMap resource="com/spring/entity/User.xml"/>

</sqlMapConfig>


jdbc.properties

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=Gemerl
password=ky123456
initialSize=1
maxActive=4


User.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="User">
<resultMap id="result" class="com.spring.entity.User">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
</resultMap>

<!-- Use type aliases to avoid typing the full classname every time. -->

<typeAlias alias="User" type="com.spring.entity.User" />

<!--
Select with no parameters using the result map for Account class.
-->

<select id="allUser" resultClass="com.spring.entity.User">
select * from t_user
</select>

</sqlMap>


control

@RequestMapping("/user")
@Controller(value="userController")
public class UserControl {
//private static Map<String,User> users = new HashMap<String,User>();
private UserService userService;


public UserService getUserService() {
return userService;
}
@Resource
public void setUserService(UserService userService) {
this.userService = userService;
}

@RequestMapping(value="/users",method=RequestMethod.GET)
public String get(Model model,HttpServletRequest request){
List<User> uu=userService.getAll();
//model.addAttribute("users", users);
model.addAttribute("users", uu);
return "/index";
}


service

@Service(value="userService")
public class UserServiceImpl implements UserService {
private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public List<User> getAll() {
return userDao.getAll();
}

}


dao

@Repository(value="userDao")
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {
@Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;

@PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}

@SuppressWarnings("unchecked")
public List<User> getAll() {
List<User> users=this.getSqlMapClientTemplate().queryForList("User.allUser");
if(users!=null){
System.out.println("Dao method getAll()----user.size():"+users.size());
}
return users;
}

}
...全文
217 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kiss2006youu 2015-05-22
  • 打赏
  • 举报
回复
能看下你的jar包吗?
Defonds 2014-09-24
  • 打赏
  • 举报
回复
User.xml 里
    <select id="allUser" resultClass="com.spring.entity.User">
        select * from t_user
     </select>
返回结果不是一个对象而是一个集合。改成这个试试看:
  <select id="allUser" resultClass="List">
        select * from t_user
     </select>
Gemerl 2014-09-24
  • 打赏
  • 举报
回复
引用 1 楼 defonds 的回复:
User.xml 里
    <select id="allUser" resultClass="com.spring.entity.User">
        select * from t_user
     </select>
返回结果不是一个对象而是一个集合。改成这个试试看:
  <select id="allUser" resultClass="List">
        select * from t_user
     </select>
已经搞定。。 数据源的问题。。。
qq840727854 2014-09-24
  • 打赏
  • 举报
回复
先试着用连接下数据库 看通不通

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧