Spring + Spring MVC + Hibernate 项目搭建和登录注册例子

开发工具:IntelliJ IDEA
数据库:MySql

目录

Spring + Spring MVC + Mybatis 项目搭建和登录注册例子

Spring Boot + Mybatis 项目搭建和登录注册例子

Spring + Spring MVC + Hibernate 项目搭建和登录注册例子

Spring Boot + Hibernate 项目搭建和登录注册例子

Spring Boot 使用 JWT

本篇将记录创建一个 Spring + Spring MVC + Hibernate 项目,并且编写一个注册登录的例子,毕竟Java的东西配置起来真有点麻烦

Struts2 和 SpringMVC 对比

既然是 SSH (Spring + Spring MVC + Hibernate)框架,自然也要提一下另一个 SSH (Struts2 + Spring + Hibernate)
这些是我复制粘贴来的

一、框架机制
spring mvc 和 struts2的加载机制不同:spring mvc的入口是servlet,而struts2是filter(servlet和filter区别见本文最后)
1、Struts2采用Filter(StrutsPrepareAndExecuteFilter)实现,SpringMVC(DispatcherServlet)则采用Servlet实现。
2、Filter在容器启动之后即初始化;服务停止以后坠毁,晚于Servlet。Servlet在是在调用时初始化,先于Filter调用,服务停止后销毁。

二、拦截机制

  1. Struts2
    a. Struts2框架是类级别的拦截,每次请求就会创建一个Action,和Spring整合时Struts2的ActionBean注入作用域是原型模式prototype(否则会出现线程并发问题),然后通过setter,getter吧request数据注入到属性。
    b. Struts2中,一个Action对应一个request,response上下文,在接收参数时,可以通过属性接收,这说明属性参数是让多个方法共享的。
    c. Struts2中Action的一个方法可以对应一个url,而其类属性却被所有方法共享,这也就无法用注解或其他方式标识其所属方法了
  1. SpringMVC
    a. SpringMVC是方法级别的拦截,一个方法对应一个Request上下文,所以方法直接基本上是独立的,独享request,response数据。而每个方法同时又何一个url对应,参数的传递是直接注入到方法中的,是方法所独有的。处理结果通过ModeMap返回给框架。
    b. 在Spring整合时,SpringMVC的Controller Bean默认单例模式Singleton,所以默认对所有的请求,只会创建一个Controller,有应为没有共享的属性,所以是线程安全的,如果要改变默认的作用域,需要添加@Scope注解修改。

三、性能方面
pringMVC实现了零配置,由于SpringMVC基于方法的拦截,有加载一次单例模式bean注入。而Struts2是类级别的拦截,每次请求对应实例一个新的Action,需要加载所有的属性值注入,所以,SpringMVC开发效率和性能高于Struts2。

四、拦截机制
Struts2有自己的拦截Interceptor机制,SpringMVC这是用的是独立的Aop方式,这样导致Struts2的配置文件量还是比SpringMVC大。

五、配置方面
spring MVC和Spring是无缝的。从这个项目的管理和安全上也比Struts2高(当然Struts2也可以通过不同的目录结构和相关配置做到SpringMVC一样的效果,但是需要xml配置的地方不少)。
SpringMVC可以认为已经100%零配置。

六、设计思想
Struts2更加符合OOP的编程思想, SpringMVC就比较谨慎,在servlet上扩展。

七、集成方面
SpringMVC集成了Ajax,使用非常方便,只需一个注解@ResponseBody就可以实现,然后直接返回响应文本即可,而Struts2拦截器集成了Ajax,在Action中处理时一般必须安装插件或者自己写代码集成进去,使用起来也相对不方便。

创建数据库

就随便一点吧,使用MySql,数据库就用 test ,新建一个 users 表,随便设置几个字段

