代码都在这儿求给为大侠给瞅瞅
数据库里有一个user表,三个字段id,username,password
_________________________________
User.java:
public class User implements java.io.Serializable
{
private int id;
private String username;
private String password;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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 User(String username, String password)
{
super();
this.username = username;
this.password = password;
}}
___________________________________________________
UserDao.javapublic interface UserDao
{
public abstract boolean addUser(User user);
}
---------------------------------------------------------
UserDaoImpl.java
public class UserDaoImpl implements UserDao
{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
} @Override
public boolean addUser(User user)
{
try {
HibernateTemplate hibernateTemplate =new HibernateTemplate(sessionFactory);
hibernateTemplate.saveOrUpdate(user);
return true;
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}}
--------------------------------------------------------------
RegisterAction.java:{
private User user;
private UserDaoImpl userDaoImpl;public User getUser()
{
return user;
}public void setUser(User user)
{
this.user = user;
}public void setUserDaoImpl(UserDaoImpl userDaoImpl)
{
this.userDaoImpl = userDaoImpl;
}@Override
public String execute() throws Exception
{
if(userDaoImpl.addUser(user))
return SUCCESS;
else
return INPUT;
}}
---------------------------------------------------------
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-3.0.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring_hibernate</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123321</value>
</property>
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean><bean id="userDaoImpl" class="com.jeedroid.daoimpl.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean><bean id="registerAction" class="com.jeedroid.action.RegisterAction">
<property name="userDaoImpl">
<ref local="userDaoImpl"/>
</property>
</bean>
</beans>
--------------------------------------------------------------------------------
struts配置文件:
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="struts2" extends="struts-default">
<action name="register" class="registerAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
--------------------------------------------------------------------------------
User.hbm.xml:
<hibernate-mapping package="com.jeedroid.model">
<class name="User" table="user">
<id name="id" >
<generator class="native"/>
</id>
<property name="username"/>
<property name="password"/>
</class>
</hibernate-mapping>
数据库里有一个user表,三个字段id,username,password
_________________________________
User.java:
public class User implements java.io.Serializable
{
private int id;
private String username;
private String password;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
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 User(String username, String password)
{
super();
this.username = username;
this.password = password;
}}
___________________________________________________
UserDao.javapublic interface UserDao
{
public abstract boolean addUser(User user);
}
---------------------------------------------------------
UserDaoImpl.java
public class UserDaoImpl implements UserDao
{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
} @Override
public boolean addUser(User user)
{
try {
HibernateTemplate hibernateTemplate =new HibernateTemplate(sessionFactory);
hibernateTemplate.saveOrUpdate(user);
return true;
} catch (DataAccessException e) {
e.printStackTrace();
return false;
}
}}
--------------------------------------------------------------
RegisterAction.java:{
private User user;
private UserDaoImpl userDaoImpl;public User getUser()
{
return user;
}public void setUser(User user)
{
this.user = user;
}public void setUserDaoImpl(UserDaoImpl userDaoImpl)
{
this.userDaoImpl = userDaoImpl;
}@Override
public String execute() throws Exception
{
if(userDaoImpl.addUser(user))
return SUCCESS;
else
return INPUT;
}}
---------------------------------------------------------
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-3.0.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring_hibernate</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123321</value>
</property>
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean><bean id="userDaoImpl" class="com.jeedroid.daoimpl.UserDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean><bean id="registerAction" class="com.jeedroid.action.RegisterAction">
<property name="userDaoImpl">
<ref local="userDaoImpl"/>
</property>
</bean>
</beans>
--------------------------------------------------------------------------------
struts配置文件:
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="struts2" extends="struts-default">
<action name="register" class="registerAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
--------------------------------------------------------------------------------
User.hbm.xml:
<hibernate-mapping package="com.jeedroid.model">
<class name="User" table="user">
<id name="id" >
<generator class="native"/>
</id>
<property name="username"/>
<property name="password"/>
</class>
</hibernate-mapping>
解决方案 »
- 关于SSH的外键的问题
- socket链接服务器,发送header的请求中为什么要设置host端口号?
- 过滤器设置了,乱码问题很严重
- jxl导入excel2010报错
- java面试让说几个你常用的linux命令,怎么说才显得你牛B
- Hibernate3中getHibernateTemplate的返回结果
- 设计问题
- java如何将html转化成xml
- struts模式中又是这个错误信息:The server encountered an unexpected condition which prevented it from fulfilling the request.
- 很简单的问题,快来拿分
- SSH奇怪问题,只能获取一个对象的部分值
- 关于mysql的中文写入问题~~~求教高手解答!
Struts Problem ReportStruts has detected an unhandled exception:Messages:
attempt to create saveOrUpdate event with null entity
File: org/hibernate/event/spi/SaveOrUpdateEvent.java
Line number: 62