框架篇: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配置文件如下(都写有说明的哈): 

复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>XRog</groupId>
  <artifactId>ssh</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssh Maven Webapp</name>
  <url>http://maven.apache.org</url>

<!– 本人将配置包的版本信息都统一放入此处,方便以后管理–>
<properties>
<springVersion>4.3.1.RELEASE</springVersion>
<hibernateVersion>4.3.5.Final</hibernateVersion>
<jstlVersion>1.2</jstlVersion>
<taglibVersion>1.1.2</taglibVersion>
<servletVersion>3.0-alpha-1</servletVersion>
<jsonVersion>1.9.13</jsonVersion>
<jacksonVersion>2.5.0</jacksonVersion>
<mysqlVersion>5.1.38</mysqlVersion>
<c3p0Version>0.9.1.2</c3p0Version>
<log4jVersion>1.2.17</log4jVersion>
<fileuploadVersion>1.3.1</fileuploadVersion>
<lombokVersion>1.16.10</lombokVersion>
</properties>

<dependencies>
<!– 单元测试 –>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> spring</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-beans<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-context<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-orm<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> spring web + spring MVC</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-web<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.springframework<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>spring-webmvc<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${springVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> hibernate配置</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.hibernate<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>hibernate-core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${hibernateVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> hibernate 缓存, 视情况添加</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.hibernate<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>hibernate-ehcache<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${hibernateVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> jsp页面使用的jstl支持</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>jstl<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jstl<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${jstlVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>taglibs<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>standard<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${taglibVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>javax.servlet<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>servlet-api<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${servletVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>provided<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> DataBase数据库连接 mysql包</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>mysql<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>mysql-connector-java<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${mysqlVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> 数据库连接池</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>c3p0<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>c3p0<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${c3p0Version}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> json数据 使springMVC可以返回json值 ,视情况添加</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.codehaus.jackson<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jackson-mapper-asl<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${jsonVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>com.fasterxml.jackson.core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jackson-core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${jacksonVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>com.fasterxml.jackson.core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jackson-annotations<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${jacksonVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>com.fasterxml.jackson.core<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>jackson-databind<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${jacksonVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> log4j配置, 视情况添加</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>log4j<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>log4j<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${log4jVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000">文件 上传</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>commons-fileupload<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>commons-fileupload<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${fileuploadVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> lombok插件导包</span><span style="color: #008000">--&gt;</span>
  <span style="color: #0000ff">&lt;</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>org.projectlombok<span style="color: #0000ff">&lt;/</span><span style="color: #800000">groupId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>lombok<span style="color: #0000ff">&lt;/</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>${lombokVersion}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">version</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>provided<span style="color: #0000ff">&lt;/</span><span style="color: #800000">scope</span><span style="color: #0000ff">&gt;</span>
  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">dependency</span><span style="color: #0000ff">&gt;</span>

</dependencies>

<build>
<finalName>ssh</finalName>
</build>
</project>

复制代码

 

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

 

SpringMVC配置

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

 

 

我们先配置下SpringMVC的配置:resources/META-INF/spring-mvc.xml

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
   <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 启动注解驱动的spring MVC功能,注册请求url和注解POJO类方法的映射</span><span style="color: #008000">--&gt;</span>
   <span style="color: #0000ff">&lt;</span><span style="color: #800000">mvc:annotation-driven </span><span style="color: #0000ff">/&gt;<br></span></pre>
     <context:component-scan base-package="com.ssh" />
    <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="views/" />      <!-- 前缀 -->
        <property name="suffix" value=".jsp" />   <!-- 后缀 -->
    </bean>
    
    <!--这里是对静态资源的映射-->
    <mvc:resources mapping="/js/**" location="/resources/js/" />
    <mvc:resources mapping="/css/**" location="/resources/css/" />
    <mvc:resources mapping="/img/**" location="/resources/img/" />

</beans>
复制代码

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

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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">
        <!-- 地址为http://localhost:8080/  显示的默认网页-->
        <welcome-file-list>
          <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> spring MVC config start</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">servlet</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">&gt;</span>spring<span style="color: #0000ff">&lt;/</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">servlet-class</span><span style="color: #0000ff">&gt;</span>org.springframework.web.servlet.DispatcherServlet<span style="color: #0000ff">&lt;/</span><span style="color: #800000">servlet-class</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">init-param</span><span style="color: #0000ff">&gt;</span>
          <span style="color: #0000ff">&lt;</span><span style="color: #800000">param-name</span><span style="color: #0000ff">&gt;</span>contextConfigLocation<span style="color: #0000ff">&lt;/</span><span style="color: #800000">param-name</span><span style="color: #0000ff">&gt;</span>
          <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 此处指向的的是SpringMVC的配置文件 </span><span style="color: #008000">--&gt;</span>
          <span style="color: #0000ff">&lt;</span><span style="color: #800000">param-value</span><span style="color: #0000ff">&gt;</span>classpath:META-INF/spring-mvc.xml<span style="color: #0000ff">&lt;/</span><span style="color: #800000">param-value</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">init-param</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">配置容器在启动的时候就加载这个servlet并实例化</span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">load-on-startup</span><span style="color: #0000ff">&gt;</span>1<span style="color: #0000ff">&lt;/</span><span style="color: #800000">load-on-startup</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">servlet</span><span style="color: #0000ff">&gt;</span>

    <span style="color: #0000ff">&lt;</span><span style="color: #800000">servlet-mapping</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">&gt;</span>spring<span style="color: #0000ff">&lt;/</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">&gt;</span>/<span style="color: #0000ff">&lt;/</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">servlet-mapping</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> spring MVC config end</span><span style="color: #008000">--&gt;</span>

</web-app>

复制代码

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网页如下:

复制代码
<html>
<body>
<center>
<h2 style="color: #ff261a;">this is my test page!</h2>
</center>
</body>
</html>
复制代码

 

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

 

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

OK,成功访问。 

SpringMVC+Spring整合

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

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    &lt;!-- 自动扫描 --&gt;
    &lt;context:component-scan base-<span style="color: #0000ff">package</span>="com.ssh"&gt;
        &lt;!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) --&gt;
        &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt;
    &lt;/context:component-scan&gt;

</beans>

复制代码

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

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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">
        <!-- 地址为http://localhost:8080/  显示的默认网页-->
        <welcome-file-list>
          <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
&lt;!--加载Spring的配置文件到上下文中去--&gt;
    &lt;context-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;<span style="color: #000000">
            classpath:META</span>-INF/<span style="color: #000000">applicationContext.xml
        </span>&lt;/param-value&gt;
    &lt;/context-param&gt;

    &lt;!-- spring MVC config start--&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
        &lt;servlet-<span style="color: #0000ff">class</span>&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-<span style="color: #0000ff">class</span>&gt;
        &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;!-- 此处指向的的是SpringMVC的配置文件 --&gt;
          &lt;param-value&gt;classpath:META-INF/spring-mvc.xml&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;!--配置容器在启动的时候就加载这个servlet并实例化--&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;!-- spring MVC config end--&gt;


    &lt;!-- Spring监听器 --&gt;
    &lt;listener&gt;
        &lt;listener-<span style="color: #0000ff">class</span>&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-<span style="color: #0000ff">class</span>&gt;
    &lt;/listener&gt;

    &lt;!--  字符集过滤  --&gt;
    &lt;filter&gt;
        &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
        &lt;filter-<span style="color: #0000ff">class</span>&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-<span style="color: #0000ff">class</span>&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;encoding&lt;/param-name&gt;
            &lt;param-value&gt;UTF-8&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
            &lt;param-value&gt;<span style="color: #0000ff">true</span>&lt;/param-value&gt;
        &lt;/init-param&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
        &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;<span style="color: #008000">/*</span><span style="color: #008000">&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;

</web-app>

复制代码

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如下:

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<span style="color: #008000">&lt;!--</span><span style="color: #008000">********************************************配置Spring***************************************</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 自动扫描 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">context:component-scan </span><span style="color: #ff0000">base-package</span><span style="color: #0000ff">="com.ssh"</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> 扫描时跳过 @Controller 注解的JAVA类(控制器) </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">context:exclude-filter </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="annotation"</span><span style="color: #ff0000"> expression</span><span style="color: #0000ff">="org.springframework.stereotype.Controller"</span><span style="color: #0000ff">/&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">context:component-scan</span><span style="color: #0000ff">&gt;</span>


<span style="color: #008000">&lt;!--</span><span style="color: #008000">********************************************配置hibernate********************************************</span><span style="color: #008000">--&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000">扫描配置文件(这里指向的是之前配置的那个config.properties)</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">context:property-placeholder </span><span style="color: #ff0000">location</span><span style="color: #0000ff">="classpath:/config.properties"</span> <span style="color: #0000ff">/&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000">配置数据源</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="dataSource"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="com.mchange.v2.c3p0.ComboPooledDataSource"</span><span style="color: #ff0000"> destroy-method</span><span style="color: #0000ff">="close"</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="driverClass"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="${jdbc.driver}"</span> <span style="color: #0000ff">/&gt;</span>  <span style="color: #008000">&lt;!--</span><span style="color: #008000">数据库连接驱动</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="jdbcUrl"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="${jdbc.url}"</span> <span style="color: #0000ff">/&gt;</span>     <span style="color: #008000">&lt;!--</span><span style="color: #008000">数据库地址</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="user"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="${jdbc.username}"</span> <span style="color: #0000ff">/&gt;</span>   <span style="color: #008000">&lt;!--</span><span style="color: #008000">用户名</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="password"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="${jdbc.password}"</span> <span style="color: #0000ff">/&gt;</span>   <span style="color: #008000">&lt;!--</span><span style="color: #008000">密码</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="maxPoolSize"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="40"</span> <span style="color: #0000ff">/&gt;</span>      <span style="color: #008000">&lt;!--</span><span style="color: #008000">最大连接数</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="minPoolSize"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="1"</span> <span style="color: #0000ff">/&gt;</span>       <span style="color: #008000">&lt;!--</span><span style="color: #008000">最小连接数</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="initialPoolSize"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="10"</span> <span style="color: #0000ff">/&gt;</span>      <span style="color: #008000">&lt;!--</span><span style="color: #008000">初始化连接池内的数据库连接</span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="maxIdleTime"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="20"</span> <span style="color: #0000ff">/&gt;</span>  <span style="color: #008000">&lt;!--</span><span style="color: #008000">最大空闲时间</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">bean</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000">配置session工厂</span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="sessionFactory"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="org.springframework.orm.hibernate4.LocalSessionFactoryBean"</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="dataSource"</span><span style="color: #ff0000"> ref</span><span style="color: #0000ff">="dataSource"</span> <span style="color: #0000ff">/&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="packagesToScan"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="com.ssh.entity"</span> <span style="color: #0000ff">/&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="hibernateProperties"</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">props</span><span style="color: #0000ff">&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">prop </span><span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.hbm2ddl.auto"</span><span style="color: #0000ff">&gt;</span>${hibernate.hbm2ddl.auto}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">prop</span><span style="color: #0000ff">&gt;</span> <span style="color: #008000">&lt;!--</span><span style="color: #008000">hibernate根据实体自动生成数据库表</span><span style="color: #008000">--&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">prop </span><span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.dialect"</span><span style="color: #0000ff">&gt;</span>${hibernate.dialect}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">prop</span><span style="color: #0000ff">&gt;</span>   <span style="color: #008000">&lt;!--</span><span style="color: #008000">指定数据库方言</span><span style="color: #008000">--&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">prop </span><span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.show_sql"</span><span style="color: #0000ff">&gt;</span>${hibernate.show_sql}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">prop</span><span style="color: #0000ff">&gt;</span>     <span style="color: #008000">&lt;!--</span><span style="color: #008000">在控制台显示执行的数据库操作语句</span><span style="color: #008000">--&gt;</span>
            <span style="color: #0000ff">&lt;</span><span style="color: #800000">prop </span><span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.format_sql"</span><span style="color: #0000ff">&gt;</span>${hibernate.format_sql}<span style="color: #0000ff">&lt;/</span><span style="color: #800000">prop</span><span style="color: #0000ff">&gt;</span>     <span style="color: #008000">&lt;!--</span><span style="color: #008000">在控制台显示执行的数据哭操作语句(格式)</span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">props</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">property</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">bean</span><span style="color: #0000ff">&gt;</span>

<span style="color: #008000">&lt;!--</span><span style="color: #008000"> 事物管理器配置  </span><span style="color: #008000">--&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="transactionManager"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="org.springframework.orm.hibernate4.HibernateTransactionManager"</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="sessionFactory"</span><span style="color: #ff0000"> ref</span><span style="color: #0000ff">="sessionFactory"</span> <span style="color: #0000ff">/&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">bean</span><span style="color: #0000ff">&gt;</span>

</beans>

复制代码

 

 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,PK extends Serializable>{
    T load(PK id);

    T get(PK id);

    List<T> 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<Person,Long> {
    }
复制代码
复制代码
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<Person> 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产生是调用的SessionFactory的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,测试完毕,十分成功

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值