注意

  1. 要按照命名规范,单词均小写,不同单词之间使用下划线隔开
  2. Hibernate 貌似不推荐使用外键,需要我们在代码中手动控制实体之间的关系
    其实是我在使用 Hibernate 和 Mybatis 的时候,用了很多时间都没解决外键的问题,写配置也觉得挺麻烦的
    感觉有时候不如自己封装JDBC,只能感叹 Entity Framework Core 是真的好用(貌似 Mybatis 的外键和 EF Core蛮像的),大概是我还太菜了吧,等我有时间解决外键的问题再写另一篇记录吧 T_T

IDEA 添加数据库

输入用户名和密码,填写连接字符串,然后点击 Test Connection,Schema 可以选择连接的数据库
连接字符串参考:jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false

创建项目

使用 Maven 创建 Java Web 项目,选择 maven-archetype-webapp

GroupId:一般是公司域名
ArtifactId:项目名称
毕竟只是个例子,所以就随便写了

配置 maven 路径,这里就不详细展开讲了,Maven 的配置 baidu bing 之类的搜索引擎随便查都有答案

项目路径的最后文件夹与项目名称一致

创建完项目,右键 src

修改 pom.xml 文件,因为配置过于麻烦,所以我直接把做好的扔出来了,还需要什么就自己添加吧

<?xml version="1.0" encoding="UTF-8"?>

<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.MyStudio</groupId>
  <artifactId>Test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Test Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.Version>4.3.18.RELEASE</spring.Version>
  </properties>

  <dependencies>
    <!-- 数据库连接池-->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.18</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.3.8.Final</version>
<!--      <version>5.4.25.Final</version>-->
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.Version}</version>
    </dependency>
    <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-orm</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-webmvc</artifactId>
      <version>${spring.Version}</version>
    </dependency>
    <!-- aop 依赖 -->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.20.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.2.0</version>
    </dependency>
    <!-- aspectjrt 依赖 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.14</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>Test</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

配置本地 tomcat

选择本地 tomcat 路径,到 tomcat 文件夹即可

配置部署文件,这两个选哪个都行,然后应用和确定

在 webapp 目录下创建几个文件夹

  • Controllers:控制器
  • DAO:数据库访问
  • Filters:过滤器
  • Models:实体模型类
  • Services:服务层
  • Utils:工具类
  • Views:视图

配置 Hibernate

选择项目结构

选择 Modules ,添加 hibernate ,应用确定

左侧选择 Persistence 》 项目 》 右键 》 Generate Persistence Mapping 》 By Database Schema

选择数据库连接,再选择需要生成的实体类,这里我稍微更改了类名,因为数据库的表是 users ,而实体类我想叫 User ,前缀后缀看个人喜好吧,hibernate 配置文件可以自己新建一个,我的配置文件在 main 目录下,然后 Ok 就会生成实体类和相应的配置文件

UserEntity 生成的实体类

package main.webapp.Models;

import javax.persistence.*;

@Entity
@Table(name = "users", schema = "test")
public class UserEntity
{
    private int id;
    private String username;
    private String password;

    @Id
    @Column(name = "id", nullable = false)
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    @Basic
    @Column(name = "username", nullable = false, length = 50)
    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    @Basic
    @Column(name = "password", nullable = false, length = 20)
    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        UserEntity that = (UserEntity) o;

        if (id != that.id)
        {
            return false;
        }
        if (username != null ? !username.equals(that.username) : that.username != null)
        {
            return false;
        }
        if (password != null ? !password.equals(that.password) : that.password != null)
        {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = id;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }
}

UserEntity.hbm.xml 生成的实体类配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="main.webapp.Models.UserEntity" table="users" schema="test">
        <id name="id">
            <column name="id" sql-type="int"/>
        </id>
        <property name="username">
            <column name="username" sql-type="varchar(50)" length="50"/>
        </property>
        <property name="password">
            <column name="password" sql-type="varchar(20)" length="20"/>
        </property>
    </class>
</hibernate-mapping>

配置文件(都在 src 目录下)

在 src 目录下新建 jdbc.properties

jdbc.username=root
jdbc.password=123456
jdbc.driverClassName=com.mysql.jdbc.Driver
# 数据库连接字符串可能要改
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false

