基于spring+springmvc+hibernate 整合深入剖析

网友投稿 255 2022-12-03

基于spring+springmvc+hibernate 整合深入剖析

目录1.新建一个maven web项目2.pom文件,导入jar包3.配置文件4.spring-mvc和spring整合5.spring和hibernate整合6.总结

三大框架反反复复搭了很多次,虽然每次都能搭起来,但是效率不高。最近重新搭了一次,理顺了思路,整理了需要注意的地方,分享出来。

工具:Eclipse(jdk 1.7) spring和hibernate版本均为4.0以上

推荐使用maven构建项目,相比自己手动导入jar包要方便很多。

1.新建一个maven web项目

(没用过maven的可以i先去了解一下)

注意点:使用Eclipse创建的maven工程文件结构可能不太正确,需要自己手动创建文件夹。

正确的文件结构:

-src

---main

------java(class文件)

------resources(配置文件)

------webapp(web文件)

---test(测试文件)

------java

------resources

2.pom文件,导入jar包

导入jar包也是框架整合中比较麻烦的点,常常会导入太多不相关的包,但由于不熟悉又不敢删掉,于是jar

包越导越多,到最后框架是搭起来,但jar包却导了十几二十个。

注意点:这里建议的做法是当你不熟悉框架需要导入什么jar包时,可以先导入核心包,然后当运行项目时提示NotFoundClass时,再根据错误提示添加相关的依赖,这样就不会导入多余的jar包。

推荐可以到该网站上去搜索你需要的依赖:https://mvnrepository.com/

pom文件:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

hcoding

MyWebDemo

war

0.0.1-SNAPSHOT

MyWebDemo Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

org.springframework

spring-context

4.3.9.RELEASE

org.springframework

spring-core

4.3.9.RELEASE

org.springframework

spring-beans

4.3.9.RELEASE

org.springframework

spring-web

4.3.9.RELEASE

org.springframework

spring-aspects

4.3.9.RELEASE

org.springframework

spring-orm

4.3.9.RELEASE

org.springframework

spring-webmvc

4.3.9.RELEASE

org.slf4j

slf4j-api

1.7.6

aspectj

aspectjrt

1.5.3

org.aspectj

aspectjtools

1.8.7

javax.servlet

jstl

1.2

runtime

javax.annotation

javax.annotation-api

1.2

javax.servlet

servlet-api

2.5

org.hibernate

hibernate-core

4.2.3.Final

c3p0

c3p0

0.9.1.2

mysql

mysql-connector-java

5.1.35

MyWebDemo

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

hcoding

MyWebDemo

war

0.0.1-SNAPSHOT

MyWebDemo Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

org.springframework

spring-context

4.3.9.RELEASE

org.springframework

spring-core

4.3.9.RELEASE

org.springframework

spring-beans

4.3.9.RELEASE

org.springframework

spring-web

4.3.9.RELEASE

org.springframework

spring-aspects

4.3.9.RELEASE

org.springframework

spring-orm

4.3.9.RELEASE

org.springframework

spring-webmvc

4.3.9.RELEASE

org.slf4j

slf4j-api

1.7.6

aspectj

aspectjrt

1.5.3

org.aspectj

aspectjtools

1.8.7

javax.servlet

jstl

1.2

runtime

javax.annotation

javax.annotation-api

1.2

javax.servlet

servlet-api

2.5

org.hibernate

hibernate-core

4.2.3.Final

c3p0

c3p0

0.9.1.2

mysql

mysql-connector-java

5.1.35

MyWebDemo

其中,注释块 中的依赖导入了框架核心jar包,我们可以提前导入,其他的我们可以根据提示或者需求进行改动。

比如,我的项目中使用的是mysql数据库和c3p0数据源,所以引入MySQL和c3p0的依赖,如果你使用的是其他数据库和数据源就需要做相应的修改。

pom文件中为注释的部分则是在项目运行时提示的jar包缺失引入的依赖,有的是spring框架依赖的包,有的则是servlet或者jsp依赖的,如果第一次搭建时不熟悉可以先不导入,等报错时再引入依赖,这样也可以加深理解。

3.配置文件

