栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

手把手教你用 maven 搭建 SSH (struts2 +hibernate5 + spring5) 项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手把手教你用 maven 搭建 SSH (struts2 +hibernate5 + spring5) 项目

手把手教你搭建 SSH 环境
一、项目环境搭建
	1.1 配置 Spring 坐标依赖
	1.2 配置 hibernate 坐标依赖
	1.3 配置 struts2 坐标依赖
	1.4 配置Java EE 坐标依赖
	1.5 其他工具
二、项目结构搭建
	2.1 配置文件
	2.2 包结构
三、编写配置文件
	3.1 web.xml 文件配置
	3.2 编写 jdbc.properties 文件
	3.3 编写 applicationContext.xml 配置文件
	3.4 struts 配置文件
四、使用 hibernate 逆向生成工具生成实体
	4.1 配置数据库连接信息
	4.2 逆向生成实体类
	4.3 实体类配置
五、JavaBean 编写
	5.1 编写 dao 层
	5.2 编写 Service 层
	5.3 编写 Controller 层 (UserAction)
	5.4 编写 struts 路由映射
六、前端界面编写
	6.1 登录界面编写
	6.1 登录成功
	6.3 登录失败

最近要使用 SSH 来编写期末的考核任务,之前也在网上查阅了很久,也试出了很多的问题。也很感谢很多前辈们的总结,我也查到了很多用用的内容。

本次项目,我将以一个简单的登录案例实现 SSH 的项目整合,项目我会放到 Github 上面,需要的同学可以 clone 下来在本地跑一跑

项目地址:SSH 脚手架

一、项目环境搭建

使用 maven 搭建一个 Java Web 项目

1.1 配置 Spring 坐标依赖

引入 Spring 坐标依赖

 
    
      org.springframework
      spring-context
      5.1.0.RELEASE
    
    
    
      org.springframework
      spring-web
      5.1.0.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      5.1.0.RELEASE
    
    
    
      org.springframework
      spring-orm
      5.1.0.RELEASE
    
    
    
    
      org.aspectj
      aspectjrt
      1.9.1
    
    
    
      org.aspectj
      aspectjweaver
      1.9.1
    
1.2 配置 hibernate 坐标依赖

我们的目标是要整合 SSH,所以需要 hibernate 的核心依赖, mysql 数据库驱动,以及 c3p0 数据库连接池

   
    
      org.hibernate
      hibernate-core
      5.2.17.Final
    
    
    
      mysql
      mysql-connector-java
      5.1.47
    
 
    
      com.mchange
      c3p0
      0.9.5.2
    
1.3 配置 struts2 坐标依赖

我们需要 struts 核心,以及 struts 整合 spring 的插件,以及 struts 对 json 数据处理的插件

    
    
      org.apache.struts
      struts2-core
      2.3.35
    
    
    
      org.apache.struts
      struts2-spring-plugin
      2.3.35
    
    
    
      org.apache.struts
      struts2-json-plugin
      2.3.8
    
1.4 配置Java EE 坐标依赖

这里可以引入 servlet api,jstl 标签库等一系列工具


    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.1
      provided
    
    
    
      org.projectlombok
      lombok
      1.18.0
      provided
    
    
    
      jstl
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
1.5 其他工具

json 处理工具

      
   org.jetbrains
   annotations-java5
   RELEASE
   compile
      
    
    
      org.json
      json
      20160810
    
二、项目结构搭建 2.1 配置文件

使用如下方式创建

  1. applicationContext.xml
  2. jdbc.properties
  3. struts.xml
2.2 包结构

创建如下的基本包结构

