1、新建JavaWeb项目,并修改字符编码。

打开,




<?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>

这个必须加,不加报错。
c3p0数据源
<?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>
核心包
依赖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>
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=utf8driverClass=com.mysql.jdbc.Driveruser=rootpassword=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(); }






原文:http://www.cnblogs.com/suiyue-/p/5740735.html