这是框架整合最重要的部分。配置文件除了web.xml之外都放在main/resources文件夹中,当然你也可以放在其他自己新建的文件夹下,不过配置文件地址要做相应的修改,这个等会详细说。

配置文件的配置,很多框架整合的教程都不太一样的,这里给出我自己的配置方法和做法。

配置文件主要分为四个:web.xm , beans.xml , spring-mvc.xml , datasource.xml 。

配置步骤分为两步,spring-mvc和spring的整合,hibernate和spring的整合,最后再运行项目。(这也是效率比较高的做法,分开整合,这样即使运行项目有报错也比较好找,而且分部整合不容易乱)

接下来就开始具体配置。

4.spring-mvc和spring整合

这是spring-mvc.xml配置文件:

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

expression="org.springframework.stereotype.Controller" />

expression="org.springframework.stereotype.Service" />

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

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

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

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

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

http://springframework.org/schema/context

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

http://springframework.org/schema/mvc

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

expression="org.springframework.stereotype.Controller" />

expression="org.springframework.stereotype.Service" />

expression="org.springframework.stereotype.Controller" />

expression="org.springframework.stereotype.Service" />

expression="org.springframework.stereotype.Service" />

这是spring-mvc基本的配置,主要是对请求和静态资源映射的配置。

注意点:

1.需要特别注意的是扫描包时要排除service层的类,不然在整合hibernate后,项目运行时会报错。

具体原因看一下这篇文章

2.然后就是如果你的包名和结构不一样的,那么扫描的包地址也要记得更换。

然后是web.xml配置文件:

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

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

MyWebDemo

charsetEncoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

charsetEncoding

/*

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

spring

/

将spring-mvc配置文件加到web.xml中,当服务器启动项目时,才会加载springmvc。配置完成后写个例子测试一下。

此时的项目结构:

-src

---main

------java(class文件)

----------com.hcoding.controller

----------com.hcoding.service

----------com.hcoding.dao

----------com.hcoding.model

------resources(配置文件)

----------spring-mvc.xml

------webapp(web文件)

----------demo(jsp文件)

----------resources(静态资源文件:css,js,img)

----------WEB-INF (web相关配置文件)

先在controller包中创建DemoController,class:

package com.hcoding.demo.controller;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import com.hcoding.demo.model.People;

import com.hcoding.demo.service.DemoService;

@Controller

public class DemoController{

@RequestMapping("/test")

public String test(){

return "test";

}

}

然后在demo文件夹中创建demo.jsp:

<%@ page language="java" contentType="text/html; UTF-8"

pageEncoding="UTF-8"%>

这是测试页面。

启动项目,在浏览器中输入地址:http://localhost:8080/

tepYGfMe

(项目名)/test 。成功可以看到页面。

5.spring和hibernate整合

datasource.xml配置文件:

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

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

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

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

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

xsi:schemaLocation="

http://springframework.org/schema/aop

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

http://springframework.org/schema/beans

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

http://springframework.org/schema/tx

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

http://springframework.org/schema/jee

http://springframework.org/schema/jee/spring-jee-3.2.xsd

http://springframework.org/schema/context

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

38 ${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

${hibernate.format_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

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

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

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

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

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

xsi:schemaLocation="

http://springframework.org/schema/aop

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

http://springframework.org/schema/beans

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

http://springframework.org/schema/tx

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

http://springframework.org/schema/jee

http://springframework.org/schema/jee/spring-jee-3.2.xsd

http://springframework.org/schema/context

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

38 ${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

${hibernate.format_sql}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

datasource.properties文件:

#database connection config

jdbc.driver = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8

jdbc.username = root

jdbc.password = 123456

#hibernate config

hibernate.dialect = org.hibernate.dialect.MySQLDialect

hibernate.show_sql = true

hibernate.format_sql = true

hibernate.hbm2ddl.auto = update

beans.xml文件:

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

xmlns:p="http://springframework.org/schema/p" xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

http://springframework.org/schema/cache

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

expression="org.springframework.stereotype.Controller" />

classpath:datasource.properties

31 32 33 34

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

xmlns:p="http://springframework.org/schema/p" xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans

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

http://springframework.org/schema/context

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

http://springframework.org/schema/cache

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

expression="org.springframework.stereotype.Controller" />

classpath:datasource.properties

31 32 33 34

expression="org.springframework.stereotype.Controller" />

classpath:datasource.properties

31 32 33 34

注意点:在xml导入properties文件是比较常见,将一些相关的配置数据写到properyties文件中也是常用的方法,利于修改。

常见的另一种导入propertise的方法:在。这种方法spring默认值只导入一个properties,

当有多个properties文件需要导入时,则需要使用另一种方法:

classpath:datasource.properties

classpath:redis.properties

个人比较推荐这种,随着项目扩展,需要导入的配置增多,肯定不止一个properties文件,这种方法更通用。

注意点:datasource.properties文件中的数据库地址,用户和密码根据自己的情况做相应的修改。

修改之前的web.xml文件:

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

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

MyWebDemo

charsetEncoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

charsetEncoding

/*

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

spring

/

contextConfigLocation

classpath:beans.xml

org.springframework.web.context.ContextLoaderListener

这就是spring整合hibernate所需要配置的四个文件。文件的加载顺序和包含关系:web.xml→bans.xml→datasource.xml→datasource.properties

注意点:如果你的配置文件名称和存放位置不同,那么你也需要相应的修改。

注意点:一定要记得配置事务,否则操作数据库时项目可能不会报错,但数据库中的数据将不会更新(删除或者修改)。具体可以自行百度事务相关的知识。

配置完成后写个例子测试一下。

在model包中创建People.class:

package com.hcoding.demo.model;

import java.util.Set;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Transient;

@Entity

@Table(name="people")

public class People {

@Id

@Column(name="ID")

private int id;

@Column(name="name")

private String name;

@Column(name="sex")

private String sex;

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

并在数据库创建people表格,添加相应的字段。

然后在dao包中创建DemoDao.class:

package com.hcoding.demo.dao;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Repository;

import com.hcoding.demo.model.People;

@Repository("demoDao")

public class DemoDao extends BaseSupportDao{

@Resource(name="sessionFactory")

private SessionFactory sessionFactory;

/**

* 保存对象

* @param p

* @return

*/

