Maven搭建struts2+spring+hibernate环境(一)
本文简单的使用STS的自带的maven插件工具搭建ssh(struts2+spring+hibernate)开发环境,图文并茂,简单上手,适合新手。
首先,STS自带的maven的插件已经默认有了maven,但是不建议使用默认的 maven。更改STS默认的maven可在windows-preferences-Maven 选项下,同时我将在另一篇文章里也有介绍,《STS,MyEclipse中Maven配置》 地址:http://blog.csdn.net/sgl731524380/article/details/8871470
一、新建Maven项目。
New--Project--Maven Project,
next.
next,输入webapp,检索,选中maven-aechetype-webapp
next,
Finish即可。
项目结构如图(修改了pom.xml文件,使用JUnit4):
二、搭建struts2+spring+hibernate
双击打开pom.xml,配置pom.xml文件。配置如下,
4.0.0
com.sgl
MSSH
war
0.0.1-SNAPSHOT
MSSH Maven Webapp
http://maven.apache.org
UTF-8
log4j
log4j
1.2.17
javax.servlet
javax.servlet-api
3.1-b09
provided
mysql
mysql-connector-java
5.1.24
com.mchange
c3p0
0.9.5-pre2
junit
junit
4.11
test
com.alibaba
fastjson
1.1.29
org.apache.struts
struts2-core
2.3.14
javassist
javassist
org.apache.struts
struts2-spring-plugin
2.3.14
org.apache.struts
struts2-convention-plugin
2.3.14
org.springframework
spring-core
3.2.2.RELEASE
org.springframework
spring-context
3.2.2.RELEASE
org.springframework
spring-jdbc
3.2.2.RELEASE
org.springframework
spring-beans
3.2.2.RELEASE
org.springframework
spring-web
3.2.2.RELEASE
org.springframework
spring-expression
3.2.2.RELEASE
org.springframework
spring-orm
3.2.2.RELEASE
org.aspectj
aspectjweaver
1.7.2
org.hibernate
hibernate-core
4.2.0.Final
maven-war-plugin
maven-compiler-plugin
1.6
1.6
utf-8
maven-resources-plugin
utf-8
maven-javadoc-plugin
utf-8
maven-surefire-plugin
2.7.2
once
-Dfile.encoding=UTF-8
保存,Maven会自动联网去下载所有需要的jar包,至于只写jar的名字,不用记住,只要去Maven中央仓库去搜索,Maven中央仓库地址:http://search.maven.org/
完成之后,多了很多jar,并且都放到了配置的本地仓库中,以后要再使用相同版本的jar时候,就不需要联网下载了。
这时候,所有需要的jar都到齐了。
后面一篇我将演示如何写一个小小的程序来验证ssh框架搭建是否成功。
Maven搭建struts2+spring+hibernate环境(二)
一、修改项目结构
上一篇中我们已经完成了jar的引入,现在开始构建测试程序。刚刚完成的 project图标上可能有一个红色的叉,只需要把项目复制--粘贴--重命名。即可解决,并不是项目搭建错误所致,这可能是IDE的一个bug吧,在 MyEclipse中搭建也出现这种情况,解决方法一样。
但是,构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder(注意:是source folder,不是package!!)。
完成后的结构如下:
src/main.java:存放java源文件
src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。
src/test/java:存放test的java文件
src/test/resources:存放test时候所需的配置文件
二、编写程序
建立如下的结构
编写配置文件,各种配置的含义我在此就不累赘了,有ssh使用基础的同学都能看懂的。
struts.xml
/success.jsp
/error.jsp
spring.xml
spring-hibernate.xml
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
${hibernate.hbm2ddl.auto}
${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
com.sgl.model
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
database.properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1url=jdbc:mysql://localhost:3306/mssh?useUnicode=true&characterEncoding=UTF-8
username=root
password=sgl
hibernate.hbm2ddl.auto=update
hibernate.show_sql=truehibernate.format_sql=true
web.xml
contextConfigLocation
classpath:spring.xml,classpath:spring-hibernate.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
openSessionInView
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSessionInView
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
编写java文件,采用MVC架构,使用hibernate,spring注解
User.java
packagecom.sgl.model;importjava.util.Date;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.Id;importjavax.persistence.Table;importjavax.persistence.Temporal;importjavax.persistence.TemporalType;importjavax.persistence.Transient;
@SuppressWarnings("serial")
@Entity
@Table(name= "tuser")public class User implementsjava.io.Serializable
{privateString id;privateDate regtime;privateString username;privateString password;
@Column(name="password",nullable=false,length=20)publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}privateString code;
@TransientpublicString getCode() {returncode;
}public voidsetCode(String code) {this.code =code;
}publicUser()
{
}publicUser(String id, Date regtime, String username,String password) {super();this.id =id;this.regtime =regtime;this.username =username;this.password =password;
}
@Id
@Column(name= "id", nullable = false, length = 36)publicString getId() {returnid;
}public voidsetId(String id) {this.id =id;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name= "regtime", length = 7)publicDate getRegtime() {returnregtime;
}public voidsetRegtime(Date regtime) {this.regtime =regtime;
}
@Column(name= "username", unique = false, nullable = false, length = 100)publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}
}
UserDaoI.java
packagecom.sgl.dao;importjava.io.Serializable;public interface UserDaoI{publicSerializable save(T o);
}
UserDaoImpl.java
packagecom.sgl.dao.impl;importjava.io.Serializable;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Repository;importcom.sgl.dao.UserDaoI;
@Repository("userDao")public class UserDaoImpl implements UserDaoI{//注入sessionfactory
@AutowiredprivateSessionFactory sessionFactory;publicSerializable save(T o) {returnsessionFactory.getCurrentSession().save(o);
}
}
UserService.java
packagecom.sgl.service;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importcom.sgl.dao.UserDaoI;importcom.sgl.model.User;
@Service("userService")
@Transactionalpublic class UserService //之前这里写错成了UserServiceImpl,谢谢wangdianyong的提醒,此处已做修改
{//自动注入dao
@Autowiredprivate UserDaoIuserDao;public voidaddUser(User user)
{
userDao.save(user);
}
}
UserAction.java
packagecom.sgl.action;importjava.util.Date;importjava.util.UUID;importorg.apache.struts2.ServletActionContext;importorg.apache.struts2.convention.annotation.Action;importorg.apache.struts2.convention.annotation.Namespace;importorg.apache.struts2.convention.annotation.ParentPackage;importorg.apache.struts2.convention.annotation.Result;importorg.springframework.beans.factory.annotation.Autowired;importcom.sgl.model.User;import com.sgl.service.UserService; //修改为UserService
@controller("userAction")public classUserAction
{
@AutowiredprivateUserService userService;privateUser user;publicUser getUser()
{returnuser;
}public voidsetUser(User user)
{this.user =user;
}publicString reg()
{
user.setId(UUID.randomUUID().toString());
user.setRegtime(newDate());try{
userService.addUser(user);
ServletActionContext.getContext().getSession().put("user", user);
ServletActionContext.getContext().getSession().put("msg", "注册成功了,可以去登陆了");return "success";
}catch(Exception e)
{
e.printStackTrace();
ServletActionContext.getContext().getSession().put("msg", "注册失败了");return "error";
}
}
}
Maven搭建struts2+spring+hibernate环境(三)
后台功能都已完成,现在是前台的页面编写
index.jsp
注册用户注册 | |
用户名 | |
密 码 | |
success.jsp
Insert title here${msg }
error.jsp
Insert title here至此,项目编写基本完成。
右键项目---run as---maven install
编译成功,没有错误
注册,查看数据库,成功!
至此,Maven搭建struts2+spring+hibernate开发环境就完成了,其中还有很多不足之处,希望大家指出。