【ssh系列一】——框架搭建

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 建立项目(SpringMVC+Spring+hibernate的maven项目)

一、建立项目(SpringMVC+Spring+hibernate的maven项目)


20170807112633504.png

20170807112657858.png

20170807112755870.png

20170807112724429.png


20170807112738116.png


二、创建结构


20170807112832292.png

20170807112852964.png


三、添加pom文件内容


 

-
<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>maven</groupId>
    <artifactId>maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name/>
    <description/>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--<hibernate.version>5.2.10.Final</hibernate.version>-->
        <hibernate.version>4.2.5.Final</hibernate.version>
        <spring.version>4.3.10.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <!-- aspectjweaver.jar这是Spring AOP所要用到的包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.1</version>
        </dependency>
        <!-- hibernate4 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <!-- spring mvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- spring3 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <exclusions></exclusions>
        </dependency>
        <!--jsp页面使用的jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
-


四、添加相关配置文件内容


   1.web.xml


-
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         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_2_5.xsd">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring hibernate -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-hibernate.xml,classpath:spring.xml</param-value>
    </context-param>
    <!-- openSessionInView配置 -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--spring mvc 配置 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- encodeing -->
    <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>
    <!-- encoding filter for jsp page -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
-


   2.spring-mvc.xml


-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 注解扫描包 -->
    <!--影响返回之后的页面跳转-->
    <context:component-scan base-package="com.ssh.controller" />
    <!--影响页面的启动,dao和service都得添加,为什么?
    是因为Controller通过@Autowired注入了service,然后service通过@Autowired注入了Dao
    所以在编译加载的时候会通过注解进行寻找相关内容,所以需要将dao和service的路径都写上
    (已经测试过了,将service中通过@Autowired注入dao的注释掉,将下面的dao路径删除,编译不会出错,
    但是调用相关方法就会报找不到dao的类的错误)-->
    <!--<context:component-scan base-package="com.ssh.dao,com.ssh.service" />-->
    <!-- 开启mvc注解 -->
    <mvc:annotation-driven />
    <!--自动装配DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    <mvc:default-servlet-handler />
    <!--页面相关的控制内容-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="suffix" value=".jsp" />            <!-- 视图文件类型 -->
        <property name="prefix" value="/WEB-INF/jsp" />  <!-- 视图文件的文件夹路径 -->
    </bean>
</beans>
-


   3.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
    <!-- 自动扫描dao和service包(自动注入) -->
    <context:component-scan base-package="com.ssh.dao,com.ssh.service" />
</beans>
-


   4.spring-hibernate.xml


-
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:config.properties" />
    <!-- 第二种形式加载properties配置文件 -->
    <!--<bean id="propertyConfigurer"-->
          <!--class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
        <!--<property name="locations">-->
            <!--<list>-->
                <!--<value>classpath:config.properties</value>-->
            <!--</list>-->
        <!--</property>-->
    <!--</bean>-->
    <!-- 配置数据源 -->
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
        <property name="driverClassName" value="${driverClassName}"/>
    </bean>
    <!-- 配置hibernate 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>
                <prop key="current_session_context_class">thread</prop>
            </props>
        </property>
        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <property name="packagesToScan">
            <list>
                <!-- 此处与entity实体路径对应 -->
                <value>com.ssh.entity</value>
            </list>
        </property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 注解方式配置事物 -->
    <tx:annotation-driven  transaction-manager="transactionManager" />
</beans>
-


   5.config.properties


-
#\u5C5E\u6027\u6587\u4EF6
#Oracle \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=
#jdbc_password=
#SQLServer \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
#jdbc_username=
#jdbc_password=
#Derby \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.DerbyDialect
#driverClassName=org.apache.derby.jdbc.EmbeddedDriver
#validationQuery=SELECT 1
#jdbc_url=jdbc:derby:sy;create=true
#jdbc_username=
#jdbc_password=
#MySQL \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate_dialect=org.hibernate.dialect.MySQLDialect
#hibernate_dialect=org.hibernate.dialect.MySQLMyISAMDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/sshtext?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=root
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
sessionInfoName=sessionInfo
uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached
-


五、添加相关类


   1.entity


-
package com.ssh.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
 * Created by mk on 2017/8/5.
 */
@Entity(name="t_user")
public class UserEntity {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String password;
    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;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
-


