文章根据传智播客人事管理系系统OA整理,感谢传智播客。
1、新建JavaWeb项目,并修改字符编码。
在项目上右击选择最后一项,修改为UTF-8
1.1添加Junit4,右击项目选择Build path
打开,
添加以下jar包,写配置文件
2、添加struts2环境。
2.1添加过jar包之后,拷贝配置文件
2.2在web.xml中加入struts2最核心的过滤器
<?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">
<display-name>SSH-1</display-name>
<!--Struts2过滤器 -->
<filter>
<!--过滤器名称 -->
<filter-name>struts2</filter-name>
<!--过滤器类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--Struts2过滤器映射 -->
<filter-mapping>
<!--过滤器名称 -->
<filter-name>struts2</filter-name>
<!--过滤器映射 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
修改struts2的配置文件为
<?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>
<!-- 更改struts默认的主题 -->
<constant name="struts.ui.theme" value="simple"/>
<!-- 配置为开发模式 ,好处是修改过之后立即生效-->
<constant name="struts.devMode" value="true"></constant>
<!--声明包 -->
<package name="pm" namespace="/" extends="json-default">
<action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
<result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
<result name="success" type="redirect">/product_list</result>
<result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
<result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
<result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
<result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
<result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
<result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
</action>
</package>
</struts>
3、添加hibernate环境
jar包,配置文件,hibernate.hbm.xml 映射文件和 hibernate.cfg.xml主文件
这个必须加,不加报错。
c3p0数据源
3.1添加过jar包后,拷贝配置文件和映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 1.数据库连接信息 <property name="connection.url">jdbc:mysql:///itcastoa0720</property>
<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
<property name="connection.username">root</property> <property name="connection.password"></property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 2.其他配置 -->
<property name="hbm2ddl.auto">update</property>
<!-- 3.映射文件 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 测试 -->
<!-- <mapping resource="com/future/test/User.hbm.xml" /> -->
<mapping resource="com/future/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
- 只是一个例子,具体内容以后修改。
<?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 package="com.future.domain">
<class name="User" table="t_user">
<id name="id" type="int">
<column name="I_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String" unique="false">
<column name="S_NAME" />
</property>
<property name="loginName" type="java.lang.String">
<column name="S_LOGINNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="S_PASSWORD" />
</property>
<property name="phoneNum" type="java.lang.String">
<column name="S_PHONENUM" />
</property>
<!--
repairHistorys:本类和repairHistory一对多
-->
<set name="repairHistorys">
<key column="repairManId"></key>
<one-to-many class="RepairHistory" />
</set>
</class>
</hibernate-mapping>
4、添加spring环境 5个
核心包依赖AOP
子类代理
日志
之后考入配置文件:(注:这个配置文件必须自己手动创建,然后复制别人的内容)
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--自动扫描与装配bean 包含子包 -->
<context:component-scan base-package="com.future"></context:component-scan>
<!-- 导入外部得properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
</beans>
至此,jar包添加完毕
5、struts2与spring整合
写测试类
package cn.itcast.test;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
public String execute() throws Exception{
System.out.println("==>>testAction execute!!");
return "success";
}
}
5.1之后配置测试页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
struts2 添加成功!
</body>
</html>
struts2配置文件:
<?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>
<!-- 更改struts默认的主题 -->
<constant name="struts.ui.theme" value="simple"/>
<!-- 配置为开发模式 ,好处是修改过之后立即生效-->
<constant name="struts.devMode" value="true"></constant>
<!--声明包 -->
<package name="defult" extends="struts-default">
<!-- 配置测试用Action,为与spring整合,所以写全类名 -->
<action name="test" class="cn.itcast.test.TestAction">
<result name="success">Test.jsp</result>
</action>
<!-- <action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
<result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
<result name="success" type="redirect">/product_list</result>
<result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
<result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
<result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
<result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
<result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
<result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
</action> -->
</package>
</struts>
如果报错:在web.xml中添加
<!-- spring用于初始化容器对象得监听器配置监听器 -->
<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>
连个JSP页面放在WebContext下。
如果页面出现:
并且访问test,出现
代表struts2配置成功,
(整合与为整合主要看struts配置文件)
接下来测试spring
package cn.itcast.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void TestSpring(){
TestAction testAction = (TestAction) ctx.getBean("testAction");
System.out.println(testAction);
}
}
不成功,因为还没有配置;
在这出现注解
现在TestAction中添加注解
spring扫描到这个Bean,
在applicationContext中添加自动扫描
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--自动扫描与装配bean 包含子包 -->
<context:component-scan base-package="cn.itcast.test"></context:component-scan>
<!-- 导入外部得properties -->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
</beans>
输入
spring独立运行~~~
5.2接下来整合spring
5.2.1 添加spring监听(因为上面解决错误添加,现在就不用添加了)
5.2.2添加整合jar包
5.2.3测试
在测试页中添加一句话
显示
整合成功、
6、Hibernate与spring整合
6.1首先
在spring配置文件中 配置sessionFactory,
applicationContext文件,配置之前写属性文件
jdbcUrl=jdbc:mysql:///carmanage?useUnicode=true&characterEncoding=utf8
driverClass=com.mysql.jdbc.Driver
user=root
password=root
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--自动扫描与装配bean 包含子包 -->
<context:component-scan base-package="cn.itcast.test"></context:component-scan>
<!-- 导入外部得properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
</beans>
在springTest测试文件中测试sessionFactory
package cn.itcast.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void TestSpring(){
TestAction testAction = (TestAction) ctx.getBean("testAction");
System.out.println(testAction);
}
@Test
public void TestSessionFactory(){
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
System.out.println(sessionFactory);
}
}
在这会出现一个错误,因为找不到hibernate得映射文件,在是之前复制得时候多复制了,现在把它注释掉
在运行,成功~~
6.2配置事务管理,在spring得配置文件applicationContext中配置。
<!-- 配置声明式事物 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置处理事务的注释 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
6.3配置测试用的javaBean
package cn.itcast.domain;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6.4配置映射文件
<?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 package="cn.itcast.domain">
<class name="User" table="test_user">
<id name="id" type="int">
<column name="I_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String" >
<column name="S_NAME" />
</property>
</class>
</hibernate-mapping>
在hibernate得配置文件中增加mapping
6.5准备测试。
6.5.1首先建立server层
package cn.itcast.test;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.domain.User;
@Service("testService")
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUse(){
Session session = sessionFactory.getCurrentSession();
session.save(new User());
int a=1/0;
session.save(new User());
}
}
接下来测试事务,在springTest中添加
@Test
public void TestTransaction(){
TestService testService = (TestService) ctx.getBean("testService");
testService.saveTwoUse();
}
提示,
代表事务成功。
注释掉:
提示:
最后:完整测试
修改TestAction文件
修改Test.jsp
,运行访问
提示
完全整合完成~
7、资源分类
7.1测试和主要内容分开,并且把配置文件放到一起
7.2关于建包
7.3关于资源
8、配置日志
日志jar包
最后,关于日志在视频中找~~~。