Java框架篇:Spring+SpringMVC+hibernate整合开发

网友投稿 206 2023-06-15


Java框架篇:Spring+SpringMVC+hibernate整合开发

前言:

最近没什么事做,搭个框架写成博客记录下来,拉通一下之前所学知识.

话不多说,我们直接步入正题。

准备工作:

1/安装并配置java运行环境

2/数据库的安装配置(mysql)

3/安装并配置服务器(Tomcat)

4/Maven

5/ IntelliJIDEA的安装配置(本人使用的主要软件是IntelliJIDEA,没用eclipse什么的)

6/ 使用IntelliJIDEA创建一个web app项目。

貌似就这些了吧

导包

不同于以往的导包,由于我们创建的是maven的webapp项目,所以现在只需配置下pomxml这个配置文件,系统会自动到maven的中央仓库去下载相应的包.

本人的pom.xml配置文件如下(都写有说明的哈):

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

4.0.0

XRog

ssh

war

1.0-SNAPSHOT

ssh Maven Webapp

http://maven.apache.org

4.3.1.RELEASE

4.3.5.Final

1.2

1.1.2

3.0-alpha-1

1.9.13

2.5.0

6.0.3

0.9.1.2

1.2.17

1.3.1

1.16.10

junit

junit

3.8.1

test

org.springframework

spring-beans

${springVersion}

org.springframework

spring-core

${springVersion}

org.springframework

spring-context

${springVersion}

org.springframework

spring-orm

${springVersion}

org.springframework

spring-web

${springVersion}

org.springframework

spring-webmvc

${springVersion}

</dependency>

org.hibernate

hibernate-core

${hibernateVersion}

org.hibernate

hibernate-ehcache

${hibernateVersion}

jstl

jstl

${jstlVersion}

taglibs

standard

${taglibVersion}

javax.servlet

servlet-api

${servletVersion}

provided

mysql

mysql-connector-java

${mysqlVersion}

c3p0

c3p0

${c3p0Version}

org.codehaus.jackson

jackson-mapper-asl

${jsonVersion}

com.fasterxml.jackson.core

jackson-core

${jacksonVersion}

com.fasterxml.jackson.core

jackson-annotations

${jacksonVersion}

com.fasterxml.jackson.core

jackson-databind

${jacksonVersion}

log4j

log4j

${log4jVersion}

commons-fileupload

commons-fileupload

${fileuploadVersion}

org.projectlombok

lombok

${lombokVersion}

provided

ssh

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

4.0.0

XRog

ssh

war

1.0-SNAPSHOT

ssh Maven Webapp

http://maven.apache.org

4.3.1.RELEASE

4.3.5.Final

1.2

1.1.2

3.0-alpha-1

1.9.13

2.5.0

6.0.3

0.9.1.2

1.2.17

1.3.1

1.16.10

junit

junit

3.8.1

test

org.springframework

spring-beans

${springVersion}

org.springframework

spring-core

${springVersion}

org.springframework

spring-context

${springVersion}

org.springframework

spring-orm

${springVersion}

org.springframework

spring-web

${springVersion}

org.springframework

spring-webmvc

${springVersion}

</dependency>

org.hibernate

hibernate-core

${hibernateVersion}

org.hibernate

hibernate-ehcache

${hibernateVersion}

jstl

jstl

${jstlVersion}

taglibs

standard

${taglibVersion}

javax.servlet

servlet-api

${servletVersion}

provided

mysql

mysql-connector-java

${mysqlVersion}

c3p0

c3p0

${c3p0Version}

org.codehaus.jackson

jackson-mapper-asl

${jsonVersion}

com.fasterxml.jackson.core

jackson-core

${jacksonVersion}

com.fasterxml.jackson.core

jackson-annotations

${jacksonVersion}

com.fasterxml.jackson.core

jackson-databind

${jacksonVersion}

log4j

log4j

${log4jVersion}

commons-fileupload

commons-fileupload

${fileuploadVersion}

org.projectlombok

lombok

${lombokVersion}

provided

ssh

OK,配置后运行下maven,就会自动向中央仓库下载相应的包啦!(这个就不多说了)。

SpringMVC配置