在 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.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.xsd">

    <!-- 引入外部属性文件=============================== -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
    </bean>

    <!-- 扫描main.webapp.DAO包下所有标注@Repository的DAO组件 -->
    <context:component-scan base-package="main.webapp.DAO"/>

    <!--服务层配置 -->
    <!-- 扫描main.webapp.Services包下所有标注@Service的服务组件 -->
    <context:component-scan base-package="main.webapp.Services"/>

    <!--在扫描时排除Controller-->
    <context:component-scan base-package="main.webapp.Controllers">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan">
            <list>
                <value>main.webapp.Models</value>
            </list>
        </property>

        <!-- 配置Hibernate的相关属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.autoReconnect">true</prop>
            </props>
        </property>

        <!-- 引入映射文件 -->
<!--        hibernate实体类的配置文件在这编写-->
        <property name="mappingResources">
            <list>
                <value>main/webapp/Models/UserEntity.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!--定义HibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--  配置事务传播特性 -->
<!--    这里的意思是:以这些单词开头的方法-->
<!--    命名看个人的习惯,我习惯用帕斯卡命名规范-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="Create*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Save*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Reg*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Update*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Delete*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Find*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Get*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="Load*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* main.webapp.DAO.*.*(..)))" order="1"/>
    </aop:config>

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

    <bean id="requestMappingHandlerAdapter"
          class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

</beans>

再创建一个 springMVC.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:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <mvc:annotation-driven/>
    <!--扫描controller层在spring中不在扫描-->
    <context:component-scan base-package="main.webapp.Controllers"/>
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/Views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--&lt;!&ndash;配置拦截器&ndash;&gt;-->
    <!--<mvc:interceptors>-->
    <!--<mvc:interceptor>-->
    <!--&lt;!&ndash;配置拦截所有请求&ndash;&gt;-->
    <!--<mvc:mapping path="/**"/>-->
    <!--&lt;!&ndash;配置拦截器的位置&ndash;&gt;-->
    <!--<bean class="这里写拦截器的位置"></bean>-->
    <!--</mvc:interceptor>-->
    <!--</mvc:interceptors>-->
<!--    <mvc:resources location="/wwwroot/css/" mapping="/wwwroot/css/**" />-->
</beans>

添加 Tomcat 包,在项目结构的 Modules 里,其实这里我偷懒了,可以选择在 Maven 中添加包,servlet 和 jsp,但是 tomcat 里有现成的就直接用吧

没有这个选项就手动去 tomcat 目录下的 lib文件夹找

最后查看项目结构 》 Modules 》 Spring

首页 jsp

配置 web.xml 文件,里面有几个关于路径的选项,可以修改

<web-app 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_3_0.xsd"
         version="3.0">
  <welcome-file-list>
    <welcome-file>/Views/Home/Index.jsp</welcome-file>
  </welcome-file-list>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--     编码过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- static resources -->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.jpeg</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.ico</url-pattern>
    <url-pattern>*.zip</url-pattern>
    <url-pattern>*.rar</url-pattern>
  </servlet-mapping>

</web-app>

Index.jsp 首页,开头的一段代码是获取端口之类的,主要是用于访问静态文件用的,可以自己去搜索相关内容,这里不讲
其实我也偷懒了,这些代码其实应该写在过滤器里,毕竟只是个例子,图个方便咯 ( ̄_, ̄ )

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String path = basePath + request.getContextPath();
    String filePath = path + "/wwwroot";
    session.setAttribute("basePath",basePath);
    session.setAttribute("path",path);
    session.setAttribute("filePath",filePath);
%>
<html>
<head>
    <link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
    <title>Home Index</title>
</head>
<body>
<h1>
    This is My Home page
</h1>
</body>
</html>

运行,还蛮成功的

DAO 层

先定义一个 BaseDAO ,之后的DAO去继承BaseDAO,这样方便一些
如果想在 SSH 中使用原生SQL语句需要在DAO中定义SessionFactory私有字段,使用SQLQuery query = this.getSession().createSQLQuery(sql);创建查询,再使用query去执行,这里我就偷懒用 HQL 了

