struts2_hibernate3.3小例子 不报错 但数据库没反应 小项目在我的资源里面
本帖最后由 yjb8646226 于 2012-11-27 18:46:11 编辑 数据库 sqlserver 数据库名hibernate 建一个person表 5个字段 id username password age registerDate
熟悉struts2 hibernate的大神帮忙分析下
项目在我的资源里面可以下载
[最优解释]
提交代码也OK的!!!
不报错、数据库没反应就是没有添加到数据!!!
你看看有没有打印sql呢?
try {
session.save(person);
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
}finally{
HibernateUtil.close(session);
}
this.registerDate = registerDate;
}
}
PersonAction
package com.yjb.action;
import com.opensymphony.xwork2.ActionSupport;
import com.yjb.model.Person;
import com.yjb.service.PersonService;
import com.yjb.service.impl.PersonServiceImpl;
public class PersonAction extends ActionSupport{
/**
*
*/
private String username;
private String password;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String excute ()throws Exception{
Person person =new Person();
person.setUsername(username);
person.setPassword(password);
person.setAge(age);
java.sql.Date registerDate=new java.sql.Date(new java.util.Date().getTime());
person.setRegisterDate(registerDate);
//把页面的值放到person里面
PersonService personService=new PersonServiceImpl();
personService.savePerson(person);
//通过PersonService借口实例化一个对象把person对象的值存入数据库
return SUCCESS;
}
}
HibernateUtil
package com.yjb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Session openSession(){
Session session=sessionFactory.openSession();
return session;
}
public static void close(Session session){
if (null!=session) {
session.close();
}
}
}
PersonDao
package com.yjb.dao;
import com.yjb.model.Person;
public interface PersonDao {
public void savePerson(Person person);
}
PersonDaoImpl
package com.yjb.dao.impl;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.yjb.dao.PersonDao;
import com.yjb.model.Person;
import com.yjb.util.HibernateUtil;
public class PersonDaoImpl implements PersonDao {
@Override
public void savePerson(Person person) {
// TODO Auto-generated method stub
Session session = HibernateUtil.openSession();
Transaction tx=session.beginTransaction();
try {
session.save(person);
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
}finally{
HibernateUtil.close(session);
}
}
}
Person.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">
<hibernate-mapping>
<class name="com.yjb.model.Person" table="person">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="username" column="username" type="string"></property>
<property name="password" column="password" type="string"></property>
<property name="age" column="age" type="int"></property>
<property name="registerDate" column="registerDate" type="date"></property>
</class>
</hibernate-mapping>
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.url">jdbc:sqlserver://localhost:1433/hibernate</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
PersonService
package com.yjb.service;
import com.yjb.model.Person;
public interface PersonService {
public void savePerson(Person person);
}
PersonServiceImpl
package com.yjb.service.impl;
import com.yjb.dao.PersonDao;
import com.yjb.dao.impl.PersonDaoImpl;
import com.yjb.model.Person;
import com.yjb.service.PersonService;
public class PersonServiceImpl implements PersonService {
@Override
public void savePerson(Person person) {
// TODO Auto-generated method stub
PersonDao personDao=new PersonDaoImpl();
personDao.savePerson(person);
}
}
[其他解释]
1.sql没有打印出来 原因未知
2.log4j:WARN No appenders could be found for logger (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider).
log4j:WARN Please initialize the log4j system properly.
启动tomcat会有这两句
3.我把文件上传到了我的资源里面 你能不能帮我分析一下