maven hbm2java_Maven搭建struts2+spring+hibernate环境

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,

ff8d347d17f34861433a375411e21251.png

next.

8f1db1b2a1b899b4a503de64e68eea90.png

next,输入webapp,检索,选中maven-aechetype-webapp

b6a39ee9ea9c461f090cf409d4770788.png

next,

61abab4e6cfcbe42eeae5b621542d4f8.png

Finish即可。

项目结构如图(修改了pom.xml文件,使用JUnit4):

42a7e0640d7090ac3ddbf6714116630f.png

二、搭建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时候,就不需要联网下载了。

b12e80e82d083d6e43afa13848d19de6.png

这时候,所有需要的jar都到齐了。

后面一篇我将演示如何写一个小小的程序来验证ssh框架搭建是否成功。

Maven搭建struts2+spring+hibernate环境(二)

一、修改项目结构

上一篇中我们已经完成了jar的引入,现在开始构建测试程序。刚刚完成的 project图标上可能有一个红色的叉,只需要把项目复制--粘贴--重命名。即可解决,并不是项目搭建错误所致,这可能是IDE的一个bug吧,在 MyEclipse中搭建也出现这种情况,解决方法一样。

8293225dbe035fa51656caa6313c207d.png

但是,构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder(注意:是source folder,不是package!!)。

完成后的结构如下:

b7a84ebe0826cc70c5048d17e941826a.png

src/main.java:存放java源文件

src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。

src/test/java:存放test的java文件

src/test/resources:存放test时候所需的配置文件

二、编写程序

建立如下的结构

744d8362eccfb7a0c2fc474538ebe51d.png

编写配置文件,各种配置的含义我在此就不累赘了,有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

9bb7d836360ecaefe48b295125eebe4f.png

编译成功,没有错误

1878c266eb983052a6ad0e206a8cbb20.png

5d6056e853cff86b93820ca7c712a4a6.png

注册,查看数据库,成功!

26b89466be6da626ead1ce88ec1d05c1.png

至此,Maven搭建struts2+spring+hibernate开发环境就完成了,其中还有很多不足之处,希望大家指出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值