Idea + maven 搭建 SSH (struts2 +hibernate5 + spring5) 环境

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

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

<?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: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">

<context:property-placeholder location=“classpath:jdbc.properties”/>

<context:component-scan base-package=“dao.,service.”/>

<context:component-scan base-package=“action”/>

context:annotation-config/

org.hibernate.dialect.MySQLDialect

update

thread

true

true

false

<tx:annotation-driven transaction-manager=“txManager”/>

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 所管理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值