   2.Dao


-
package com.ssh.dao;
import com.ssh.entity.UserEntity;
/**
 * Created by mk on 2017/8/5.
 */
public interface UserDao {
    public int insert(UserEntity userEntity);
}
-


   3.DaoImpl


-
package com.ssh.dao.impl;
import com.ssh.dao.UserDao;
import com.ssh.entity.UserEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
 * Created by mk on 2017/8/5.
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    SessionFactory sessionFactory;
    public int insert(UserEntity userEntity) {
        Session session = sessionFactory.getCurrentSession();
        return (Integer)session.save(userEntity);
    }
}
-


   4.Service


-
package com.ssh.service;
import com.ssh.entity.UserEntity;
/**
 * Created by mk on 2017/8/5.
 */
public interface UserService {
    public int insert(UserEntity userEntity);
}
-


   5.ServiceImpl


-
package com.ssh.service.impl;
import com.ssh.dao.UserDao;
import com.ssh.entity.UserEntity;
import com.ssh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * Created by mk on 2017/8/5.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    public int insert(UserEntity userEntity) {
        return userDao.insert(userEntity);
    }
}
-


   6.Controller


-
package com.ssh.controller;
import com.ssh.entity.UserEntity;
import com.ssh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Created by mk on 2017/8/5.
 */
@RequestMapping("/user1")
@Controller
public class userController {
    @Autowired
    UserService userService;
    @RequestMapping("/testindex")
    public String inset(Model model){
        UserEntity userEntity = new UserEntity();
        userEntity.setName("1");
        userEntity.setPassword("123");
        int result = userService.insert(userEntity);
        model.addAttribute("result",result);
        return "/test";
    }
}
-


外.text.jsp


-
<%--
  Created by IntelliJ IDEA.
  User: mk
  Date: 2017/8/5
  Time: 17:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${result}<br>
</body>
</html>
-


六、建立数据库相关内容


   1.建立数据库


20170807113617679.png


   2.建立表(和entity中的字段类型对应)


-
CREATE TABLE `t_user` (
  `id` int(100) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
-


七、配置tomcat


20170807114236990.png


详细内容请参考:单击连接


八、运行测试


20170807114527413.png


九、总结


   对于ssh有了一个整体的搭建框架的认识,接下来就是对每一个配置文件的剖析过程!

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
Java 关系型数据库 MySQL
JSP SSH公车拍卖系统myeclipse开发mysql数据库bs框架java编程网结构
JSP SSH公车拍卖系统是一套完善的web设计系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,开发环境为TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql5.0,使用java语言开发,系统主要采用B/S模式开发。
122 0
|
7月前
|
Java 数据库连接 数据库
让星星⭐月亮告诉你,SSH框架01、Spring概述
Spring是一个轻量级的Java开发框架,旨在简化企业级应用开发。它通过IoC(控制反转)和DI(依赖注入)降低组件间的耦合度,支持AOP(面向切面编程),简化事务管理和数据库操作,并能与多种第三方框架无缝集成,提供灵活的Web层支持,是开发高性能应用的理想选择。
80 1
|
11月前
|
网络安全 数据安全/隐私保护
分布式系统详解--框架(Hadoop-Ssh免密登陆配置)
分布式系统详解--框架(Hadoop-Ssh免密登陆配置)
112 0
|
12月前
|
Java 数据库连接 网络安全
SSH 组合框架模式小知识分享
【5月更文挑战第4天】SSH 组合框架模式小知识分享
78 0
|
前端开发 Java 网络安全
基于SSH框架甜品商城管理系统【源码+数据库】
基于SSH框架甜品商城管理系统【源码+数据库】
106 0
|
前端开发 Java 关系型数据库
JSP仓库进销存系统ssh框架mysql数据库myeclipse开发mvc结构java
JSP 仓库进销存系统(struts2+hibernate) 是一套完善的web设计系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。
86 0
|
Java 网络安全 Spring
SSH框架学习中遇到的问题
SSH框架学习中遇到的问题
75 0
|
设计模式 开发框架 缓存
SSH框架简介篇
SSH框架简介篇
534 0
|
SQL 缓存 Java
《SSH框架》---Hibernate
《SSH框架》---Hibernate
129 0
|
Java 数据库连接 网络安全
《SSH框架》---spring
《SSH框架》---spring
112 0