1. Web. XML configuration ` ` ` XML


Copy the code

OpenSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

OpenSessionInViewFilter
*.action

struts2
*.jsp

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2
*.action

contextConfigLocation
classpath:spring.xml

org.springframework.web.context.ContextLoaderListener

Java Struts 2: 1.commons-fileupload.jar Java Struts 2: 1.commons-fileupload.jar

2.commons-io. Jar (Commons project (Commons project is a common component in Java) IO subproject, is to handle exceptions)


Annotations. Jar (package with annotations support)


4. Aspectjrt.jar (AOP enabled package)


5. Aspectjweaver. Jar (AOP support package)


Cglib-nodep-2.1_3. jar (cglib-nodep-2.1_3.jar)


7.commons-pool.jar 8.commons-dbcp.jar hibernate 包: 1.hibernate3.jar(hibernate的核心jar包) 2.antlr-2.7.2.jar(语言转换工具,hibernate利用它实现HQL到SQL的转换) 3.commons-collections-3.2.1.jar(commons项目中的子项目,是对collection集合的封装) 4.dom4j-1.6.1.jar(对dom4j的封装,是解析xml文件的) 5.javassist-3.9.0.GA.jar(一个开源的分析、编辑和创建Java字节码的类库) 6.jta-1.1.jar(hibernate对事务的处理) 7.slf4j-api-1.6.4.jar(一个日志系统的服务的api) 8.slf4j-nop-1.6.4.jar(对slf4j-api-x.x.x.jar的一个实现) 9.ojdbc14.jar (oracle驱动) 10.mysql-connector-java-5.1.6-bin.jar (mySql驱动) 如果使用注解还需添加hibernate-annotations-3.4.0.GA包: 11.hibernate-annotations.jar 12.ejb3-persistence.jar 13.hibernate-commons-annotations.jar json需要的jar包: 1.commons-beanutils-1.8.2.jar 2.commons-collections-3.2.1.jar 3.commons-lang-2.5.jar 4.commons-logging-1.1.1.jar 5.ezmorph-1.0.6.jar 6.json-lib-2.2.3-jdk15.jar 另: EL表达式:jstl.jar excel表格:jxl.jar 操作pdf文件:iText-5.0.5.jar 统计图(JFreechart两个):jcommon-1.0.10.jar,jfreechart-1.0.6.jar “` 3.搭建三层架构 “`java 1.bean类 注解: @Entity @Table(name=”对应的表名”) 字段上加对应的注解 @Id(主键) @GeneratedValue(strategy=GenerationType.AUTO)(表明自增长) 也可以写明对应的关系,如一对一,一对多 “` “`java 2.action层 注解:@Controller(标明action层) @ParentPackage(“struts-default”)(继承的包,也可以是json-default) @Scope(“protorype”)(多例) @Namespace(“/”)(命名空间) 写入service类,在service类名上加上注解 @Resource 注入,写set,get方法 在方法上加上注解 @Action(value = “方法名”, results = { @Result(name = “success”, location = “/main.jsp”), @Result(name = “input”, location = “/login.jsp”) }) “` “`java 3.service实现层 注解:@Service 写入dao类,在dao类名上加上注解 @Resource 注入,写set,get方法 “` “`java 4.dao实现层 注解:@Repository 加入 @Autowired private JdbcTemplate jdbcTemplate; 不需要写set,get方法,可以直接用jdbcTemplate调方法 “` 4.创建文件jdbc.properties,spring.xml “`xml jdbc.properties: ## jdbc 连接配置 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://loaclhost:3306/spring-good jdbc.user=root jdbc.password=root ## c3p0 数据源配置 c3p0.minPoolSize=15 c3p0.maxPoolSize=25 c3p0.acquireIncrement=15 c3p0.checkoutTimeout=10000 c3p0.initialPoolSize=20 c3p0.maxIdleTime=20 c3p0.idleConnectionTestPeriod=60000 spring.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:aop=”http://www.springframework.org/schema/aop” xmlns:p=”http://www.springframework.org/schema/p” xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:context=”http://www.springframework.org/schema/context” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd”> <!– 自动扫描包下的文件 –> <context:component-scan base-package=”com.znsd.ssh”/> <!– 通过context:property-placeholder加载配置文件 –> <context:property-placeholder location=”classpath:jdbc.properties” /> <!– 配置c3p0连接池属性 –> <bean id=”dataSource” class=”com.mchange.v2.c3p0.ComboPooledDataSource” destroy-method=”close”> <property name=”driverClass” value=”com.mysql.jdbc.Driver” /> <property name=”jdbcUrl” value=”jdbc:mysql://localhost:3306/spring-good” /> <property name=”user” value=”${jdbc.user}” /> <property name=”password” value=”${jdbc.password}” /> <!– 队列中的最小连接数 –> <property name=”minPoolSize” value=”${c3p0.minPoolSize}”></property> <!– 队列中的最大连接数 –> <property name=”maxPoolSize” value=”${c3p0.maxPoolSize}”></property> <!– 当连接耗尽时创建的连接数 –> <property name=”acquireIncrement” value=”${c3p0.acquireIncrement}”></property> <!– 等待时间 –> <property name=”checkoutTimeout” value=”${c3p0.checkoutTimeout}”></property> <!– 初始化连接数 –> <property name=”initialPoolSize” value=”${c3p0.initialPoolSize}”></property> <!– 最大空闲时间,超出时间连接将被丢弃 –> <property name=”maxIdleTime” value=”${c3p0.maxIdleTime}”></property> <!– 每隔60秒检测空闲连接 –> <property name=”idleConnectionTestPeriod” value=”${c3p0.idleConnectionTestPeriod}”></property> </bean> <!– session工厂由spring来管理 –> <bean id=”sessionFactory” class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”> <!– 数据源配置 –> <property name=”dataSource” ref=”dataSource”></property> <!– 读取hibernate配置信息 –> <!– <property name=”configLocation” value=”classpath:hibernate.cfg.xml” /> –> <!– 自动加载映射文件 *表示匹配该文件下的所有映射文件 –> <property name=”packagesToScan”> <list> <value>com.znsd.ssh.bean</value> </list> </property> <!–各种hibernate属性 –> <property name=”hibernateProperties”> <props> <!– 方言 –> <prop key=”hibernate.dialect”>org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key=”hibernate.show_sql”>true</prop> <prop key=”hibernate.format_sql”>true</prop> <prop key=”hibernate.hbm2ddl.auto”>update</prop> <!– <prop key=”hibernate.current_session_context_class”>thread</prop> –> <prop key=”hibernate.current_session_context_class”>org.springframework.orm.hibernate4.SpringSessionContext </prop> </props> </property> </bean> <!– 数据源的注入 –> <bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”> <property name=”dataSource” ref=”dataSource”></property> </bean> <!– 配置事务 –> <bean id=”txManager” class=”org.springframework.orm.hibernate4.HibernateTransactionManager”> <property name=”sessionFactory” ref=”sessionFactory” /> </bean> <!– AOP切面拦截事务 –> <aop:config> <aop:pointcut id=”serviceMethod” expression=”execution(* com.znsd.ssh.service.impl.*.*(..))” /> <aop:advisor advice-ref=”txAdvice” pointcut-ref=”serviceMethod” /> </aop:config> <!– 事务的通知方式 –> <tx:advice id=”txAdvice” transaction-manager=”txManager”> <tx:attributes> <!– read-only 事务为只读,一般查询数据可把该属性设置为true,可以提升效率 –> <tx:method name=”find*” propagation=”REQUIRED” read-only=”true” /> <tx:method name=”search*” propagation=”REQUIRED” read-only=”true” /> <tx:method name=”query*” propagation=”REQUIRED” read-only=”true” /> <!– propagation为事务的传播属性,常用为REQUIRED:如果当前线程有事务就直接使用当前事务,没有就创建一个事务 –> <tx:method name=”add*” propagation=”REQUIRED” /> <tx:method name=”submit*” propagation=”REQUIRED” /> <tx:method name=”save*” propagation=”REQUIRED” /> <tx:method name=”insert*” propagation=”REQUIRED” /> </tx:attributes> </tx:advice> </beans> “` 


I don’t know what happened…