我现在在这里把我配置后的结构拿出来给你们看下

我们先配置下SpringMVC的配置:resources/META-INF/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">

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

好了,我们现在修改下web.xml这个配置文件,完善下SpringMVC的配置, web.xml配置如下:

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

/index.jsp

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:META-INF/spring-mvc.xml

1

spring

/

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

/index.jsp

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:META-INF/spring-mvc.xml

1

spring

/

OK,SpringMVC配置完成,现在我们进行一下测试吧

在controller层新建一个MainController,内容如下

package com.ssh.controller;

import org.springframework.stereotype.Controller;

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

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

/**

* Created by XRog

* On 2/1/2017.12:36 AM

*/

@Controller

public class MainController {

@RequestMapping(value = "test", method = RequestMethod.GET)

public String test(){

// 实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀

return "test";

}

}

test.jsp网页如下:

重启Tomcat服务器, 然后浏览器访问http://localhost/test如下图所示:

PS: Tomcat默认端口是8080,我更改了端口号,如若你们为更改,正确的访问地址是localhost:8080/test

OK,成功访问。

SpringMVC+Spring整合

这个就十分简单了, 配置applicationContext.xml这个Spring的配置文件如下:

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

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

完善web.xml配置文件如下:

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

/index.jsp

contextConfigLocation

classpath:META-INF/applicationContext.xml

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:META-INF/spring-mvc.xml

1

spring

/

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

/index.jsp

contextConfigLocation

classpath:META-INF/applicationContext.xml

spring

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:META-INF/spring-mvc.xml

1

spring

/

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

web.xml配置文件中更改了三处:引入Spring配置文件 Spring的监听器  以及 字符集过滤

OK,Spring+SpringMVC配置完成,下面我们开始测试:

在service写一个TestServiceImpl实现TestService接口并实现其test()方法, 代码如下:

package com.ssh.service.impl;

import com.ssh.service.TestService;

import org.springframework.stereotype.Service;

/**

* Created by XRog

* On 2/1/2017.12:58 AM

*/

@Service

public class TestServiceImpl implements TestService {

public String test() {

return "test";

}

}

PS:这里注意写@Service注解

MainController控制器更改如下:

package com.ssh.controller;

import com.ssh.service.TestService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

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

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

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

/**

* Created by XRog

* On 2/1/2017.12:36 AM

*/

@Controller

public class MainController {

@Autowired

private TestService testService;

@RequestMapping(value = "test", method = RequestMethod.GET)

public String test(){

// 实际返回的是views/test.jsp ,spring-mvc.xml中配置过前后缀

return "test";

}

@RequestMapping(value = "springtest", method = RequestMethod.GET)

public String springTest(){

return testService.test();

}

}

控制器这里我们运用了Spring的依赖注入自动装配。

在浏览器中输入地址http://localhost/springtest调用springtest方法

yes,成功返回,说明我们之前的配置没问题

Spring+SpringMVC+hibernate整合

好了,现在就缺hibernate这个框架了。。 我先给大家看些我搭建好之后的结构图吧

我们想来编写config.properties这个配置文件,里面存放的是hibernate的一些配置

#database connection config

jdbc.driver = com.mysql.jdbc.Driver

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

jdbc.username = root

jdbc.password = ???

#hibernate config

hibernate.dialect = org.hibernate.dialect.MySQLDialect

hibernate.show_sql = true

hibernate.format_sql = true

hibernate.hbm2ddl.auto = update

这里连接数据库的参数由于我是连接我晚上的数据库,因此我数据库地址和密码打了“???”,你们连接时改成自己本地的就OK了

下面配置hibernate,这里我为了方便,就直接写进applicationContext.xml里面。配置后的applicationContext.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">

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

${hibernate.format_sql} &http://lt;!--在控制台显示执行的数据哭操作语句(格式)-->

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

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

${hibernate.format_sql} &http://lt;!--在控制台显示执行的数据哭操作语句(格式)-->

OK,到了这里,配置结束。下面进入测试阶段

实体类(entity):

package com.ssh.entity;

import lombok.Data;

import javax.persistence.*;

/**

* Created by XRog

* On 2/2/2017.2:03 PM

*/

