1、用户访问顺序 和 程序开发顺序
2、代码开发
在进行代码开发的时候,要遵循“程序开发顺序”,从entity->dao->service->action。
2.1、entity
User.java
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.rk.tax.entity" auto-import="true"> <class name="User" table="T_Users"> <id name="id" column="id" type="string" length="32"> <generator class="uuid.hex"></generator> </id> <property name="name" column="name" type="string" length="20" not-null="true"></property> <property name="dept" column="dept" type="string" length="20" not-null="true"></property> <property name="account" column="account" type="string" length="50" not-null="true"></property> <property name="password" column="password" type="string" length="50" not-null="true"></property> <property name="headImg" column="headImg" type="string" length="100"></property> <property name="gender" column="gender" type="boolean" not-null="true"></property> <property name="email" column="email" type="string" length="50"></property> <property name="mobile" column="mobile" type="string" length="20"></property> <property name="birthday" column="birthday" type="date" length="10" ></property> <property name="state" column="state" type="string" length="1"></property> <property name="memo" column="memo" type="string" length="200"></property> </class> </hibernate-mapping>
2.2、dao
UserDao.java
package com.rk.tax.dao;
import com.rk.core.dao.BaseDao;
import com.rk.tax.entity.User;
public interface UserDao extends BaseDao<User> {
}UserDaoImpl.java
package com.rk.tax.dao.impl;
import com.rk.core.dao.impl.BaseDaoImpl;
import com.rk.tax.dao.UserDao;
import com.rk.tax.entity.User;
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
}2.3、service
UserService.java
package com.rk.tax.service;
import java.io.Serializable;
import java.util.List;
import com.rk.tax.entity.User;
public interface UserService {
void save(User entity);
void update(User entity);
void delete(Serializable id);
User findById(Serializable id);
List<User> findAll();
}UserServiceImpl.java
package com.rk.tax.service.impl;
import java.io.Serializable;
import java.util.List;
import com.rk.tax.dao.UserDao;
import com.rk.tax.entity.User;
import com.rk.tax.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(User entity) {
userDao.save(entity);
}
public void update(User entity) {
userDao.update(entity);
}
public void delete(Serializable id) {
userDao.delete(id);
}
public User findById(Serializable id) {
return userDao.findById(id);
}
public List<User> findAll() {
return userDao.findAll();
}
}2.4、action
UserAction.java
package com.rk.tax.action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.rk.tax.entity.User;
import com.rk.tax.service.UserService;
public class UserAction extends ActionSupport {
/***** 1、业务数据 *****/
private List<User> userList; //用于显示用户列表
private User user; //用于添加、修改和删除单个用户
private String[] selectedRow; //用于删除多个用户
// {{
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String[] getSelectedRow() {
return selectedRow;
}
public void setSelectedRow(String[] selectedRow) {
this.selectedRow = selectedRow;
}
// }}
/***** 2、业务实现类 *****/
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/***** 3、页面响应操作 *****/
//列表页面
public String listUI()
{
userList = userService.findAll();
return "listUI";
}
//跳转到新增页面
public String addUI()
{
return "addUI";
}
//保存新增
public String add()
{
if(user != null)
{
userService.save(user);
}
return "list";
}
//跳转到编辑页面
public String editUI()
{
if(user != null && user.getId() != null)
{
user = userService.findById(user.getId());
}
return "editUI";
}
//保存编辑
public String edit()
{
if(user != null)
{
userService.update(user);
}
return "list";
}
//删除
public String delete()
{
if(user != null && user.getId() != null)
{
userService.delete(user.getId());
}
return "list";
}
//批量删除
public String deleteSelected()
{
if(selectedRow != null)
{
for(String id : selectedRow)
{
userService.delete(id);
}
}
return "list";
}
}3、代码配置
| 代码层 | 配置 |
| entity层 | hibernate配置(进行实体与数据表映射) |
| dao层 | spring配置 |
| service层 | spring配置 |
| action层 | spring配置(生成action对象) struts配置(进行action与uri之间的映射) |
3.1、entity层配置
(1)主要是User.hbm.xml
(2)将User.hbm.xml添加到Spring的sessionFactory的mappingLocations中
<!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:com/rk/*/entity/*.hbm.xml</value> </list> </property> </bean>
3.2、dao层配置
(1)将userDao添加到bean-dao.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userDao" class="com.rk.tax.dao.impl.UserDaoImpl" parent="baseDao"></bean> </beans>
这里有一点需要注意:parent="baseDao"。而baseDao定义在bean-base.xml文件中
<!-- 所有业务dao的parent --> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
同样需要注意:baseDao有abstract="true"属性,还包含<property>属性。
这样做的好处是,凡是指定了parent="baseDao"属性的bean,都会继承baseDao的<property>。
(2)将bean-dao.xml添加到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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="classpath:bean-base.xml"/> <import resource="classpath:com/rk/*/config/bean-*.xml"/> </beans>
3.3、service层配置
(1)添加userService到bean-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.rk.tax.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>
(2)添加bean-service.xml到applicationContext.xml中
3.4、action配置
(1)添加userAction到bean-action.xml中。这里需要注意的是:注册的action类应指定scope="prototype"。
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userAction" class="com.rk.tax.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> </beans>
(2)添加bean-action.xml到applcationContext.xml中
(3)添加userAction到struts-user.xml文件中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user_package" namespace="/tax" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="{1}">/WEB-INF/jsp/tax/user/{1}.jsp</result>
<result name="list" type="redirectAction">
<param name="actionName">user_listUI</param>
<param name="namespace">/tax</param>
</result>
</action>
</package>
</struts>(4)添加struts-user.xml到struts.xml中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 禁用动态方法访问 --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- 配置成开发模式 --> <constant name="struts.devMode" value="true" /> <!-- 配置拓展名为action --> <constant name="struts.action.extention" value="action" /> <!-- 把主题配置成simple --> <constant name="struts.ui.theme" value="simple" /> <!-- 设置创建对象的工厂 --> <constant name="struts.objectFactory" value="spring" /> <include file="com/rk/test/config/struts-test.xml"/> <include file="com/rk/tax/config/struts-user.xml"/> <!-- <package name="default" namespace="/" extends="struts-default"> <action name="" class=""> <result name="success"></result> </action> </package> --> </struts>
项目目录结构
原文:http://lsieun.blog.51cto.com/9210464/1835922