创建maven项目后,毫不犹豫,超简单傻瓜式搞定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> <!-- 属性 --> <properties> <argLine>-Dfile.encoding=UTF-8</argLine> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <struts.version>2.3.24</struts.version> </properties> <!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</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-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 依赖管理 --> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- 导入 struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> </dependency> <!-- servlet jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.15</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.2.0</version> </dependency> </dependencies> <build> <plugins> <!-- 设置编译版本为1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
接着我喜欢从web端开始,所以先弄的是struts2
一个简单的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>
可以弄个网页先试试好用不(单独测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>
显示结果页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就靠这个了)
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去,目录结构如下
实体类及其映射
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; } @Override public 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 持久层实现类
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); } @Override public 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核心配置文件进行整合
<?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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- dataSource数据源 --> <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> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事务的通知 --> <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> <!-- 事务通知跟切面组装 --> <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>
常见异常解决
java.lang.ClassCastException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter
这个异常要把servlet-api 改为provided
两个包都有相关内容会引起冲突,要把一个设置为从属(provided 级别低一级)
Line: 245 – com/opensymphony/xwork2/spring/SpringObjectFactory.java:245:-1
1.Struts与Spring不兼容,struts.xml中的action的class属性应注入Spring的bean
2.如果有struts2-spring-plugin-2.3.24.1.jar表明该插件引入工程后,会自动设置Struts的ObjectFactory为StrutsSpringObjectFactory,
从而让Spring的IOC容器来托管Struts的Action。但是工程web.xml中没有配置加载spring ApplicationContext相关的listener,所以导致了启动的问题。
java.sql.SQLException: ORA-00001: 违反唯一约束条件 (SCOTT.SYS_C0011456)
这个问题的原因是:customer.setCid(cid); 主键有自增或者sequence,与你提供的id冲突,执行添加操作hibernateTemplate.save(customer);时不设置customer
的id即可