SSH

Deadly 提交于 2019-12-02 22:33:26

介绍:
SSH即struts+spring+hibernate的一个集成框架他们各自是
struts:控制器 spring:调度 hibernate:操作数据库
SSH框架从业务逻辑上分为四层:

Dao层:数据访问层:使用hibernate链接数据库(增删改查)操作
配置:在spring的配置中进行文件配置

Service层:业务逻辑层:负责业务模块的设计逻辑,设计接口,再设计实现的类
配置:在spring的配置中进行配置

Controller层:控制器层:调用Service层的接口来控制业务流程。
配置:在spring的配置中进行配置

View层:表现层:与Controller层紧密联系,主要负责前端jsp页面展示.

开整:是在eclipse上的简单的项目:
1:建一个maven项目:**

 

 

 

 

 

 

 

 


完成以后点Finish
建成功以后他会报错那是没有web.xml文件,步骤如下:
单击选中项目右键如下图:

 

 

 


2.接下来配置一下tomcat:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


3.配置pom.xml文件,数据库是mysql

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zy</groupId>
  <artifactId>ssh</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <!-- 版本信息都统一放入此处,方便以后管理-->
  <properties>
    <springVersion>4.3.1.RELEASE</springVersion>
    <hibernateVersion>4.3.5.Final</hibernateVersion>
    <jstlVersion>1.2</jstlVersion>
    <taglibVersion>1.1.2</taglibVersion>
    <servletVersion>3.0-alpha-1</servletVersion>
    <jsonVersion>1.9.13</jsonVersion>
    <jacksonVersion>2.5.0</jacksonVersion>
    <mysqlVersion>5.1.38</mysqlVersion>
    <c3p0Version>0.9.1.2</c3p0Version>
    <log4jVersion>1.2.17</log4jVersion>
    <fileuploadVersion>1.3.1</fileuploadVersion>
    <lombokVersion>1.16.10</lombokVersion>
  </properties>

  <dependencies>
    <!-- 单元测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- spring-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <!-- spring web + spring MVC-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${springVersion}</version>
    </dependency>

    <!-- hibernate配置-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>${hibernateVersion}</version>
    </dependency>

    <!-- hibernate 缓存, 视情况添加-->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>${hibernateVersion}</version>
    </dependency>

    <!-- jsp页面使用的jstl支持-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstlVersion}</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>${taglibVersion}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servletVersion}</version>
      <scope>provided</scope>
    </dependency>

    <!-- DataBase数据库连接 mysql包-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysqlVersion}</version>
    </dependency>

    <!-- 数据库连接池-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>${c3p0Version}</version>
    </dependency>

    <!-- json数据 使springMVC可以返回json值 ,视情况添加-->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>${jsonVersion}</version>
    </dependency>

    <!-- Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jacksonVersion}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jacksonVersion}</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jacksonVersion}</version>
    </dependency>

    <!-- log4j配置, 视情况添加-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4jVersion}</version>
    </dependency>

    <!--文件 上传-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>${fileuploadVersion}</version>
    </dependency>

    <!-- lombok插件导包-->
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombokVersion}</version>
        <scope>provided</scope>
      </dependency>
 </dependencies>
 </project>  

   

4.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>ssh</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
         <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
              <param-name>contextConfigLocation</param-name>
              <!-- 此处指向的的是SpringMVC的配置文件 -->
              <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <!--配置容器在启动的时候就加载这个servlet并实例化-->
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
        <!-- Spring监听器 -->
        <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>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-hibernate.xml</param-value>
        </context-param>
</web-app>

 
5.springMVC配置

<mvc:annotation-driven />
    <context:component-scan
        base-package="com.zhiyou100" />
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- 前缀 -->
        <property name="prefix" value="view/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!--这里是对静态资源的映射 -->
    <mvc:resources mapping="/js/**" location="/resources/js/" />
    <mvc:resources mapping="/css/**"
        location="/resources/css/" />
    <mvc:resources mapping="/img/**"
        location="/resources/img/" />

</beans>
————————————————
版权声明:本文为CSDN博主「落红无情物」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhang1278851469/article/details/88594164<mvc:annotation-driven />
    <context:component-scan
        base-package="com.zhiyou100" />
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- 前缀 -->
        <property name="prefix" value="view/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!--这里是对静态资源的映射 -->
    <mvc:resources mapping="/js/**" location="/resources/js/" />
    <mvc:resources mapping="/css/**"
        location="/resources/css/" />
    <mvc:resources mapping="/img/**"
        location="/resources/img/" />

</beans>

需要修改一下你自己的包名

 

 

 


6.配置spring-hibernate:文件名称: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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
        
          <!--********************************************配置Spring***************************************-->
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.zy">
            <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>


    <!--********************************************配置hibernate********************************************-->

    <!--扫描配置文件(这里指向的是之前配置的那个config.properties)-->
    <context:property-placeholder location="classpath:/db.properties" />

    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />  <!--数据库连接驱动-->
        <property name="jdbcUrl" value="${jdbc.url}" />     <!--数据库地址-->
        <property name="user" value="${jdbc.username}" />   <!--用户名-->
        <property name="password" value="${jdbc.password}" />   <!--密码-->
        <property name="maxPoolSize" value="40" />      <!--最大连接数-->
        <property name="minPoolSize" value="1" />       <!--最小连接数-->
        <property name="initialPoolSize" value="10" />      <!--初始化连接池内的数据库连接-->
        <property name="maxIdleTime" value="20" />  <!--最大空闲时间-->
    </bean>

    <!--配置session工厂-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.zy.pojo" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <!--hibernate根据实体自动生成数据库表-->
                <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>
    </bean>

    <!-- 事物管理器配置  -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

 


注意:里面需要修改包名,在session工厂,还有在spring自动扫描:

7.创建db.property文件:

#database connection config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql:///ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = 123456

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update

 


注意:需要修改数据库密码,名称

 

 


8.日志配置log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### set log levels - for more verbose logging change 'info' to 'debug' ###
 
log4j.rootLogger=DEBUG, stdout

 


9.开始建包建层

9.1先建View层一个展示页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h2 style="color: #ff261a;">this is my test page!</h2>
</center>
</body>
</html>



9.2 建Controller层

package com.zy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class UserController {

}


9.3 dao层和pojo实体类层
9.3.1 dao层

package com.zy.dao;

public interface UserDao {

}


9.3.2 实体类

package com.zy.pojo;

public class User {

}


9.4 Service层和实现类

9.4.1service层

package com.zy.service;

public interface UserService {

}


9.4.2 实现类,实现UserService

package com.zy.service.imp;

import org.springframework.stereotype.Service;

import com.zy.service.UserService;

@Service
public class UserServiceImp implements UserService{

}

 



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!