码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-Struts2-Hibernate Maven整合

时间:2014-09-05 23:54:22      阅读:675      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   使用   java   ar   for   


myeclipse建立maven项目的过程我就省略了

看下项目截图

bubuko.com,布布扣

pom.xml

<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.iss</groupId>
	<artifactId>maven02</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>maven02 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<!-- log4j 日志 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- servlet API -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1-b09</version>
			<scope>provided</scope>
		</dependency>
		<!-- MySQL驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.24</version>
		</dependency>
		<!-- mchange C3P0 数据源 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5-pre2</version>
		</dependency>
		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<!-- fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.29</version>
		</dependency>

		<!-- struts2 -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.14</version>
			<exclusions>
				<exclusion>
					<groupId>javassist</groupId>
					<artifactId>javassist</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.3.14</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>
					struts2-convention-plugin</artifactId>
			<version>2.3.14</version>
		</dependency>
		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.2.2.RELEASE</version>
		</dependency>
		<!-- aspectjweaver -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.2</version>
		</dependency>
		<!-- hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.2.0.Final</version>
		</dependency>

	</dependencies>
	<build>
		<!-- Maven插件 -->
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
			</plugin>

			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-javadoc-plugin</artifactId>
				<configuration>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.7.2</version>
				<configuration>
					<forkMode>once</forkMode>
					<argLine>-Dfile.encoding=UTF-8</argLine>
				</configuration>
			</plugin>

		</plugins>

	</build>
</project>
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>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/spring.xml,/WEB-INF/classes/spring-hibernate.xml,</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--Hibernate的session丢失解决方法 -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- struts2配置 -->
	<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-list>
</web-app>

database.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=root
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true

spring-hibernate.xml

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-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
	">
	<!-- 使用C3P0数据源,MySQL数据库 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- MySQL5 -->
		<property name="driverClass" value="${driverClassName}"></property>
		<property name="jdbcUrl" value="${url}"></property>
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<property name="maxPoolSize" value="40"></property>
		<property name="minPoolSize" value="1"></property>
		<property name="initialPoolSize" value="1"></property>
		<property name="maxIdleTime" value="20"></property>
	</bean>


	<!-- session工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
			</props>
		</property>

		<!-- 注解方式配置 -->
		<property name="packagesToScan">
			<list>
				<value>com.iss.model</value>
			</list>
		</property>


	</bean>

	<!-- 配置事务 -->
	<bean name="txManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager" />


</beans>

spring.xml

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">
	<!--启用注解 -->
	<context:annotation-config />

	<context:component-scan
		base-package="com.iss.action,com.iss.dao.impl,com.iss.service"></context:component-scan>

	<!-- 引入属性文件Hibernate属性配置 -->
	<context:property-placeholder location="classpath:database.properties" />

	<!-- 自动扫描dao和service包(自动注入) -->
	<!-- <context:component-scan base-package="com.iss*" /> -->

</beans>

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>
	<!-- 将Action交给spring容器管理 -->
	<constant name="struts.objectFactory" value="spring" />
	<!-- 模式为开发模式,修改xml配置文件会自动加载,项目交付的时候设置为false,以免影响性能 -->
	<constant name="struts.devMode" value="true" />
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- 字符集编码 -->
	<constant name="struts.i18n.encoding" value="utf-8" />
	<package name="defaultPackage" namespace="/" extends="struts-default">
		<action name="userAction" class="com.iss.action.UserAction"
			method="saveUser">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

src/main/java/中各个包的内容

com.iss..action.UserAction

package com.iss.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.iss.model.User;
import com.iss.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("userAction")
public class UserAction extends ActionSupport {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	// private UserService<User> userService;
	//
	// @Autowired
	// public void setUserService(UserService<User> userService) {
	// this.userService = userService;
	// }
	@Autowired
	private UserService userService;

	public String saveUser() {
		System.out.println(userService);
		userService.save(user);
		return "success";
	}

}

com.iss.dao.UserDao

package com.iss.dao;

import com.iss.model.User;

public interface UserDao<T> {
	public User save(T o);

}

com.iss.dao.impl.UserDaoImpl

package com.iss.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.iss.dao.UserDao;
import com.iss.model.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao<User> {

	@Autowired
	private SessionFactory sessionFactory;

	//
	// public void setSessionFactory(SessionFactory sessionFactory) {
	// this.sessionFactory = sessionFactory;
	// }
	//
	// public SessionFactory getSessionFactory() {
	// return sessionFactory;
	// }

	@Override
	public User save(User o) {
		Session session = sessionFactory.getCurrentSession();
		session.save(o);
		return o;
	}

}

com.iss.model.User

package com.iss.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User implements Serializable {
	private int id;
	private String name;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

com.iss.service.UserService

package com.iss.service;

import com.iss.model.User;

public interface UserService {

	public User save(User user);
}

com.iss.service.impl.UserServiceImpl

package com.iss.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.iss.dao.UserDao;
import com.iss.model.User;
import com.iss.service.UserService;


@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao<User> userDao;


	@Override
	public User save(User user) {
		// TODO Auto-generated method stub
		return userDao.save(user);
	}


}

index,jsp

<body>
	<form action="userAction" method="post">
		<table width="207" border="0" align="center">
			<tr>
				<td colspan="2" align="center" nowrap="nowrap">用户注册</td>
			</tr>
			<tr>
				<td width="68" nowrap="nowrap">用户名</td>
				<td width="127" nowrap="nowrap"><label> <input
						name="user.name" type="text" id="username" size="20" /> </label></td>
			</tr>
			<tr>
				<td colspan="2" align="center" nowrap="nowrap"><label>
						<input type="submit" value="注册" /> <input type="reset" value="重填" />
				</label></td>
			</tr>
		</table>
	</form>
</body>

success.jsp

<body>${user.name }
</body>

写完之后在项目上点击右键选择run as  mvn  install,build success 之后将项目导入tomcat服务器在浏览器地址栏输入地址测试即可!






Spring-Struts2-Hibernate Maven整合

标签:des   blog   http   os   io   使用   java   ar   for   

原文地址:http://blog.csdn.net/wangdianyong/article/details/39085971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!