標籤:
1:當然是導jar包啦;
struts2:
spring:
hibernate:
至於這些jar包是什麼作用,我想就不必我解釋了,大家都懂得,ssh2基本的jar包;
還有一些其他jar包:struts2-spring-plugin-2.1.8.1.jar(struts2-spring整合使用的jar包) , c3p0-0.9.2-pre1.jar(使用連結池連結資料庫)
2:添加struts.xml檔案
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <!-- Overwrite Convention --> <package name="users" namespace="/lishun" extends="struts-default"> <!-- 這裡的action是叫spring管理了,所以這裡的class屬性值是寫的是該被注入Action的id名稱,後面的action類會有提到 --> <action name="users" class="usersAction" method="execute"> <result name="success">/index.jsp</result> </action> </package></struts>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定義struts的核心監聽器 --> <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 把struts與spring整合;;一定不要忘記導struts2-spring-plugin-2.1.8.1.jar 包 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application.xml</param-value> </context-param></web-app>
配置hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 執行資料庫操作時顯示sql語句 --> <property name="show_sql">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 自動建表:若表不存在就建立表,若存在就不建立,若實體類發生改變就重新建立表 --> <property name="hbm2ddl.auto">update</property> <!-- 匯入對應檔 --> <mapping resource="com/lishun/domian/Users.hbm.xml" /> </session-factory></hibernate-configuration>
配置application.xml
<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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 自動掃描com.lishun包下的所有bean,再通過註解來對這些bean進行注入值 --> <context:component-scan base-package="com.lishun"></context:component-scan> <!-- 載入屬性檔案來讀取資料庫連結字串 --> <context:property-placeholder location="claspath:DataBaseConnection.properties"/> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 把hibernate給spring管理 --> <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="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driverClass}"></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> <!-- 配置sessionFactory的事務,基於註解的方式 --> <bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/></beans>
DataBaseConnection.properties屬性檔案
3:測試
這是我的目錄結構,是一層調用著一層
控制器Action:
@Controller("usersAction")//控制器註解,並定義改bean的id是usersAction,@Scope("prototype")//聲明bean的範圍為prototype:每次從容器擷取bean都是新的對象。public class UsersAction extends ActionSupport { @Resource private IUserService servise; public String execute(){ servise.sava(); return "success"; }}
業務層seivice:
@Service//service層註解public class UserServiceBean implements IUserService { //給屬性注入值 @Resource public IUserDao userDao; @Override @Transactional //這個方法會自動開啟事務和提交事務,當有異常時就復原事務 public void sava() { userDao.sava(); }}
資料操作層dao:
public class UserDaoBean implements IUserDao { //從設定檔注入sessionfactory值 @Resource private SessionFactory seesionFactory; @Override public void sava() { Session session = seesionFactory.getCurrentSession(); System.out.println(session); Users u=new Users(); u.setUname("王尼瑪"); session.save(u); }}
最有一個javabean實體,這個實體很簡單就只有id和name兩個欄位,測試用的沒弄很多欄位上去
public class Users { private Integer uid; private String uname; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } @Override public String toString() { return "Users [uid=" + uid + ", uname=" + uname + "]"; }}
對應的對應檔
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.lishun.domian" > <!-- 定義類名和對應的表名 --> <class name="Users" table="tb_users"> <id name="uid" type="integer"> <!-- 主鍵是自增的 --> <generator class="native"></generator> </id> <property name="uname" type="string"> </property> </class></hibernate-mapping>
文章摘自:http://www.cnblogs.com/lishun1005/p/4412252.html
(摘錄筆記)JAVA學習筆記SSH整合搭建項目