public class BaseDAO<T>
{
    private Class<T> _entityClass;
    private HibernateTemplate _hibernateTemplate;

    public BaseDAO()
    {
        //获取T的类型
        Type generType=this.getClass().getGenericSuperclass();
        Type[] params=((ParameterizedType)generType).getActualTypeArguments();
        this._entityClass=(Class)params[0];
    }

    public HibernateTemplate get_hibernateTemplate()
    {
        return _hibernateTemplate;
    }

    @Autowired
    public void set_hibernateTemplate(HibernateTemplate _hibernateTemplate)
    {
        this._hibernateTemplate = _hibernateTemplate;
    }

    public T Load(Serializable id)
    {
        return (T)this.get_hibernateTemplate().load(this._entityClass,id);
    }

    public List<T> LoadAll()
    {
        return this.get_hibernateTemplate().loadAll(this._entityClass);
    }

    public T Get(Serializable id)
    {
        return (T)this.get_hibernateTemplate().get(this._entityClass,id);
    }

    public void Save(T entity)
    {
        this.get_hibernateTemplate().save(entity);
    }

    public void Delete(T entity)
    {
        this.get_hibernateTemplate().delete(entity);
    }

    public void Update(T entity)
    {
        this.get_hibernateTemplate().update(entity);
    }

    public List Find(String hql)
    {
        return this.get_hibernateTemplate().find(hql);
    }

    public List Find(String hql,Object... params)
    {
        return this.get_hibernateTemplate().find(hql, params);
    }
}

UserDAO

@Repository
public class UserDAO extends BaseDAO<UserEntity>
{
    private static final String GET_USER_BY_USERNAME = "from UserEntity u where u.username = ?";
    private static final String GET_USER_BY_USERNAME_AND_PASSWORD = "from UserEntity u where u.username = ? and u.password = ?";

    public UserEntity GetUserByUsernameAndPassword(String username, String password)
    {
        List<UserEntity> userList = (List<UserEntity>) this.get_hibernateTemplate().find(UserDAO.GET_USER_BY_USERNAME_AND_PASSWORD, username, password);
        if (userList.size() == 0)
        {
            return null;
        }
        else
        {
            return userList.get(0);
        }
    }

    public UserEntity GetUserByUsername(String username)
    {
        List<UserEntity> userList = (List<UserEntity>) this.get_hibernateTemplate().find(UserDAO.GET_USER_BY_USERNAME, username);
        if (userList.size() == 0)
        {
            return null;
        }
        else
        {
            return userList.get(0);
        }
    }

}

Service 层

UserService

@Service
public class UserService
{
    private UserDAO _userDAO;

    @Autowired
    public void SetUserDAO(UserDAO userDAO)
    {
        this._userDAO = userDAO;
    }

    public void Save(UserEntity userEntity)
    {
        this._userDAO.Save(userEntity);
    }

    public UserEntity GetUserByUsernameAndPassword(String username, String password)
    {
        return this._userDAO.GetUserByUsernameAndPassword(username, password);
    }

    public UserEntity GetUserByUsername(String username)
    {
        return this._userDAO.GetUserByUsername(username);
    }

}

Controller 层

UserController,控制器里,我返回值使用ModelAndView是偷懒,因为我懒得写前端代码,实际上返回值可以自己定义

@Controller
@RequestMapping("/UserController")
public class UserController
{
    private UserService _userService;

    @Autowired
    public void SetUserService(UserService userService)
    {
        this._userService = userService;
    }

    @RequestMapping("/Index")
    public ModelAndView Index()
    {
        ModelAndView view = new ModelAndView();
        view.setViewName("User/Index");

        return view;
    }

