1,对于web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.4"> <display-name>广告管理系统</display-name> <!-- 在web.xml中 首先配置的是对spring进行配置 --> <!-- Spring核心配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value> </context-param> <!--由Sprng载入的Log4j配置文件位置--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <!--Spring刷新Log4j配置文件的间隔,单位为millisecond--> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"--> <context-param> <param-name>webAppRootKey</param-name> <param-value>admanager.root</param-value> </context-param> <!-- 这个是自己写的一个过滤器,将所有的编码全部统一到utf-8编码格式 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.kemp.servlet.SetCharacterEncodingFilter</filter-class> <!-- 变量设置,将SetCharacterEncodingFilter类中的变量进行设置,系统将自动将变量赋值 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</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> <!-- 配置Struts2核心控制器 --> <filter> <!-- 配置filter名字 --> <filter-name>struts2</filter-name> <!-- 配置filter实现类 --> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- 配置filter拦截的URL --> <filter-mapping> <!-- 配置Struts2核心控制器拦截所有用户请求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <!-- 配置filter拦截的URL --> <filter-mapping> <!-- 拦载所有用户请求 --> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 用于初始化Spring的Listener,这个监听器主要是用来自动加载applicationContext.xml的 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring的log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>10</session-timeout> </session-config> <!-- 一般来说,这个配置不是必需的,因为标签库已经被包括在struts2-core-2.0.11.jar中了。 如果您真的需要在web.xml中配置标签库,可以把/META-INF/taglib.tld文件复制到您的应用程序的WEB-INF目录下, 命名为struts-tags.tld --> <jsp-config> <taglib> <taglib-uri>/WEB-INF/struts-tags.tld</taglib-uri> <taglib-location>/WEB-INF/struts-tags.tld</taglib-location> </taglib> </jsp-config> </web-app>
2 因为在系统中应用了spring,所以spring的applicationContext.xml组要的好好的研究一下,即依赖注入之间的关系。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 引入init.properties中属性 --> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:init.properties</value> </property> </bean> <!-- 配置数据源,连接池使用c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass"> <value>${datasource.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${datasource.url}</value> </property> <property name="user"> <value>${datasource.username}</value> </property> <property name="password"> <value>${datasource.password}</value> </property> <property name="acquireIncrement"> <value>${c3p0.acquireIncrement}</value> </property> <property name="initialPoolSize"> <value>${c3p0.initialPoolSize}</value> </property> <property name="minPoolSize"> <value>${c3p0.minPoolSize}</value> </property> <property name="maxPoolSize"> <value>${c3p0.maxPoolSize}</value> </property> <property name="maxIdleTime"> <value>${c3p0.maxIdleTime}</value> </property> <property name="idleConnectionTestPeriod"> <value>${c3p0.idleConnectionTestPeriod}</value> </property> <property name="maxStatements"> <value>${c3p0.maxStatements}</value> </property> <property name="numHelperThreads"> <value>${c3p0.numHelperThreads}</value> </property> </bean> <!-- 配置Hibernate中的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 引入数据源 --> <property name="dataSource"> <ref local="dataSource" /> </property> <!-- 配置Hibernate对应的映射资源 --> <property name="mappingResources"> <list> <!-- <value>com/kemp/video/dept/Dept.hbm.xml</value> --> <value>com/kemp/video/dept/Dept.hbm.xml</value> <value>com/kemp/video/role/Role.hbm.xml</value> <value>com/kemp/video/users/Users.hbm.xml</value> <value>com/kemp/video/dictionary/Dictionary.hbm.xml</value> <value>com/kemp/video/subdictionary/SubDictionary.hbm.xml</value> <value>com/kemp/video/hotel/Hotel.hbm.xml</value> <value>com/kemp/video/advertiser/Advertiser.hbm.xml</value> <value>com/kemp/video/advertisement/Advertisment.hbm.xml</value> <value>com/kemp/video/adbanner/AdBanner.hbm.xml</value> </list> </property> <!-- 配置Hibernate的属性,包括断言,show_sql等 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size} </prop> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size} </prop> <prop key="hibernate.connection.release_mode"> ${hibernate.connection.release_mode} </prop> </props> </property> </bean> <!-- 配置事务管理器bean --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 为事务管理器注入sessionFactory" --> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 事务控制代理抽象定义 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <!-- 为事务代理bean注入一个事物管理器 --> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <!-- 定义事务传播属性 --> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="release*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <!-- DAO --> <bean id="deptdao" class="com.kemp.video.dept.DeptDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="roledao" class="com.kemp.video.role.RoleDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="usersdao" class="com.kemp.video.users.UsersDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="dictionaryDao" class="com.kemp.video.dictionary.DictionaryDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="subDictionaryDao" class="com.kemp.video.subdictionary.SubDictionaryDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="hotelDao" class="com.kemp.video.hotel.HotelDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置系统的DAO组件 --> <bean id="advertiserDao" class="com.kemp.video.advertiser.AdvertiserDao"> <!-- 依赖注入SessionFactory引用 --> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="advertismentdao" class="com.kemp.video.advertisement.AdvertismentDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="adBannerDao" class="com.kemp.video.adbanner.AdBannerDao"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- Target --> <bean id="deptTarget" class="com.kemp.video.dept.DeptService"> <property name="deptdao"> <ref bean="deptdao"/> </property> </bean> <bean id="roleTarget" class="com.kemp.video.role.RoleService"> <property name="roledao"> <ref bean="roledao"/> </property> </bean> <bean id="usersTarget" class="com.kemp.video.users.UsersService"> <property name="usersdao"> <ref bean="usersdao"/> </property> </bean> <bean id="dictionaryTarget" class="com.kemp.video.dictionary.DictionaryService"> <property name="dictionaryDao"> <ref bean="dictionaryDao"/> </property> </bean> <bean id="subDictionaryTarget" class="com.kemp.video.subdictionary.SubDictionaryService"> <property name="subDictionaryDao"> <ref bean="subDictionaryDao"/> </property> </bean> <bean id="hotelTarget" class="com.kemp.video.hotel.HotelService"> <property name="hotelDao"> <ref bean="hotelDao"/> </property> </bean> <!-- 配置业务逻辑服务类 --> <bean id="advertiserTarget" class="com.kemp.video.advertiser.AdvertiserService"> <!-- 依赖注入DAO引用 --> <property name="advertiserDao"> <ref bean="advertiserDao"/> </property> </bean> <bean id="advertismenterTarget" class="com.kemp.video.advertisement.AdvertismentService"> <property name="advertismentdao"> <ref bean="advertismentdao"/> </property> <property name="adBannerDao"> <ref bean="adBannerDao"/> </property> </bean> <bean id="adBannerTarget" class="com.kemp.video.adbanner.AdBannerService"> <property name="adBannerDao"> <ref bean="adBannerDao"/> </property> <property name="hotelDao"> <ref bean="hotelDao"/> </property> </bean> <!-- Services --> <bean id="deptservice" parent="transactionProxy"> <property name="target"> <ref bean="deptTarget"/> </property> </bean> <bean id="roleservice" parent="transactionProxy"> <property name="target"> <ref bean="roleTarget"/> </property> </bean> <bean id="usersservice" parent="transactionProxy"> <property name="target"> <ref bean="usersTarget"/> </property> </bean> <bean id="dictionaryService" parent="transactionProxy"> <property name="target"> <ref bean="dictionaryTarget"/> </property> </bean> <bean id="subDictionaryService" parent="transactionProxy"> <property name="target"> <ref bean="subDictionaryTarget"/> </property> </bean> <bean id="hotelService" parent="transactionProxy"> <property name="target"> <ref bean="hotelTarget"/> </property> </bean> <!-- 继承抽象事务代理 --> <bean id="advertiserService" parent="transactionProxy"> <!-- 依赖注入业务逻辑组件引用 --> <property name="target"> <ref bean="advertiserTarget"/> </property> </bean> <bean id="advertismentservice" parent="transactionProxy"> <property name="target"> <ref bean="advertismenterTarget"/> </property> </bean> <bean id="adBannerService" parent="transactionProxy"> <property name="target"> <ref bean="adBannerTarget"/> </property> </bean> <!-- view --> <bean id="deptAction" class="com.kemp.web.actions.DeptAction" singleton="false"> <property name="deptservice"> <ref bean="deptservice"/> </property> <property name="usersservice"> <ref bean="usersservice"/> </property> </bean> <bean id="roleAction" class="com.kemp.web.actions.RoleAction" singleton="false"> <property name="roleservice"> <ref bean="roleservice"/> </property> <property name="usersservice"> <ref bean="usersservice"/> </property> </bean> <bean id="usersAction" class="com.kemp.web.actions.UsersAction" singleton="false"> <property name="usersservice"> <ref bean="usersservice"/> </property> <property name="deptservice"> <ref bean="deptservice"/> </property> <property name="roleservice"> <ref bean="roleservice"/> </property> </bean> <bean id="dictionaryAction" class="com.kemp.web.actions.DictionaryAction" singleton="false"> <property name="dictionaryService"> <ref bean="dictionaryService"/> </property> </bean> <bean id="subDictionaryAction" class="com.kemp.web.actions.SubDictionaryAction" singleton="false"> <property name="subDictionaryService"> <ref bean="subDictionaryService"/> </property> <property name="dictionaryService"> <ref bean="dictionaryService"/> </property> </bean> <bean id="hotelAction" class="com.kemp.web.actions.HotelAction" singleton="false"> <property name="hotelService"> <ref bean="hotelService"/> </property> <property name="adBannerService"> <ref bean="adBannerService"/> </property> </bean> <!-- 配置系统的Action组件 --> <bean id="advertiserAction" class="com.kemp.web.actions.AdvertiserAction" singleton="false"> <!-- 依赖注入业务逻辑组件引用 --> <property name="advertiserService"> <ref bean="advertiserService"/> </property> <property name="advertismentservice"> <ref bean="advertismentservice"/> </property> </bean> <bean id="AdvertismentAction" class="com.kemp.web.actions.AdvertismentAction" singleton="false"> <property name="advertismentservice"> <ref bean="advertismentservice"/> </property> <property name="advertiserService"> <ref bean="advertiserService"/> </property> <property name="subDictionaryService"> <ref bean="subDictionaryService"/> </property> <property name="hotelService"> <ref bean="hotelService"/> </property> <property name="adBannerService"> <ref bean="adBannerService"/> </property> </bean> <!-- 将struts的事务交给spring,同时通过依赖注入,将三个service依次注入到Action中 --> <bean id="adBannerAction" class="com.kemp.web.actions.AdbannerAction" singleton="false"> <property name="adBannerService"> <ref bean="adBannerService"/> </property> <property name="subDictionaryService"> <ref bean="subDictionaryService"/> </property> <property name="hotelService"> <ref bean="hotelService"/> </property> <property name="advertismentservice"> <ref bean="advertismentservice"/> </property> </bean> </beans>
在上面的applicationContext.xml 的配置文件中,特别是注意一下之间的依赖注入的关系。
题目说的是对系统的一些看法,但是却罗列了applicationContext.xml和web.xml。
这次说一下DAO吧
1)系统中DAO的设计
DAO大家都明白,这个是最底层(当然是相对的最底层~~),是直接与数据库打交道的,包括对数据库的增删修等操作。
很多的时候,我们是需要写一个操作基类(BaseDAO),然后所有的其他的类继承这个基类进行操作,如果基类的方法不够,则可以进行相应的扩展(即再写一下自己的方法,完善对应模块的基类)
在这儿写一下一个自己不熟悉的一种基类的方式(或者说是一种思路)
1)首先,先写一个基类的接口
public interface IGenericDAO<T, PK extends Serializable> { public abstract void save(T entity); public abstract void update(T entity); public abstract void delete(T entity); public abstract void deleteById(PK id); public void deleteAll(List<T> list) ; public abstract void saveOrUpdate(T entity); public abstract T findById(PK id) ; public List<T> findByExample(T exampleInstance) ; public List<T> findByExample(T exampleInstance, String[] excludeProperty) ; public List<T> findByProperty(String propertyName,final Object value) ; public List<T> findByProperty(String propertyName,final Object value, String orderBy, boolean isAsc) ; public List<T> findByTimeAndOther(String timeProperty,String sTime,String eTime,String [] propertyName,Object... values) ; public List<T> findByPropertys(String[] propertyNames, final Object[] values, int [] rowStartIdxAndCount, String orderType,String connType,boolean isPrecise) ; public abstract List<T> findAll() ; public List<T> findAll( String orderBy, boolean isAsc) ; public List<T> findByTime(String propertyName,final String startValue,final String endValue); public List<T> findByTime(String propertyName, final String startValue, final String endValue ,String orderProperty, boolean isDesc) ; public List<T> findByMax(String propertyName); public List<T> findByCriteria(Criterion... criterion) ; public List<T> findByCriteria(String orderBy, boolean isAsc,Criterion... criterion) ; public List<T> find(String hql, Object... values) ; public PaginationSupport findPageByPropertys(String[] propertyNames, final Object[] values, int [] rowStartIdxAndCount, String orderType,String connType,boolean isPrecise,int pageIndexCount) ; public void updateByHql(String hqlUpdate); public int searchCountsByHql(String hql, Object... values); public List<T> getEntityBySql(String sql) ; public List<T> searchBySql(String sql); public void deleteOrUpdateBySql(String sql); public int searchCountsBySql(String sql); public int findPageSize(String sql); public List<T> findBysqls(String sql, int[] rowStartIdxAndCount) ; public List<T> getFixedResultsBySql(String sql,int[] indexAndCount); public int updateByProcedure(String tableName, String value,String oldValue, String startTime,String endTime); public List<T> getLatestResult(String propertyName,final Object value, String orderBy, boolean isAsc,int[] indexAndCount); }
2)写完这个基类的接口,则必须要写相应的实现类
public class GenericDAO<T, PK extends Serializable> extends HibernateDaoSupport implements IGenericDAO<T, PK> { private Class<T> entityClass; private String className; protected QueryTool queryTool; protected CalculateTool calculateTool; // private static final Logger logger = Logger.getAnonymousLogger() ; @SuppressWarnings("unchecked") public GenericDAO() { // 通过范型反射,取得子类中定义的entityClass. this.entityClass = (Class<T>) ((ParameterizedType) getClass() .getGenericSuperclass()).getActualTypeArguments()[0]; className = entityClass.getSimpleName(); queryTool = new QueryTool(entityClass, className); calculateTool = new CalculateTool(entityClass, className); } public GenericDAO(Class<T> class1, SessionFactory sessionFactory) { this.entityClass = class1; className = entityClass.getSimpleName(); this.setSessionFactory(sessionFactory); queryTool = new QueryTool(entityClass, className); calculateTool = new CalculateTool(entityClass, className); } public void save(T entity) throws DataAccessException { super.getHibernateTemplate().save(entity); } public void update(T entity) throws DataAccessException { Assert.notNull(entity); super.getHibernateTemplate().update(entity); } public void saveOrUpdate(T entity) throws DataAccessException { Assert.notNull(entity); super.getHibernateTemplate().saveOrUpdate(entity); } public void delete(T entity) throws DataAccessException { Assert.notNull(entity); super.getHibernateTemplate().delete(entity); getHibernateTemplate().flush(); } @SuppressWarnings("unchecked") public void deleteById(PK id) throws DataAccessException { super.getHibernateTemplate().delete( getHibernateTemplate().load(entityClass, id)); } @SuppressWarnings("unchecked") public void deleteAll(List<T> list) throws DataAccessException { super.getHibernateTemplate().deleteAll(list); }
(当然还有很多方法的实现,但是在这儿就不写了)
这样一个基类就写完了,以后要做的就是继承它了,别忘了用接口哟~~~