maven ssh mysql_Idea+maven搭建SSH(struts2+hibernate5+spring5)环境的方法步骤

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

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

一、项目环境搭建

使用 maven 搭建一个 Java Web 项目

03554b21b0f0b5cfd168397a8cb27eb8.png

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 配置文件

使用如下方式创建

0c5a90f86806268a6cf11c0c8dbaa176.png

1.applicationContext.xml

2.jdbc.properties

3.struts.xml

2.2 包结构

创建如下的基本包结构

6da763f480e5519a9f08b976d48eb6ba.png

三、编写配置文件

3.1 web.xml 文件配置

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

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 配置文件

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"

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/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">

org.hibernate.dialect.MySQLDialect

update

thread

true

true

false

3.4 struts 配置文件

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

四、使用 hibernate 逆向生成工具生成实体

4.1 配置数据库连接信息

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

26a36c84f4233ab6ea24dcd7143d63dd.png

完善基本配置信息

1972473b6c8c8eee4083871f35ede1b2.png

4.2 逆向生成实体类

5ab35ddd9b9bf3d9490769d7cae0e307.png

6651cc9e850d63dd9aca2e341d933696.png

eb2e16eeb3e398bf2bdac1800d2d5b2d.png

4.3 实体类配置

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

45ef28b05894dd59a2771fb356725d42.png

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

dc64fc7d6e742905305fb0080d9517d3.png

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

46132379a3f2fd636cf277a129abb744.png

然后就没问题了。

五、JavaBean 编写

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

5.1 编写 dao 层

创建 UserDao 以及 它的实现类 UserDaoImpl

c98461f60186fe6c4147791f37e18114.png

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 层,这时候发现出现了爆红的问题,这里我们需要手动添加项目的依赖信息

58bfbb73b12d994984fdac72952528cb.png

点击 project structure

49b7adab2460f203842416272728c5c7.png

0f76fd83f21c293739bcb6b3c059da85.png

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

069e8afd4144baa12094afbeb5884559.png

显示正常了

19ed8cb3b251aaac60fb52646883bf56.png

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 添加如下配置

12deb072f375f050fa0d4a994b5c17dd.png

stucts action 配置

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/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.

--%>

Title

账户:

密码:

3503cdea11350c0c496e1db51c4e199b.png

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.

--%>

Title

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

fc58a3f2496c5a96b635507ade41aa35.png

4e0a304a7a47591acfd66b36d6b8da2b.png

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.

--%>

Title

出错啦!!!

0e12c1372c4d209932db49a65a04002e.png

e16e934cf6c07f3ceb9097dd1938f057.png

到此这篇关于Idea+maven搭建SSH(struts2+hibernate5+spring5)环境的方法步骤的文章就介绍到这了,更多相关Idea+maven搭建SSH内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM是指Spring+SpringMVC+MyBatis的集成开发环境MySQL是一个关系型数据库管理系统,用于存储和管理数据。Maven是一个项目管理和构建工具,可以自动下载所需的类库和插件,并管理项目的依赖关系。Idea是一个Java集成开发环境(IDE),提供了开发、调试和部署Java代码的工具。 在SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境下,我们可以通过Maven构建项目,引入相应的依赖库。Idea提供了可视化的界面,方便我们进行开发和调试工作。 首先,我们可以使用Maven来管理项目的依赖。在pom.xml文件中添加相应的依赖,Maven会自动下载并引入到项目中。 其次,我们可以使用Idea创建Spring项目,并配置相关的配置文件。在Idea的配置界面中,我们可以设置项目的数据库连接信息和配置MyBatis的相关内容。 然后,我们可以使用MyBatis来操作MySQL数据库。在MyBatis的mapper文件中编写SQL语句,并在Spring中配置相应的bean,使其可以与数据库进行交互。 此外,我们还可以使用SpringMVC来开发Web应用。在SpringMVC中,我们可以通过配置相应的请求映射和控制器来处理请求,并返回相应的结果。 最后,通过整合SpringSpringMVC和MyBatis,我们可以实现业务逻辑与数据库的交互,并通过Maven进行项目构建和管理。这样,我们就可以在SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境中进行基于这些框架和工具的开发工作了。 总之,掌握SSM MySQL Maven Idea MyBatis Spring SpringMVC的集成开发环境,意味着我们可以利用这些强大工具和框架来进行Java开发,并能够高效地开发出优质的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值