ssh整合的时候不报错,但插入的时候数据库也没有数据,控制台把insert语句已经打出来了,这是什么情况,请大侠们帮忙看看
applicationContext.xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
" default-autowire="byName" default-lazy-init="true">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" 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}"></property>
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/demo/mapping/demo.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dDao" class="com.demo.dao.Impl.demoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="dService" class="com.demo.service.Impl.demoServiceImpl">
<property name="dDao" ref="dDao"></property>
</bean>
<bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
struts.xml 如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="demo" namespace="/demo" extends="struts-default">
<action name="demo" class="com.demo.action.RegisterAction">
<result name="index">/demo/demo-index.jsp</result>
<result name="add">/demo/demo-add.jsp</result>
<result name="operate">/demo/demo-add.jsp</result>
<result name="save" type="redirectAction">demo!index.action</result>
<result name="update" type="redirectAction">demo!index.action</result>
<result name="delete" type="redirectAction">demo!index.action</result>
</action>
</package>
</struts>
jdbc.properties 如下:
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.6.90:1521:orcl
jdbc.username=example
jdbc.password=123456
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
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>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
dao 接口如下:
package com.demo.dao;
import java.io.Serializable;
import java.util.List;
import com.demo.model.Demo;
public interface demoDao {
public void save(Demo demo);
public List<Demo> getDemos(final String strHql,final Object...params);
public List<Demo> getDemos();
public void delete(Demo demo);
public abstract <T> T findByPrimaryKey(Class<T> class1, Serializable serializable);
public void update(Demo demo);
}
dao的实现:
package com.demo.dao.Impl;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import com.demo.dao.demoDao;
import com.demo.model.Demo;
public class demoDaoImpl extends HibernateDaoSupport implements demoDao{
@Transactional
public void save(Demo demo) {
this.getHibernateTemplate().save(demo);
}
@SuppressWarnings("unchecked")
public List<Demo> getDemos(final String strHql,final Object...params) {
List<Demo> demos=new ArrayList<Demo>();
demos=this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,SQLException {
Query query=session.createQuery(strHql);
for(int i=0;params!=null&&i<params.length;i++){
query.setString(i,params[i].toString());
}
return query.list();
}
});
return demos;
}
public void delete(Demo demo) {
this.getHibernateTemplate().delete(demo);
}
@SuppressWarnings("unchecked")
public <T> T findByPrimaryKey(Class<T> clazz, Serializable key) {
T obj = (T) getSession().get(clazz, key);
return obj;
}
public void update(Demo demo) {
this.getHibernateTemplate().update(demo);
}
@SuppressWarnings("unchecked")
public List<Demo> getDemos() {
return this.getHibernateTemplate().find("from Demo where 1=1");
}
}
service接口:
package com.demo.service;
import java.io.Serializable;
import java.util.List;
import com.demo.model.Demo;
public interface demoService {
public void save(Demo demo);
public List<Demo> getDemos(final String strHql,final Object...params);
public List<Demo> getDemos();
public void delete(Demo demo);
public <T> T findByPrimaryKey(Class<T> class1, Serializable serializable);
public String createHql(Demo demo);
public void update(Demo demo);
}
service 的实现:
package com.demo.service.Impl;
import java.io.Serializable;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.demo.dao.demoDao;
import com.demo.model.Demo;
import com.demo.service.demoService;
public class demoServiceImpl implements demoService{
private demoDao dDao;
public demoDao getdDao() {
return dDao;
}
public void setdDao(demoDao dDao) {
this.dDao = dDao;
}
public void save(Demo demo) {
dDao.save(demo);
}
public List<Demo> getDemos(final String strHql,final Object...params) {
return dDao.getDemos(strHql, params);
}
public void delete(Demo demo) {
dDao.delete(demo);
}
@Override
public String createHql(Demo demo) {
StringBuffer buf=new StringBuffer();
buf.append("from Demo d where 1=1");
if(demo!=null){
buf.append("and d.name like '%"+demo.getName()+"%'");
}
return buf.toString();
}
@Override
public <T> T findByPrimaryKey(Class<T> class1, Serializable serializable) {
return dDao.findByPrimaryKey(class1, serializable);
}
@Override
public void update(Demo demo) {
dDao.update(demo);
}
@Override
public List<Demo> getDemos() {
return dDao.getDemos();
}
}
action 如下:
package com.demo.action;
import java.util.List;
import com.demo.model.Demo;
import com.demo.service.demoService;
public class RegisterAction {
private Demo demo;
private demoService dService;
private List<Demo> demos;
public Demo getDemo() {
return demo;
}
public void setDemo(Demo demo) {
this.demo = demo;
}
public demoService getdService() {
return dService;
}
public void setdService(demoService dService) {
this.dService = dService;
}
public List<Demo> getDemos() {
return demos;
}
public void setDemos(List<Demo> demos) {
this.demos = demos;
}
public String execute(){
return "index";
}
public String index(){
//demos=dService.getDemos(dService.createHql(demo),null);
demos=dService.getDemos();
return "index";
}
public String add(){
return "add";
}
public String operate(){
demo=dService.findByPrimaryKey(Demo.class,demo.getId());
return "operate";
}
public String save(){
dService.save(demo);
return "save";
}
public String delete(){
demo=dService.findByPrimaryKey(Demo.class,demo.getId());
dService.delete(demo);
return "delete";
}
public String update(){
demo=dService.findByPrimaryKey(Demo.class,demo.getId());
demo.setName(demo.getName());
demo.setAge(demo.getAge());
demo.setAddress(demo.getAddress());
dService.update(demo);
return "update";
}
}
实体:
package com.demo.model;
public class Demo {
private Long id;
private String name;
private Integer age;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
映射文件:demo.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping package="com.demo.model">
<class name="Demo" table="tb_demo" catalog="example">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="sequence">
<param name="sequence">seq_demo</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="tb_name" length="50" not-null="true" unique="true" />
</property>
<property name="age" type="java.lang.Integer">
<column name="tb_age" />
</property>
<property name="address" type="java.lang.String">
<column name="tb_address" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
页面:
demo-add.jsp 如下 :
<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>ssh整合测试</title>
<script type="text/javascript" src="/SSH1/js/jquery.js"></script>
</head>
<body>
<form method="post" action="demo/demo.action">
<input type="hidden" id="num" name="demo.id" value="${demo.id}"/>
<table>
<tr>ssh整合数据测试</tr>
<tr><td align="right">姓名:</td><td align="left"><input type="text" name="demo.name" value="${demo.name}"/></td></tr>
<tr><td align="right">年龄:</td><td align="left"><input type="text" name="demo.age" value="${demo.age}"/></td></tr>
<tr><td align="right">家庭住址:</td><td align="left"><input type="text" name="demo.address" value="${demo.address}"/></td></tr>
<tr><td colspan="2"><input type="button" value="保存" id="save"/><input type="reset" value="重置"/></td></tr>
</table>
</form>
</body>
</html>
<script>
$("document").ready(function (){
$("#save").click(function(){
if($("#num").val()==null||$("#num").val()==""){
document.forms[0].action="/SSH1/demo/demo!save.action";
document.forms[0].submit();
}else{
alert("eee");
document.forms[0].action="/SSH1/demo/demo!update.action";
document.forms[0].submit();
}
});
});
</script>
demo-index.jsp 如下:
<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>ssh整合测试</title>
</head>
<body>
<form action="demo/demo.action">
<table>
<tr><td><a href="<%=request.getContextPath() %>/demo/demo!add.action">添加</a></td></tr>
<tr><td>序号</td><td>姓名</td><td>年龄</td><td>地址</td><td>操作</td></tr>
<s:if test="demos.size()>0">
<s:iterator value="demos" status="st">
<tr>
<td>${st.index+1}</td>
<td>${name}</td>
<td>${age}</td>
<td>${address}</td>
<td><a href="<%=request.getContextPath() %>/demo/demo!operate.action?demo.id=${id}">修改</a> <a href="<%=request.getContextPath() %>/demo/demo!operate.action?demo.id=${id}">删除</a></td>
</tr>
</s:iterator>
</s:if>
</table>
</form>
</body>
</html>