Spring5+SpringMvc+Hibernate5整合的实现

网友投稿 237 2022-12-04


Spring5+SpringMvc+Hibernate5整合的实现

在进行环境搭建的时候我发现国内的Spring+SpringMvc+Hibernate整合资料比较少,即使有的话要么就是将所有配置放在一个配置文件,不易于理解结构,要么就是版本太旧,因此这篇文章简单讲解了如何配置相关的开发环境,使用的版本为jdk1.8+spring5+hibernate5

1 分层整合

我们都知道在Spring可以通过标签来将不同的配置文件进行整合的,因此我们就用这个思路来进行整合,我们将全部的配置文件分为dao层,service层和view层,这样整合起来比较方便,也比较容易懂

2 创建项目引入依赖

我们首先创建一个名字为Spring5+SpringMvc+Hibernate5的项目,从原型中找到如下的选项

idea给我们创建的项目默认是不完整的

少了一些文件,我们只需要在src上右键,选择创建对应的文件夹即可

有些低版本的idea可能没有这么只能,你只能手动创建并设置资源和源码目录

创建好了之后我们就可以修改一些pom文件中的配置了,以下的文件可以根据自己的情况进行修改

2.1 引入依赖

spring依赖

org.springframework

spring-webmvc

5.2.0.RELEASE

org.springframework

spring-jdbc

5.2.0.RELEASE

org.springframework

spring-orm

5.2.0.RELEASE

org.springframework

spring-test

5.2.0.RELEASE

provided

hibernate依赖

org.hibernate

hibernate-core

5.2.17.Final

org.hibernate

hibernate-validator

5.2.0.Final

数据库和连接池依赖

com.alibaba

druid

1.1.10

mysql

mysql-connector-java

8.0.12

引入jsp

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet.jsp

jsp-api

2.1

provided

javax.servlet

jstl

1.2

jsp其实不是必须,但是你如果需要用到进行不分离的开发,那么该依赖最好还是引入一下

其他的依赖可以根据自己的情况引入

2.2 构建配置修改

在build的时候有可能会出现静态资源没有build的情况,因此可以采用以下方法

src/main/java

**/*.xml

**/*.properties

true

src/main/resources

**/*.xml

**/*.properties

true

.......

3 准备数据库

我们在这里准备一个名字为hibernate的数据库,请自行创建,至于数据库表我们则不需要创建

4 配置dao层

我们首先在resources下准备一个db-mysql.properties文件,内容如下

jdbc.driverClassName=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/hibernate?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8

jdbc.username=root

jdbc.password=123456

请根据自己的数据库版本和配置进行书写,当然这一步不是必须的,但是为了方便找到对应配置项,你可准备这个文件

好了我们可以开始进行spring的配置了,在resources文件夹下创建一个spring-dao,xml,这里的名字可以自行决定,在里面输入以下内容

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

destroy-method="close" lazy-init="false">

update

org.hibernate.dialect.MySQL55Dialect

true

true

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

destroy-method="close" lazy-init="false">

destroy-method="close" lazy-init="false">

update

org.hibernate.dialect.MySQL55Dialect

true

true

请根据自己的情况创建对应的包和选择相应的属性例如数据库方言,我这里是选择了com.fzu.pojo下存放实体类,我创建了一个Student实体类,其中关于hibernate注解的内容我们不赘述

package com.fzu.pojo;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table

public class Student {

@Id

private Long studentid;

private String name;

private Integer age;

// 这里请不要用desc,这个是mysql保留字

private String description;

@Override

public String toString() {

return "Student{" +

"studentid=" + studentid +

", name='" + name + '\'' +

", age=" + age +

", description='" + description + '\'' +

'}';

}

public Long getStudentid() {

return studentid;

}

public void setStudentid(Long studentid) {

this.studentid = studentid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

}

最后在applicationContext.xml中引入该文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

由于这里我们service层的内容比较少我们就不单独创建一个配置文件了

5 配置mvc层

创建spring-mvc.xml文件,并输入以下内容

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd">

其中视图解析器路径和扫描的包可以自行确定,不要忘记在applicationContext.xml中引入

6 配置web.xml

我们需要在web.xml中配置spring容器,视图servlet和编码servlet

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

index.jsp

index.html

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

dispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

15

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

index.jsp

index.html

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

dispatcherServlet

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

15

在创建完相应的类之后,请不要忘记配置tomcat服务器,再启动之后我们就可以看到相应的表创建了

7 总结

我们首先看一下最后的文件结构

可以看到我们的整个配置过程是模块化的,从dao层往上,每一层都可以单独拆装,除此之外我们项进行其他配置也是很简单的,例如如果我们想要新增一个redis配置

我们可以按照如下步骤

引入相关依赖

创建db-redis.properties配置文件

创建spring-redis.xml配置文件并进行配置

在applicationContext中引入即可

可以看到整个过程逻辑很清晰


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java及nginx实现文件权限控制代码实例
下一篇:详解Spring中接口的bean是如何注入的
相关文章

 发表评论

暂时没有评论,来抢沙发吧~