1.建立普通的Javaweb項目,導入項目所必須的jar包。 2.配置web.xml文件。 3.在src下建立struts.xml。 4.在實體包下配置 實體名.hbm.xml 5.在src下建立applicationContext.xml。 6.在src下建立資料庫的相關配置信息db.proper ...
1.建立普通的Javaweb項目,導入項目所必須的jar包。
2.配置web.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>market</display-name> <!-- 讓spring隨web啟動而創建的監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring配置文件位置參數 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session作用範圍 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <!-- struts2核心過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
3.在src下建立struts.xml。
<?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> <constant name="struts.devMode" value="false" /> <package name="basicstruts2" extends="struts-default" namespace="/"> <!--自定義攔截器可以====不配置 begin--> <interceptors> <!-- 自定義攔截器 --> <interceptor name="myIntercetor" class="com.market.web.interceptor.MyInterceptor"> <!-- 指定方法不攔截 --> <param name="excludeMethods">login</param> </interceptor> <!-- 定義攔截器棧 --> <interceptor-stack name="myStack"> <interceptor-ref name="myIntercetor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 設置預設攔截器棧 --> <default-interceptor-ref name="myStack"/> <!--自定義攔截器可以====不配置 end--> <!-- 配置全局結果集 --> <global-results> <result name="login">/返回的頁面</result> </global-results> <!-- 用戶管理 --> <action name="userAction_*" class="userAction" method="{1}"> <result name="home">/返回的頁面</result> </action> </package> </struts>
4.在實體包下配置 實體名.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.market.entity.Product" table="t_product"> <id name="列名"> <column name="屬性名" length="長度值" /> <generator class="native" /> </id> <property name="屬性名"> <column name="列名" length="長度值"/> </property> </class> </hibernate-mapping>
5.在src下建立applicationContext.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" 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 http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 讀取db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0連接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 將SessionFactory配置到spring的容器中 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 將連接池註入到sessionFactory,hibernate通過連接池獲取連接 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate基本信息 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 將hibernate生成的sql語句列印到控制台 --> <prop key="hibernate.show_sql">true</prop> <!-- 將hibernate生成的sql語句格式化(語法縮進) --> <prop key="hibernate.format_sql">true</prop> <!-- 自動建表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 引入orm元數據 --> <property name="mappingLocations"> <list> <value>classpath:com/market/entity/*.xml</value> </list> </property> </bean> <!-- 配置核心事物管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 開啟spring組件掃描 --> <context:component-scan base-package="com.market"/> <!-- 支持spring註解 --> <context:annotation-config/> <!-- 開啟註解事物 --> <tx:annotation-driven/> </beans>
6.在src下建立資料庫的相關配置信息db.properties。
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///資料庫名稱
jdbc.user=用戶名
jdbc.password=密碼
7.建立一個log4j.properties記錄日誌信息。
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=e:\\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
### fatal error warn info debug trace
log4j.rootLogger=error, stdout
8.開發中使用註解@Controller,@Service,@Repository,@Transactional 等註入屬性。
9.我的目錄結構如下