public People save(People p){

return (People) sessionFactory.getCurrentSession().save(p);

}

}

在service包中创建DemoServic.class:

package com.hcoding.demo.service;

import java.util.HashSet;

import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.hcoding.demo.dao.DemoDao;

import com.hcoding.demo.model.Book;

import com.hcoding.demo.model.People;

@Service("demoService")

public class DemoService {

@Resource(name="demoDao")

private DemoDao demoDao;;

@Transactional

public void save(People p){

demoDao.save(p);

}

public People newPeople(){

People p=new People();

p.setName("小白");

p.setSex("男");

return p;

}

}

注意点:在save方法上加@Transactional注解来开启事务。

在DemoController.class中调用save方法保存数据:

package com.hcoding.demo.controller;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import com.hcoding.demo.model.People;

import com.hcoding.demo.service.DemoService;

@Controller

public class DemoController{

@Resource(name="demoService")

private DemoService demoService;

@RequestMapping("/test")

public String test(){

People p=demoService.newPeople();

demoService.save(p);

return "test";

}

}

启动服务器,访问项目。刷新数据库,如果数据保存到数据库中了,说明框架搭建完成。

6.总结

以上就是框架整合的全过程,也是自己看了很多教程和自己搭了很多遍后整理的,基本上配置文件是比较整洁,没有多余的内容(因为很容易由于不懂原理,依样画葫芦莫名其妙写了些没有的内容进去),大部分内容也有说明作用,也说了一些需要注意的,我自己犯过错地方。

当然,如果你是第一次搭建框架,那么问题远没有那么少,遇到问题就多百度吧,当然,在这之前对于spring框架还是要多了解一点会更利于学习。另外,项目是全注解的,所以对注解不太了解也可以自行百度一下。

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

上一篇:Spring注解方式无法扫描Service注解的解决
下一篇:Spring和SpringMVC扫描注解类冲突的解决方案
相关文章

 发表评论

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