orcal 数据库 maven架构 ssh框架 的全xml环境模版 及常见异常解决
創(chuàng)建maven項(xiàng)目后,毫不猶豫,超簡單傻瓜式搞定dependencies(pom.xml 就是maven的依賴管理),這樣你就有了所有你要的包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.swift</groupId><artifactId>ssh-maven</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>通用父工程</name><properties><spring.version>4.2.4.RELEASE</spring.version><struts2.version>2.3.24</struts2.version><hibernate.version>5.0.7.Final</hibernate.version><slf4j.version>1.6.6</slf4j.version><springdataredis.version>1.4.1.RELEASE</springdataredis.version><activemq.version>5.14.5</activemq.version><shiro.version>1.2.2</shiro.version><springdatajpa.version>1.10.4.RELEASE</springdatajpa.version><jedis.version>2.6.2</jedis.version><poi.version>3.11</poi.version><c3p0.version>0.9.1.2</c3p0.version><cxf.version>3.0.1</cxf.version><servlet.version>2.5</servlet.version><junit.version>4.11</junit.version></properties><dependencies><!-- 權(quán)限控制 框架 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-all</artifactId><version>${shiro.version}</version></dependency><!-- spring 框架 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><!-- spring data jpa 數(shù)據(jù)庫持久層 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>${springdatajpa.version}</version></dependency><!-- 消息隊(duì)列 MQ --><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-spring</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-kahadb-store</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-broker</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>${activemq.version}</version></dependency><!-- struts2 框架 --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>${struts2.version}</version><exclusions><exclusion><groupId>javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>${struts2.version}</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-convention-plugin</artifactId><version>${struts2.version}</version></dependency><!-- hibernate 框架 --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${hibernate.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>${hibernate.version}</version></dependency><!-- 數(shù)據(jù)庫連接池 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>mchange-commons-java</artifactId><version>0.2.12</version></dependency><!-- 日志框架 --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><!-- 工具包 --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!-- redis nosql 內(nèi)存數(shù)據(jù)庫 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis.version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>${springdataredis.version}</version></dependency><!-- oracle數(shù)據(jù)庫驅(qū)動(dòng),需要手動(dòng)安裝 --><!-- <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> --><dependency><groupId>com.oracle</groupId><artifactId>ojdbc14</artifactId><version>10.2.0.1.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- Excel解析工具類 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>${poi.version}</version></dependency><!-- Servlet、JSP --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>${servlet.version}</version><scope>provided</scope></dependency><!-- 單元測試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- 導(dǎo)入webservice依賴 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-extension-providers</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.codehaus.jettison</groupId><artifactId>jettison</artifactId><version>1.3.7</version></dependency><!-- 對象轉(zhuǎn)為json 工具包 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.37</version></dependency><dependency><groupId>com.colobu</groupId><artifactId>fastjson-jaxrs-json-provider</artifactId><version>0.3.1</version></dependency><!-- 引入json-lib的依賴 --><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version></dependency><!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j --><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>${jedis.version}</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.4.1.RELEASE</version></dependency><!-- 緩存 --><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></dependency><!-- 定時(shí)器|定時(shí)任務(wù) --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.1</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz-jobs</artifactId><version>2.2.1</version></dependency><!-- 阿里云發(fā)送短信涉及的jar包 --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>3.2.9</version></dependency><!-- 該坐標(biāo),中央倉庫中沒有,需要手動(dòng)導(dǎo)入到本地倉庫才好使 --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>1.0.0-SNAPSHOT</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target></configuration></plugin></plugins></build> </project>?
web端開始struts2
一個(gè)action
package com.swift.web;import com.opensymphony.xwork2.ActionSupport; import com.swift.entity.Customer; import com.swift.service.CustomerService;public class CustomerAction extends ActionSupport {private Integer cid;private String name;private Customer cus;private CustomerService customerService;public void setName(String name) {this.name = name;}public void setCustomerService(CustomerService customerService) {this.customerService = customerService;}public Customer getCus() {return cus;}public void setCid(Integer cid) {this.cid = cid;}public String findById() throws Exception {cus=customerService.findById(cid);return "list";}public String add() throws Exception {Customer customer=new Customer();customer.setName(name);customerService.add(customer);return "index";} }配合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><package name="swift" extends="struts-default" namespace="/"><action name="CustomerAction_*" class="customerAction" method="{1}"><result name="list" type="dispatcher">list.jsp</result><result name="index" type="redirect">index.jsp</result></action></package></struts>可以弄個(gè)網(wǎng)頁先試試好用不(單獨(dú)測struts不用spring整合,需要把上邊的class="customerAction"修改為Action類的全限定名)
index.jsp
<%@ 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> 歡迎您登錄首頁<hr /><form action="${pageContext.request.contextPath }/CustomerAction_findById" method="post"><span>請輸入客戶編號:</span><input type="text" name="cid" ><input type="submit" value="查詢" ></form> <hr /><form action="${pageContext.request.contextPath }/CustomerAction_add" method="post"><span>請錄入客戶姓名:</span><input type="text" name="name" ><input type="submit" value="增加" ></form> </body> </html>顯示結(jié)果頁list.jsp
<%@ 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><table><tr><td>編號:</td><td><input type="text" value="${cus.cid }"></td></tr><tr><td>姓名:</td><td><input type="text" value="${cus.name }"></td></tr></table> </body> </html>需要開啟struts的filter,(struts2就靠這個(gè)了)
web.xml
<?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_2_5.xsd" version="2.5"><display-name>ssh-maven</display-name><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>index.html</welcome-file><welcome-file>index.htm</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>然后走到hibernate去,目錄結(jié)構(gòu)如下
實(shí)體類及其映射
package com.swift.entity;public class Customer {private Integer cid;private String name;public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Customer [cid=" + cid + ", name=" + name + "]";}public Customer(Integer cid, String name) {this.cid = cid;this.name = name;}public Customer() {}}Customer.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.swift.entity.Customer" table="tb_customer" ><id name="cid" column="cid"><generator class="native"></generator></id><property name="name" column="name"></property></class></hibernate-mapping>CustomerDaoImpl.java 持久層實(shí)現(xiàn)類
package com.swift.dao.impl;import org.springframework.orm.hibernate5.HibernateTemplate;import com.swift.dao.CustomerDao; import com.swift.entity.Customer;public class CustomerDaoImpl implements CustomerDao{HibernateTemplate hibernateTemplate;public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public Customer findById(Integer cid) {return hibernateTemplate.get(Customer.class, cid);}@Overridepublic void add(Customer customer) {hibernateTemplate.save(customer);}}連接的庫?db.properties
driverClass = oracle.jdbc.driver.OracleDriver jdbcUrl=jdbc:oracle:thin:@192.168.189.101:1521:orcl user=scott password=tiger #driverClass = com.mysql.jdbc.Driver #jdbcUrl=jdbc:mysql://localhost:3306/maven #user=root #password=root以及最后的spring核心配置文件進(jìn)行整合
<?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:aop="http://www.springframework.org/schema/aop"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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><context:property-placeholder location="classpath:db.properties"/><!-- dataSource數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${driverClass}"></property><property name="jdbcUrl" value="${jdbcUrl}"></property><property name="user" value="${user}"></property><property name="password" value="${password}"></property></bean><!-- SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocations" value="classpath:hibernate.cfg.xml"></property></bean><!-- hibernateTemplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 事務(wù)管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 事務(wù)的通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="find*" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="*" propagation="REQUIRED" /></tx:attributes></tx:advice><!-- 事務(wù)通知跟切面組裝 --><aop:config><aop:pointcut expression="execution (* com.swift.service.impl.*.*(..))"id="pointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /></aop:config><bean id="customerDao" class="com.swift.dao.impl.CustomerDaoImpl"><property name="hibernateTemplate" ref="hibernateTemplate"></property></bean><bean id="customerService" class="com.swift.service.impl.CustomerServiceImpl"><property name="customerDao" ref="customerDao"></property></bean><bean id="customerAction" class="com.swift.web.CustomerAction"><property name="customerService" ref="customerService"></property></bean></beans>hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!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><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="com/swift/entity/Customer.hbm.xml" /></session-factory></hibernate-configuration>?
常見異常解決
?
java.lang.ClassCastException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter
這個(gè)異常要把servlet-api 改為provided
兩個(gè)包都有相關(guān)內(nèi)容會引起沖突,要把一個(gè)設(shè)置為從屬(provided 級別低一級)
?
Line: 245 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:245:-1
1.Struts與Spring不兼容,struts.xml中的action的class屬性應(yīng)注入Spring的bean
2.如果有struts2-spring-plugin-2.3.24.1.jar表明該插件引入工程后,會自動(dòng)設(shè)置Struts的ObjectFactory為StrutsSpringObjectFactory,
從而讓Spring的IOC容器來托管Struts的Action。但是工程web.xml中沒有配置加載spring ApplicationContext相關(guān)的listener,所以導(dǎo)致了啟動(dòng)的問題。
?
java.sql.SQLException: ORA-00001: 違反唯一約束條件 (SCOTT.SYS_C0011456)
這個(gè)問題的原因是:customer.setCid(cid); 主鍵有自增或者sequence,與你提供的id沖突,執(zhí)行添加操作hibernateTemplate.save(customer);時(shí)不設(shè)置customer
的id即可
?
轉(zhuǎn)載于:https://www.cnblogs.com/qingyundian/p/9169469.html
總結(jié)
以上是生活随笔為你收集整理的orcal 数据库 maven架构 ssh框架 的全xml环境模版 及常见异常解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ignite自定义函数
- 下一篇: Android Crash战斗日记(一、