首先,项目可以运行且不报错。页面运行也很正常添加账户和登录都可以实现。问题就出在数据库里没有添加成功的那个数据。
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">
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- -->
<!-- spring 配置 -->
<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>
<!-- -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" extends="struts-default">
<action name="SaveUser" class="com.lxh.action.Login_action" method="SaveUser">
<result name="success">/index.jsp</result>
</action>
<action name="Tset" class="com.lxh.action.Login_action" method="Test">
<result name="success">/1.jsp</result>
<result name="lose">/login.jsp</result>
</action>
</package>
</struts>这是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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 声明sessionfactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 事务处理 -->
<bean id="transaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置持久化层 -->
<bean id="logindao" class="com.lxh.dao.Login_dao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置业务层 -->
<bean id="login_service" class="com.lxh.service.Login_service">
<property name="logindao" ref="logindao"></property>
</bean>
<!-- 配置控制层 -->
<bean id="login_action" class="com.lxh.action.Login_action">
<property name="login_service" ref="login_service"></property>
</bean>
</beans>这是hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/lianxi
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">lianxi</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/lxh/po/Login.hbm.xml" /> </session-factory></hibernate-configuration>
主要问题是在页面可以注册一个号,也能通过这号进入页面,但就是数据库中没这个数据.
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">
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- -->
<!-- spring 配置 -->
<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>
<!-- -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" extends="struts-default">
<action name="SaveUser" class="com.lxh.action.Login_action" method="SaveUser">
<result name="success">/index.jsp</result>
</action>
<action name="Tset" class="com.lxh.action.Login_action" method="Test">
<result name="success">/1.jsp</result>
<result name="lose">/login.jsp</result>
</action>
</package>
</struts>这是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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 声明sessionfactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 事务处理 -->
<bean id="transaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置持久化层 -->
<bean id="logindao" class="com.lxh.dao.Login_dao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置业务层 -->
<bean id="login_service" class="com.lxh.service.Login_service">
<property name="logindao" ref="logindao"></property>
</bean>
<!-- 配置控制层 -->
<bean id="login_action" class="com.lxh.action.Login_action">
<property name="login_service" ref="login_service"></property>
</bean>
</beans>这是hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/lianxi
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">lianxi</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/lxh/po/Login.hbm.xml" /> </session-factory></hibernate-configuration>
主要问题是在页面可以注册一个号,也能通过这号进入页面,但就是数据库中没这个数据.
你的情况是:可以插入数据库(如果数据没有commit,你插入的数据并没有真正的存入数据库,而是只是存在了数据库所占据的内存中),读取数据库正确(DBMS首先在内存中读取,找不到,再去DB中查找),数据回滚了,没有真正存进数据库
如果使用的是mysql InnoDB数据库引擎(这个支持数据回滚,MyISAM 不支持事务回滚)可以尝试设置一下:set autocommit=0;或者google下 有关 mysql hibernate 事务提交的 相关内容小弟个人观点,还望大家指点一二
-----不会吧 数据库没有数据 你怎么能登录的 难道不是从数据库中查到才能登录么?
建议你看下 你操作SQL的地方是不是有问题,还是没有commit ?
Configuration conf = new Configuration();
conf.configure();
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(xxxxxxxxxx); tx.commit();
session.close();
sessionFactory.close();
都ssh整合了,貌似不能用这个了,事务的提交都交个sping了,
不知道楼主是不是想这个大哥说的那样,通过session这种方式提交的?如果是应该会出现你那种情况,你没有将数据插入,原因在你的事务不再是hibernate来管理事务了,而是sping来管理了!
正常的ssh整合都是将事务的提交交个sping去管理,要用sping管理的话,你的操作类应该继承HibernateDaoSupport这个类
例如:
package dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserImpl extends HibernateDaoSupport {
//查询
public List selectALl() {
return getHibernateTemplate().find("from Users");
}
//增加(save,add)
public void addUser(User us){
getHibernateTemplate().save(us);
}
}同时记住你的sping的配置文件要为addUser这个方法进行事务的配置你可以写一个applicationContext-tran.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean><tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
//这句表示以add开头的方法的事务提交都由sping来处理
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice><aop:config>
//此处的第一×表示所有的返回类型 service.impl.*表示的是哪些类的事务交个sping去管理,service.impl.*.*的第二个×表示的是这个类下的所以方法都交个sping管理,(..)表示的是方法里所以的参数
<aop:pointcut expression="execution(* service.impl.*.*(..))" id="point"/>
<aop:advisor advice-ref="advice" pointcut-ref="point"/>
</aop:config>
</beans>
同时别忘了如果你有多个application这样sping的配置文件,记得在web。xml中记得为它配置读取!
可能你现在不会像我这样做用,但是我相信你以后会这样做!不同的代码放不同的文件,更让人容易看懂!
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
applicationContext.xml还要配制 ActionBean ... <action name="SaveUser" class=" actionBean " method="SaveUser">