搭建spring、Struts2、hibernate三大框架的环境

这里分两部分来讲:一、用myeclipse 2014 快速搭建环境,非常快捷, 大部分配置文件信息系统都帮我们写好,建议老手使用

         二、手动搭建环境,包括配置文件的手工编写,jar 的引入 etc

使用myeclipse插件搭建环境

  1、新建web 工程(这个不会可以不用往下看了)

  2、使用myeclipse插件

  一、添加spring 框架

 

    ①版本信息:

    

    ② applicationContext.xml 文件生成位置,直接默认就可以

    ③ 选择jar 简单的可以使用默认就可以

    ④ 点击finish

  二、添加Struts 2 框架

  1、同上操作

  

  2、依旧默认

    3、选择过滤器过滤的请求的种类

  4、jar信息同上,默认就可以

  

  三、添加hibernate 框架

    1、版本信息

    2、hibernate配置文件,以及sessionfactory的新建位置

    

    3、选择数据源,如果不懂可以参照我这篇文章的内容

    

    4、jar 默认即可 finish

    

    以上就搭建好了一个完整的ssh框架。

纯手工搭建ssh环境,新手建议这样尝试操作。这里讲的可能更好,因为使用比较好的方法去自行配置

  一、搭建spring框架 和hibernate框架:

    1、把jar 包copy到WEB-INF 目录下面的 lib 下,这些需要的包

    

 

    2、在src目录下添加applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!--引入db.properties -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置自动扫描的包 --><!-- 这个需要使用注解的方式注入属性 -->
<context:component-scan base-package="com.lzb.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 配置数据源 -->

<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 配置自动产生数据表 -->
<prop key="hibernate.show_sql">true</prop><!-- 是否显示sql -->
<prop key="hibernate.format_sql">true</prop><!-- 格式化sql -->
<prop key="hibernate.cache.use_second_level_cache">true</prop><!--开启二级缓存-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop><!--缓存区工厂类-->
<prop key="hibernate.cache.use_query_cache">true</prop><!-- 使用query-->
</props>
</property>

<!-- 用注解的方式产生数据表 -->
<property name="annotatedClasses">
<list>
<value>com.lzb.shop.entity.Picture</value><!--对应你的实体类-->
</list>
</property>

<!-- 用 hbm.xml 文件的方式产生数据表 -->
<!--
<property name="mappingResources">
<list>
<value>com/lzb/shop/entity/User.hbm.xml</value> 对应你的实体类映射文件
</list>
</property>
-->
</bean>

<!-- 注入sessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 配置user action -->
<bean id="indexAction" class="com.lzb.shop.controller.IndexAction"></bean>
<bean id="shopAction" class="com.lzb.shop.controller.ShopAction"></bean>
<bean id="productAction" class="com.lzb.shop.controller.ProductAction"></bean>

</beans>

 这里建议测试一下有没有搭建成功:

1、推荐使用Junit Test

  

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory sessionFactory = (SessionFactory)cxt.getBean(SessionFactory.class);
System.out.println(sessionFactory.openSession());

  如果打印无误就是成功,能够得到session 对象

2、测试能够自动产出数据表

  ①、写一个实体类

    

package com.lzb.shop.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @ClassName Picture
* @description
* @author Enzo
* @date 2017年8月31日
*/
@Table(name="picture")
@Entity
public class Picture {
private Integer id;
private String userName;
private String imgName;
private String imgPath;
private String imgType;
private String upTime;
private String imgDetial;
@Id
@GeneratedValue
@Column(name="ID",unique=true,nullable=false,length=4)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
} @Column(name="USERNAME",length=10)
public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} @Column(name="IMGNAME",length=20)
public String getImgName() {
return imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
@Column(name="IMGPATH",length=50)
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
@Column(name="IMGTYPE",length=20)
public String getImgType() {
return imgType;
}
public void setImgType(String imgType) {
this.imgType = imgType;
} @Column(name="UPTIME")
public String getUpTime() {
return upTime;
}
public void setUpTime(String upTime) {
this.upTime = upTime;
}
@Column(name="IMGDETIAL",length=100)
public String getImgDetial() {
return imgDetial;
}
public void setImgDetial(String imgDetial) {
this.imgDetial = imgDetial;
}
}

  ②、再次运行Junit,进入Navicat

    刷新观察有没有出现数据表,出现则测试成功

二、添加Struts2框架

  

  需要以上jar

  1、在src 目录下创建一个struts.xml 配置文件

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <!-- 开启开发者模式,作用,页面中报错信息会更加详细,默认false -->
<constant name="struts.devMode" value="true" /> <!-- 指定由spring负责action对象的创建(必选) -->
<constant name="struts.objectFactory" value="spring" /> <package name="index" namespace="/" extends="struts-default">
<action name="index" class="indexAction">
<result name="index">WEB-INF/index.html</result>
</action>
</package>
<!-- shop 相关的action -->
<package name="shop" namespace="/" extends="struts-default">
<action name="shop" class="shopAction">
<result name="shop">WEB-INF/category.html</result>
</action>
</package>
</struts>

  这样差不多就可以了