三、编写配置文件 3.1 web.xml 文件配置



  Archetype Created Web Application

  
    contextConfigLocation
    
    classpath:applicationContext.xml
  
  
  
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
    struts2
    /*
  
  
  
    org.springframework.web.context.ContextLoaderListener
  

3.2 编写 jdbc.properties 文件

这里我们需要自己手动修改数据库的信息配置

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&autoReconnect=true&useSSL=false
jdbc.user=root
jdbc.password=root
#连接池中保留的最小连接数
jdbc.minPoolSize=1
#连接池中保留的最大连接数
jdbc.maxPoolSize=20
#初始化连接数
jdbc.initialPoolSize=1
3.3 编写 applicationContext.xml 配置文件

这里面也包含了数据库的基本配置



    
    
    
    
    
    
    
    
    
    
 
 
 
 
 
 
 
    
    
 
 
     
  
  
      org.hibernate.dialect.MySQLDialect
  
  update
  thread
  
  true
  true
  false
     
 
 
 
    
    

    
 
 
    

    
    
 
    
    

3.4 struts 配置文件

我们还没有编写的具体的 action 服务,所以这里先跳过

四、使用 hibernate 逆向生成工具生成实体 4.1 配置数据库连接信息

使用 idea 自带的数据库连接的工具

完善基本配置信息

4.2 逆向生成实体类



4.3 实体类配置

生成好后可以看到和数据库对应的实体类,我的表很简单,一个简单的用户表,只有 id, username, password 字段

但是我们发现里面的部分内容会爆红,这是因为我们没有指定数据源

选择我们刚才连接的数据库

然后就没问题了。

五、JavaBean 编写

看到包结构,大家应该可以猜出来,我是使用的典型的 MVC 三层架构来编写的

5.1 编写 dao 层

创建 UserDao 以及 它的实现类 UserDaoImpl

UserDao 编写

package dao;

import entity.User;

public interface UserDao {

    // 用户登录验证
    public User selectByUsernameAndPassword(String username, String password);
}

UserDaoImpl

package dao.Impl;

import dao.UserDao;
import entity.User;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

// 使用 Spring 来接管持久层的所有操作
@Repository
public class UserDaoImpl implements UserDao {

    // 使用 Hibernate 提供的模板
    @Autowired
    @Resource
    private HibernateTemplate hibernateTemplate;

    // 生成对应的 get 和 set 方法
    public HibernateTemplate getHibernateTemplate() {
 return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
 this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
 // 登录的逻辑不算难,就是使用 sql 语句查询,username 和 password 两个字段是否存在即可,我们使用的是 hibernate 框架,所以要写 hql 语句
 Session session = hibernateTemplate.getSessionFactory().openSession();
 Query q = session.createQuery("from User u where u.username = ? and u.password = ?");
 q.setParameter(0,username);
 q.setParameter(1,password);
 User u = (User) q.uniqueResult();
 return u;
    }
}

我们写好了 dao 层,这时候发现出现了爆红的问题,这里我们需要手动添加项目的依赖信息

点击 project structure


添加这个就可以了,问题就解决了

显示正常了

5.2 编写 Service 层

同样,我们创建对应的 UserService 和 对应的 UserServiceImpl

有的同学可能会问道,不就是一个简单的登录功能嘛,有必要这么麻烦吗?是的,这么做确实没必要,但是随着项目的越来越大,只有把具体的功能全部分开来做,这样才不至于整个项目太过于乱

编写用户的业务层 接口 UserService

package service;

import entity.User;

public interface UserService {
    // 登录验证
    User checklogin(String username, String password);
}

编写 业务层对应的实现类 UserServiceImpl

package service.Impl;

import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.UserService;

@Service
public class UserServiceImpl implements UserService {
    
    // 这里业务层调用持久层的方法
    @Autowired
    private UserDao ud;
    
    @Override
    public User checklogin(String username, String password) {
 return ud.selectByUsernameAndPassword(username,password);
    }
}
5.3 编写 Controller 层 (UserAction)

这里的逻辑思路,是 controller 层 调用 service 的方法,service 层调用 dao 层的方法

package action;

import com.opensymphony.xwork2.ActionContext;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import service.UserService;

import java.util.Map;

// 使用 Controller 表示这是控制层,使用 ua 表示这个类被 Spring 所管理
@Controller("ua")
public class UserAction {
    // 编写两个属性,使用 struts2 的 ognl 表达式可以直接接收到前端穿过来的数据,不再需要 request.getParameter("xxxx") 接收数据了
    private String username;
    private String password;

    // 调用业务层的方法
    @Autowired
    private UserService us;

    // get 方法可以不要, set 方法必须有,不然前端的数据就无法注入进来
    public String getUsername() {
 return username;
    }

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

    public String getPassword() {
 return password;
    }

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

    // 编写登录逇控制层方法
    public String login() {
 System.out.println(username + " " + password); // 打印穿过来的数据
 ActionContext ac = ActionContext.getContext();
 // 得到 servlet 中的三大域的 session 域,在这里我们要将数据保存至 session,并在前端展示
 Map session = ac.getSession(); // 我们可以看到 session 的实质就是一个 map
 User user = us.checklogin(username,password); // 登录验证
 if ( user!=null ) {
     session.put("user",username);
     return "success";
 } else {
     return "error";
 }
    }
}
5.4 编写 struts 路由映射

记得在 Project Structure 添加如下配置

stucts action 配置







    
 
 	
     /index.jsp
     
     /error.jsp
 
    

六、前端界面编写 6.1 登录界面编写
<%--
  Created by IntelliJ IDEA.
  User: Gorit
  Date: 2020/6/13
  Time: 23:18
  Contact: gorit@qq.com
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    


6.1 登录成功
<%--
  Created by IntelliJ IDEA.
  User: Gorit
  Date: 2020/6/13
  Time: 23:21
  Contact: gorit@qq.com
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>


    Title


    

欢迎你 ${sessionScope.user} 登录!!


6.3 登录失败
<%--
  Created by IntelliJ IDEA.
  User: Gorit
  Date: 2020/6/13
  Time: 23:21
  Contact: gorit@qq.com
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    

出错啦!!!


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号