一、新建JavaWeb項目,並修改字符編碼。html
<?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>
<?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>
<?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>
<?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>
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";
}
}
<%@ 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>
<?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>
<!-- 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>
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);
}
}
<?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>
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>
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);
}
}
<!-- 配置聲明式事物 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置處理事務的註釋 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
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;
}
}
<?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>
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());
}
}
@Test
public void TestTransaction(){
TestService testService = (TestService) ctx.getBean("testService");
testService.saveTwoUse();
}