一、分析功能
登陆、增删改查
二、环境搭建
1.导jar包
struts2+spring+Hibernate3
2. 根据实际情况在数据库中建表
3.按表分包写domain类
注意:
- 包的结构为:公司域名反写+项目名+表名+domain/dao/service+(impl)+domain类(domainService/domain/Dao),例如:
- 注意表中的多对一关系与一对多关系,一对多使用Set集合Set=new HashSet,多对一使用对象。
4.逐类写domain.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> <class name="com.web.crm.post.domain.CrmPost" table="crm_post"> <id name="postId"> <generator class="uuid"></generator> </id> <property name="postName"></property> <!-- 多对一:多个职务对应一个部门 --> <many-to-one name="department" class="com.web.crm.department.domain.CrmDepartment" column="depId"></many-to-one> <!-- 一对多:一个部门对应多个职务 --> <set name="crmStaffs"> <key column="postId"></key> <one-to-many class="com.web.crm.staff.domain.CrmStaff"/> </set> </class> </hibernate-mapping>
上面这个xml中包含多对一关系与一对多关系,主键使用uuid方式
- <!-- 多对一:多个职务对应一个部门 -->(多对一实际上也是一对一)
<many-to-one name="一的名字(在类中的名字)" class="一的全限定类名" column="一的主键,也就是当前表的外键"/> - <!-- 一对多:一个部门对应多个职务 -->(在类中以Set集合的形式出现)
<set name="多的名字(类中Set的名字)">
<key column="一的主键,也就是当前类的外键"></key>
<one-to-many class="多的全限定类名,及Set集合中的T"/>
</set>
5. 写Spring配置文件
开发中习惯将源码文件与配置文件分文件夹放置,新建的文件夹称为源码文件夹,在编译完成后会自动合并在一起。
一般来说,将公共的配置项写在applicationContext.xml里,其他类型的写在applicationContext-domain.xml里,最后在总的配置文件里import进来。
公共配置项一般包括数据源dataSource配置,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:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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"> <!-- 公共配置项 --> <!-- 1.1加载properties文件 --> <context:property-placeholder location="classpath:jdbcInfo.properties"/> <!-- 1.2 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 2配置SessioFactory --> <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">true</prop> </props> </property> <property name="mappingLocations" value="classpath:com/web/crm/*/domain/*.hbm.xml"></property> </bean> <!-- 3事务管理 --> <!-- 3.1 事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 3.2 事务详情 *增删改:读写;查询:只读 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*"/> <tx:method name="update*"/> <tx:method name="delete*"/> <tx:method name="find*" read-only="true"/> <tx:method name="login*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3.3 aop编程 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.web.crm.*.service..*.*(..))"/> </aop:config> <!-- 导入其他配置文件 --> <!-- 员工 --> <import resource="applicationContext-staff.xml"/> </beans>读入的数据库配置文件为:(名称为jdbcInfo.properties)
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/crm jdbc.user=root jdbc.password=123
6. struts配置
配置文件位置同spring,不同点在于struts.xml文件必须在config文件夹下。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.devMode" value="true"></constant> <constant name="struts.ui.theme" value="simple"></constant> <!-- 公共项 --> <package name="common" namespace="/" extends="struts-default"> </package> <include file="struts/struts-staff.xml"></include> </struts>
7.配置web.xml
web.xml中主要配置3项东西:
- spring配置文件位置
- 加载spring配置文件对应的监听器
- struts的过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 1.1 spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!-- 1.2 加载spring配置文件使用监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 1.2 加载spring配置文件使用监听器 --><filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>