6.编写代码
基本框架建立好后,我们添加几个类,展示一下目录:
下面是示例代码
先建立数据库ssh_test,再编写实体类建表
建立表时需要用到一些工具,我们建立一个工具包
domain包
User.java
package ssh.dan.domain; public class User { private String userName; private String passWord; 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; } }
User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="ssh.dan.domain.User" table="t_user"> <id name="userName"> <generator class="assigned"/> </id> <property name="passWord"/> </class> </hibernate-mapping>
配置文件
hibernate.cfg.xml(配置数据库及映射文件)
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh_test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">dandan</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="ssh/dan/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
dao包
UserDao.java
package ssh.dan.dao; import ssh.dan.domain.User; public interface UserDao { /** * 添加用户 * @param user */ public void addUser(User user); /** * 根据用户名查找用户 * @param userName * @return */ public User findUserByName(String userName); }
UserDaoImpl.java(使用了spring框架对Hibernate的集成的HibernateDaoSupport类)
package ssh.dan.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import ssh.dan.domain.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Override public void addUser(User user) { getHibernateTemplate().save(user); } @Override public User findUserByName(String userName) { return (User)getHibernateTemplate().load(User.class, userName); } }
manager包
UserManager.java
package ssh.dan.manager; import ssh.dan.domain.User; public interface UserManager { public void addUser(User user); public User findUserByName(String userName); }
UserManagerImpl.java(使用setter注入方式)
package ssh.dan.manager; import ssh.dan.dao.UserDao; import ssh.dan.domain.User; public class UserManagerImpl implements UserManager { private UserDao userDao; @Override public void addUser(User user) { try{ if(findUserByName(user.getUserName()) != null){ userDao.addUser(user); } }catch(Exception e){ e.printStackTrace(); } } @Override public User findUserByName(String userName) { User user = null; try{ user = userDao.findUserByName(userName); }catch(Exception e){ e.printStackTrace(); } return user; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
web包
UserActionForm.java
package ssh.dan.web; import org.apache.struts.action.ActionForm; public class UserActionForm extends ActionForm { private String userName; private String passWord; 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; } }
UserAction.java
package ssh.dan.web; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import ssh.dan.domain.User; import ssh.dan.manager.UserManager; public class UserAction extends Action { private UserManager userManager; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserActionForm uaf = (UserActionForm)form; User user = new User(); user.setUserName(uaf.getUserName()); user.setPassWord(uaf.getPassWord()); userManager.addUser(user); return mapping.findForward("success"); } public void setUserManager(UserManager userManager) { this.userManager = userManager; } }
配置文件
applicationContext-common.xml(配置sessionFactory和事务管理器):
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 那些类那些方法使用事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* ssh.dan.manager.*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> <!-- 事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> </beans>
applicationContext-daos.xml(声明dao包中的类):
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userDao" class="ssh.dan.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
applicationContext-managers.xml(声明manager包中的类,并注入相应的dao类):
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="ssh.dan.manager.UserManagerImpl"> <property name="userDao" ref="userDao"/> </bean> </beans>
applicationContext-actions.xml(声明action类,并注入相应的manager类):
<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean name="/user" class="ssh.dan.web.UserAction"> <property name="userManager" ref="userManager"/> </bean> </beans>
struts-config.xml(配置action和actionform,使用spring中的代理来配置action的type):
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="userForm" type="ssh.dan.web.UserActionForm"/> </form-beans> <action-mappings> <action path="/user" type="org.springframework.web.struts.DelegatingActionProxy" name="userForm" scope="request" > <forward name="success" path="/success.jsp"/> </action> </action-mappings> </struts-config>
web.xml(配置struts的actionservlet以及spring集成的ContextLoaderListener,CharacterEncodingFilter和OpenSessionInViewFilter):
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- Standard Action Servlet Configuration (with debugging) --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <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>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB18030</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
新建数据库ssh_test,启动Tomcat后表就建立好了,然后开始运行,结果:
index界面:
user_add界面:
success界面:
数据库结果: