作为一个学习中的码农,一直学习才是我们的常态,所以最近学习了SSH(Spring,Struts2,Hibernate)整合,数据库用的MySQL。
写了一个简单的例子,用的工具是IntelliJ Idea,写的Maven项目。
1.首先创建项目
我们看一下项目架构
2.创建好项目我们开始项目的第一步,一如jar包,我们首先考虑一个问题需要什么jar包,会有什么问题,jar包冲突问题怎么解决。
笔者遇到的问题比较少,但是遇到了一个log4j与hibernate带的jbossjar包版本冲突。最后注释掉log4j就可以了
<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/maven-v4_0_0.xsd">
<parent>
<artifactId>Spring</artifactId>
<groupId>cn.curry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SSH02</artifactId>
<packaging>war</packaging>
<name>SSH02 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--单测-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
</dependency>
<!--javaee-->
<dependency>
<groupId>javax.javaee</groupId>
<artifactId>javaee</artifactId>
<version>6.0-alpha-1</version>
<classifier>sources</classifier>
</dependency>
<!--slf4j日志-->
<!-- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>-->
<!--log4j日志-->
<!-- <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>-->
<!--spring核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!--aspectJ包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!--spring-jdbcjar 包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!--dbcp包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency> <!--c3p0jar 包-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--mysql数据库驱动-->
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34_1</version>
</dependency>
<!--hibernate核心包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<!--oracle数据库-->
<!--<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>-->
<!--jta的jar包-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!--支持缓存的核心包-->
<!-- <dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.4.3 </version>
</dependency>-->
<!--支持缓存的依赖包-->
<!--<dependency>
<groupId>backport-util-concurrent</groupId>
<artifactId>backport-util-concurrent</artifactId>
<version>3.1</version>
</dependency>-->
<!-- jstl jar包-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.1</version>
</dependency> <dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
<!-- mybatis jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.8</version>
</dependency> <dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.8</version>
</dependency>
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.alexkasko.springjdbc</groupId>
<artifactId>springjdbc-iterable</artifactId>
<version>1.0.3</version>
</dependency>--> </dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
3.开始代码书写,创建实体类,小配置
package cn.curry.entity; /**
* Created by Curry on 2017/4/2.
*/ public class Stock {
private Integer sid;
private String sname;
private Integer scount; public Integer getSid() {
return sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public Integer getScount() {
return scount;
} public void setScount(Integer scount) {
this.scount = scount;
}
}
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.curry.entity">
<class name="Stock" table="stock">
<id name="sid">
<generator class="native"/>
</id>
<property name="sname"/>
<property name="scount"/>
</class>
</hibernate-mapping>
实体类小配置不多说。
3.然后我们开始写dao及其实现
dao层
package cn.curry.dao; import cn.curry.entity.Stock; /**
* Created by Curry on 2017/4/2.
*/
public interface IStockDao {
public int addStock(Stock stock);
}
实现层
package cn.curry.dao.impl; import cn.curry.dao.IStockDao;
import cn.curry.entity.Stock;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import java.io.Serializable; /**
* Created by Curry on 2017/4/2.
*/
public class StockDao implements IStockDao {
private SessionFactory sessionFactory;
public int addStock(Stock stock) {
Session session = sessionFactory.getCurrentSession();
Serializable save = session.save(stock);
Integer count=(Integer)save; return count.intValue();
} public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
4.然后我们开始写service
package cn.curry.service; import cn.curry.entity.Stock; /**
* Created by Curry on 2017/4/3.
*/
public interface IStockService {
public int addStock(Stock stock);
}
接下来写实现
package cn.curry.service.impl; import cn.curry.dao.IStockDao;
import cn.curry.entity.Stock;
import cn.curry.service.IStockService; /**
* Created by Curry on 2017/4/3.
*/
public class StockService implements IStockService{
private IStockDao stockDao;
public int addStock(Stock stock) {
return stockDao.addStock(stock);
} public IStockDao getStockDao() {
return stockDao;
} public void setStockDao(IStockDao stockDao) {
this.stockDao = stockDao;
}
}
简单说两句,以上都是一些最基础的东西,可以看出我们是对数据库stock表添加一条数据。
5.继续我们的重点了开始书写Action层
package cn.curry.action; import cn.curry.entity.Stock;
import cn.curry.service.IStockService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; /**
* Created by Curry on 2017/4/4.
*/
//继承ActionSupport使其成为action类,实现ModelDriven实现属性名自动装配
public class StockAction extends ActionSupport implements ModelDriven<Stock>{
//植入stock实体类
private Stock stock;
//植入iStockService业务层
private IStockService iStockService; public StockAction(){
stock=new Stock();
}
@Override//重写execute
public String execute() throws Exception {
int count = iStockService.addStock(stock);
if (count>0){
return SUCCESS;
}else{
return INPUT;
} } public Stock getModel() {
return stock;
} public Stock getStock() {
return stock;
} public void setStock(Stock stock) {
this.stock = stock;
} public IStockService getiStockService() {
return iStockService;
} public void setiStockService(IStockService iStockService) {
this.iStockService = iStockService;
}
}
6.我们先写页面最后在写最重点的xml配置
我们准备了两个页面一个index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>添加</title>
</head>
<body>
<h2>添加股票</h2>
<form action="addStock" method="post">
<input name="sname"/>
<input name="scount"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
第二个成功页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>添加成功!!!</h1>
</body>
</html>
7.最后的配置文件部分了,我们先写一个数据库连接的配置文件jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///y2163
jdbc.username=root
jdbc.password=1234
然后我们看strust2的配置strus.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="messages" />
<package name="default" namespace="/" extends="struts-default">
<!--控制权交给Spring容器所以class配置的是Spring配置的注入id-->
<action name="addStock" class="stockAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package> </struts>
接下来是我们至关重要的的applicationContext.xml的配置,里面配置连接数据库,关联hibernate,关联Struts2
<?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:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--<context:component-scan base-package="cn.curry"></context:component-scan>-->
<!--关联jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置注入sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--hibernate属性配置,代替hibernate的大配置-->
<property name="hibernateProperties">
<props>
<!--sql方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</prop>
<!--是否输出sql语句-->
<prop key="hibernate.show_sql">true</prop>
<!--是否格式化输出sql语句-->
<prop key="hibernate.format_sql">true</prop>
<!--是否与当前线程绑定-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--关联小配置-->
<property name="mappingDirectoryLocations" value="classpath:cn/curry"></property>
</bean>
<!--注入dao-->
<bean id="stockDao" class="cn.curry.dao.impl.StockDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--注入service-->
<bean id="stockService" class="cn.curry.service.impl.StockService">
<property name="stockDao" ref="stockDao"></property>
</bean>
<bean id="stockAction" class="cn.curry.action.StockAction">
<property name="iStockService" ref="stockService"></property>
</bean>
<!--事物管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--aop配置事物管理-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="piontCut" expression="execution(* *..service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="piontCut"/>
</aop:config>
</beans>
最后配置一下web.xml,就可以发布项目了
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<!--读取配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置Struts2-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--欢迎页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后我们完成了我们项目的全部配置,最后我们启动程序看一下结果
首先看一下数据库的数据
然后启动服务,看页面
然后我们再看一下数据库
我们看到成功的在数据库添加了一条数据。
到此为止,我们的xml整合已经全部完成。
总结:我们有大量的配置,所以我们要小心不要写错名字,引用错名字,最后在编写类的时候不要引用错包,主要还是多写多练,熟悉每一步的配置。