写一个action 测试

package com.lzb.shop.controller;

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

import com.lzb.shop.service.IndexService;

public class IndexAction{
@Autowired //这里用了注解自动注入了indexService
private IndexService indexService;
public String execute(){
String str = indexService.getStr();
System.out.println(str);
return "index";
}
}

  能进入对应的页面说明成功

感谢您的阅读!有问题欢迎留言询问,经常在博客园,相信能够及时回答您的问题。

  

ssh环境的搭建,基于注解和配置文件使用的更多相关文章

  1. 生产环境LAMP搭建 - 基于 fastcgi

    生产环境LAMP搭建 - 基于 fastcgi 由于在module模式,php只是已http的模块形式存在,无形中加重了http的服务负载,通常在企业架构中,使用fastcgi的模式,将所有的服务都设 ...

  2. 菜鸟学SSH(十七)——基于注解的SSH将配置精简到极致

    很早之前就想写一篇关于SSH整合的博客了,但是一直觉得使用SSH的时候那么多的配置文件,严重破坏了我们代码整体性,比如你要看两个实体的关系还得对照*.hbm.xml文件,要屡清一个Action可能需要 ...

  3. SpringMVC框架搭建 基于注解

    本文将以一个很简单的案例实现 Springmvc框架的基于注解搭建,一下全为个人总结 ,如有错请大家指教!!!!!!!!! 第一步:创建一个动态web工程(在创建时 记得选上自动生成 web.xml ...

  4. AD域环境的搭建 基于Server 2008 R2

    AD(Active Directory)即活动目录,微软的基础件.微软的很多产品如:Exchange Server,Lync Server,SharePoint Server,Forefront Se ...

  5. Ubuntu16.04环境下搭建基于三台主机的mysql galera cluster集群(实测有效)

    (注意: (1)文中红色字体部分不一定需要操作 (2)由于word文档编辑的原因,实际操作时部分命令需要手动输入!!直接复制粘贴会提示错误!! ) 一  搭建环境: 1 Ubuntu16.04版本(系 ...

  6. spring基于注解的配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA

    简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...

  8. 在nginx环境下搭建基于ssl证书的websocket服务转发,wss

    1.证书准备 本地调试,可以安装自签名证书,安装方法参考https本地自签名证书添加到信任证书访问 2.修改配置文件 将上面的配置文件拷贝到conf目录,添加或者修改节点如下 # HTTPS serv ...

  9. MAC OS环境下搭建基于Python语言的appium自动化测试环境

    #1 安装JDK java -version #2 下载SDK http://adt.android-studio.org/ 下载adt #3 配置sdk环境变量 打开终端,依次输入命令 vim .b ...

随机推荐

  1. Quartz.Net 与 Autofac 自动注入 的整合问题

    一.问题发现 今天早上在用 Quartz.Net 做定时扫描异常队列的功能模块时,发现处理异常队列的Job里面的ILog对象服务,Autofac没有自动注入进来. 然后在网上查阅相关资料,无奈发现 Q ...

  2. (转)eclipse安装jetty

    背景:在项目开发的过程中,一个老的项目使用的是jetty启动,在用tomcat启动的过程中出现了启动不了的异常,浪费了好多时间.因为项目一直是用jetty启动的,为了不浪费时间,也只好改变思路选择je ...

  3. Spring boot——logback 基础使用篇(一)

    1 简单日志配置 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logb ...

  4. markdown 常用格式API

    摘要 记录常用格式 参考:https://www.zybuluo.com/mdeditor 1. 标题 写法: 文字前加 #, 几个# 表示几级标题 标题下方增加 = 或 - 效果 标题1 标题2 标 ...

  5. 【JS】cookies 的使用

    摘要 cookies 的限制 IE6~IE6以下,每个域名最多20个cookie IE7及以上,每个域名最多50个cookie Firefox,每个域名最多50个cookie Opera,每个域名最多 ...

  6. 【B2B】2015 年B2B的春天

    摘要 看看关于B2B的现状,以及行业发展近况. 现状 http://www.cyzone.cn/a/20160115/288471.html 行业发展 蓬勃发展的行业: 方兴未艾的行业: 未来的行业:

  7. String类的方法

    String str = "hello";    /*   * 1.String当中跟char[]有关系的方法   */  char[] array = str.toCharArr ...

  8. docker的简单应用(总结笔记)

    sudo docker pull ubuntu /*下载Ubuntu最新镜像*/sudo docker pull ubuntu:14.04 /*下载Ubuntu14.04版镜像*/sudo docke ...

  9. docker在Centos上的安装

    Centos6安装docker 系统:centos6.5 内核:3.10.107-1(已升级),docker对RHEL/Centos的最低内核支持是2.6.32-431,epel源的docker版本推 ...

  10. ES6 变量、常量声明总结

    较之前ES5,新颁布在声明上有改变 一.var  对比  let 1.作用域不同 let只在命令所在的代码块 {} 里有效 ES5只有全局作用域和函数作用域,没有块级作用域,带来很多不合理的场景,比如 ...