    @RequestMapping(value = "/Login/DoLogin", method = RequestMethod.POST)
    public ModelAndView DoLogin(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        mav.addObject("username", username);
        mav.addObject("password", password);

        if (request.getParameter("btn").equals("Register"))//确定点击的按钮
        {
            mav.setViewName("User/Register");
        }
        else if (request.getParameter("btn").equals("Login"))
        {
            if (username.equals("") || password.equals(""))
            {
                mav.addObject("LoginMessage", "用户名或密码不能为空");
                mav.setViewName("User/Index");
            }
            else
            {
                UserEntity userEntity = this._userService.GetUserByUsernameAndPassword(username, password);
                if (userEntity != null)
                {
                    mav.setViewName("User/Success");
                }
                else
                {
                    mav.addObject("LoginMessage", "用户名或密码错误");
                    mav.setViewName("User/Index");
                }
            }
        }

        return mav;
    }

    @RequestMapping(value = "/Register/DoRegister", method = RequestMethod.POST)
    public ModelAndView DoRegister(HttpServletRequest request)
    {
        ModelAndView mav = new ModelAndView();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repeatPassword = request.getParameter("repeatPassword");
        mav.addObject("username", username);
        mav.addObject("password", password);
        mav.addObject("repeatPassword", repeatPassword);

        if (username.equals("") || password.equals("") || repeatPassword.equals(""))
        {
            mav.addObject("RegisterMessage", "输入框内容不能为空");
            mav.setViewName("User/Register");
        }
        else if (password.equals(repeatPassword) == false)
        {
            mav.addObject("RegisterMessage", "密码不一致");
            mav.setViewName("User/Register");
        }
        else
        {
            UserEntity userEntity = new UserEntity();
            if (this._userService.GetUserByUsername(username) == null)
            {
                userEntity.setUsername(username);
                userEntity.setPassword(password);
                this._userService.Save(userEntity);
                mav.setViewName("User/Index");
            }
            else
            {
                mav.addObject("RegisterMessage", "用户名已存在");
                mav.setViewName("User/Register");
            }
        }

        return mav;
    }

}

JSP 页面

Index.jsp ,登录页面
requestScope可以将控制器mav.addObject()的值输出

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String context = request.getContextPath();
    request.setAttribute("context", context);
%>
<html>
<head>
    <link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
    <title>User Index</title>
</head>
<body>
<h1>
    This is Login Index Page
</h1>
<form action="${context}/UserController/Login/DoLogin" method="post">
    <label>账户:
        <input type="text" name="username" value="${requestScope.username}">
    </label>
    <br>
    <label>密码:
        <input type="password" name="password" value="${requestScope.password}">
    </label>
    <br>
    <label style="color: red">
        ${requestScope.LoginMessage}
    </label>
    <br>
    <button type="submit" name="btn" value="Login">登录</button>
    <button type="submit" name="btn" value="Register">注册</button>
    <a href="${pageContext.session.getAttribute("path")}/Views/User/Register.jsp" style="color: red">注册</a>
</form>
</body>
</html>

Register.jsp ,注册页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String context = request.getContextPath();
    request.setAttribute("context", context);
%>
<html>
<head>
    <link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
    <title>Register</title>
</head>
<body>
<h1>
    This is Register Page
</h1>
<form action="${context}/UserController/Register/DoRegister" method="post">
    <label>用户名:</label>
    <input type="text" name="username" value="${requestScope.username}">
    <br>
    <label>密码</label>
    <input type="password" name="password" value="${requestScope.password}">
    <br>
    <label>重复密码:</label>
    <input type="password" name="repeatPassword" value="${requestScope.repeatPassword}">
    <br>
    <label style="color: red">
        ${requestScope.RegisterMessage}
    </label>
    <br>
    <button type="submit">注册</button>
</form>
</body>
</html>

Success.jsp ,登录成功页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <link href="${pageContext.session.getAttribute("filePath")}/css/MyStyleSheet.css" type="text/css" rel="stylesheet">
    <title>Login Success</title>
</head>
<body>
<h1>
    Login Success
</h1>
</body>
</html>

因为控制器的返回值是视图,所以要这样访问,根据控制器的标注修改 url ,如果只是 html + js 的话,可以直接 Views/ 访问视图

完整项目结构

Spring + Spring MVC + Hibernate 项目搭建和登录注册例子 结束

posted @ 2021-02-24 16:25  .NET好耶  阅读(814)  评论(0编辑  收藏  举报