@Data

@Entity

@Table(name = "Person")

public class Person {

@Id

@GeneratedValue

private Long id;

@Column(name = "created")

private Long created = System.currentTimeMillis();

@Column(name = "username")

private String username;

@Column(name = "address")

private String address;

@Column(name = "phone")

private String phone;

@Column(name = "remark")

private String remark;

}

PS:我这里用了一个@Data注解,此注解会自动生成get方法,set方法,toString方法等一系列方法,功能十分强大,不过需要安装插件以及导包, 有兴趣的可以百度下

当然,你也可以手动编写get/set/构造方法。

数据库访问层(repository):

package com.ssh.repository;

import java.io.Serializable;

import java.util.List;

/**

* Created by XRog

* On 2/2/2017.2:28 PM

*/

public interface DomainRepository{

T load(PK id);

T get(PK id);

List findAll();

void persist(T entity);

PK save(T entity);

void saveOrUpdate(T entity);

void delete(PK id);

void flush();

}

package com.ssh.repository;

import com.ssh.entity.Person;

/**

* Created by XRog

* On 2/2/2017.2:25 PM

*/

public interface PersonRepository extends DomainRepository {

}

package com.ssh.repository.impl;

import com.ssh.repository.PersonRepository;

import com.ssh.entity.Person;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

import java.util.List;

/**

* Created by XRog

* On 2/2/2017.2:30 PM

*/

@Repository

public class PersonRepositoryImpl implements PersonRepository {

@Autowired

private SessionFactory sessionFactory;

private Session getCurrentSession() {

return this.sessionFactory.openSession();

}

public Person load(Long id) {

return (Person)getCurrentSession().load(Person.class,id);

}

public Person get(Long id) {

return (Person)getCurrentSession().get(Person.class,id);

}

public List findAll() {

return null;

}

public void persist(Person entity) {

getCurrentSession().persist(entity);

}

public Long save(Person entity) {

return (Long)getCurrentSession().save(entity);

}

public void saveOrUpdate(Person entity) {

getCurrentSession().saveOrUpdate(entity);

}

public void delete(Long id) {

Person person = load(id);

getCurrentSession().delete(person);

}

public void flush() {

getCurrentSession().flush();

}

}

PS:我这里显示写了一个比较通用的接口,其他所有接口皆继承此接口, 再编写实现类

注意:我这里写的session产生是调用的Shttp://essionFactory的openSession()这个方法。之前使用getCurrentSession()一直报错,后来百度了一下才知道,hibernate3版本可以使用getCurrentSession()来创建session,而hibernate4版本则不行。

服务层(service):

package com.ssh.service;

import com.ssh.entity.Person;

/**

* Created by XRog

* On 2/2/2017.2:39 PM

*/

public interface PersonService {

Long savePerson();

}

package com.ssh.service.impl;

import com.ssh.entity.Person;

import com.ssh.repository.PersonRepository;

import com.ssh.service.PersonService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

/**

* Created by XRog

* On 2/2/2017.2:40 PM

*/

@Service

public class PersonServiceImpl implements PersonService {

@Autowired

private PersonRepository personRepository;

public Long savePerson() {

Person person = new Person();

person.setUsername("XRog");

person.setPhone("18381005946");

person.setAddress("chenDu");

person.setRemark("this is XRog");

return personRepository.save(person);

}

}

控制层(controller):

package com.ssh.controller;

import com.ssh.entity.Person;

import com.ssh.service.PersonService;

import com.ssh.service.TestService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

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

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

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

/**

* Created by XRog

* On 2/1/2017.12:36 AM

*/

@Controller

public class MainController {

@Autowired

private PersonService personService;

@RequestMapping(value = "savePerson", method = RequestMethod.GET)

@ResponseBody

public String savePerson(){

personService.savePerson();

return "success!";

}

}

OK,编写完毕,我们重启下服务器然后测试:

我们在看下数据库,跟我们插入的数据对比下

OK,测试完毕,十分成功


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

上一篇:bootstrap输入框组使用方法
下一篇:探讨Java中函数是值传递还是引用传递问题
相关文章

 发表评论

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