使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置
注:在阅读本文前,请先阅读:
使用IntelliJ IDEA开发SpringMVC网站(一)开发环境
使用IntelliJ IDEA开发SpringMVC网站(二)框架配置
六、数据库配置
下面,就要通过一个简单的例子,来介绍,SpringMVC如何集成Spring Data JPA(有Hibernate JPA提供),来进行强大的数据库访问,并通过本章节的讲解,更加深刻地认识Controller是如何进行请求处理的,相信看完这一章节,你就可以开始你的开发工作了。
准备工作:
在src\main\java中新建两个包:com.gaussic.model、com.gaussic.repository,将在后面用上。
1、创建Mysql数据库
本文的讲解使用Mysql数据库,如果使用其它数据库的读者,可以去网上参考其他的配置教程,在此不做太多的叙述。数据库是一个底层的东西,底层的细节对上层的抽象并没有太大的影响,因此,只要配置好数据库,本章的内容仍然是适用于所有数据库的(貌似如此)。
假设我们现在要建立一个小小的博客系统,其数据库ER图如下所示(当然这只是一个小小的例子,真实的博客系统远远比这要复杂的多):
在数据库中,有两张表:
(1)用户表user:储存用户的登录信息,主键id设为自增;
(2)博文表blog:储存用户发表的博文,主键id设为自增,其中有一个外键userid链接到user表。
详细表结构如下图所示:
2、IntelliJ IDEA导入数据库
对于此前所接触的一些常用的框架中,一张数据表往往对应一个Java Bean。在SpringMVC中,这个Java Bean相当于model。那么,这个类是否需要自己来写呢?不需要,利用IntelliJ IDEA可以帮我们自动的生成这些JavaBean。
首先,右键项目,选择Add Framework Support:
下拉选择JavaEE Persistence,右边provider选择Hibernate,Libraries选择Set up Later(Maven已经将所需jar包都导入了):
在这一步结束后,我们可以发现,在resources里面生成了persistence.xml配置文件,左边栏出现了一个Persistence标题(若没有请点击左下角那个灰框):
persistemce.xml具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value=""/>
<property name="hibernate.connection.driver_class" value=""/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
我们先不着急填写这个配置文件。点开左边栏的Persistence,显示如下图所示:
右键项目名,选择Generate Persistence Mapping,再选择By Database Schema:
出现如下界面,其主要需要配置的地方如下图红框所示:
点击Choose Data Source右边的三个点选择数据源,在弹出的界面左上角选择“+”,选择Mysql:
在如下界面填写主机、端口号、数据库名、用户名、密码,如果驱动丢失点击下面的Download可以下载驱动,点击 Test Connection可以测试数据库是否连接成功:
在以上界面配置完成后,点OK,回到如下页面,package填写model包(1),勾选Show default relationships以显示所有数据库关系(2),再点击刷新按钮(3),将会找到数据库中的两个表,展开两个表并全选(4,除了外键),再勾选Generate Column Defination以生成每一列的描述信息(5),点击OK,选择Yes。
稍后,打开model包,可以看到生成了两个Java Bean,在SpringMVC中称为两个实体,它们对应了数据库的两张表:
BlogEntity如下所示:
package com.gaussic.model;
import javax.persistence.*;
/**
* Created by dzkan on 2015/10/4.
*/
@Entity
@Table(name = "blog", schema = "", catalog = "springdemo")
public class BlogEntity {
private int id;
private String title;
private String content;
private UserEntity userByUserid;
@Id
@Column(name = "id", nullable = false, insertable = true, updatable = true)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "title", nullable = true, insertable = true, updatable = true, length = 100)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Basic
@Column(name = "content", nullable = true, insertable = true, updatable = true, length = 255)
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BlogEntity that = (BlogEntity) o;
if (id != that.id) return false;
if (title != null ? !title.equals(that.title) : that.title != null) return false;
if (content != null ? !content.equals(that.content) : that.content != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
@ManyToOne
@JoinColumn(name = "userid", referencedColumnName = "id", nullable = false)
public UserEntity getUserByUserid() {
return userByUserid;
}
public void setUserByUserid(UserEntity userByUserid) {
this.userByUserid = userByUserid;
}
}
我们可以看到,在BlogEntity中,不仅包含了其所有的列,还多生成了一个:
private UserEntity userByUserid;
在SpringMVC中,可以通过这个类作为外键,进行外键的相关操作,具体如何使用将在后面讲解。
再看UserEntity:
package com.gaussic.model;
import javax.persistence.*;
import java.util.Collection;
/**
* Created by dzkan on 2015/10/4.
*/
@Entity
@Table(name = "user", schema = "", catalog = "springdemo")
public class UserEntity {
private int id;
private String firstName;
private String lastName;
private String password;
private Collection<BlogEntity> blogsById;
@Id
@Column(name = "id", nullable = false, insertable = true, updatable = true)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "firstName", nullable = true, insertable = true, updatable = true, length = 45)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Basic
@Column(name = "lastName", nullable = true, insertable = true, updatable = true, length = 45)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Basic
@Column(name = "password", nullable = true, insertable = true, updatable = true, length = 45)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
if (id != that.id) return false;
if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false;
if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false;
if (password != null ? !password.equals(that.password) : that.password != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "userByUserid")
public Collection<BlogEntity> getBlogsById() {
return blogsById;
}
public void setBlogsById(Collection<BlogEntity> blogsById) {
this.blogsById = blogsById;
}
}
同样生成了:
private Collection<BlogEntity> blogsById;
同样是为了满足外键的需求。
3、配置数据库
既然数据库已经导入了,那么前期准备工作基本完成,还需要进行最终的配置。
首先,打开mvc-dispatcher-servlet.xml,添加下列配置(如果某些地方报错,请选中并按Alt + Insert补全配置):
<!-- 表示JPA Repository所在的包 -->
<jpa:repositories base-package="com.gaussic.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="defaultPersistenceUnit"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
讲解:
(1) jpa:repositories:这一部分涉及到数据库的接口,将在后面详解;
(2)entityManagerFactory:实体管理器工厂,读取persistence.xml配置;
(3)transactionManager:事务管理器,利用entityManager进行事务管理;
(4)tx:annotation-driven:打开事务管理器的注解驱动,可以使用注解的方法操纵数据库。
整体如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--指明 controller 所在包,并扫描其中的注解-->
<context:component-scan base-package="com.gaussic.controller"/>
<!-- 静态资源(js、image等)的访问 -->
<mvc:default-servlet-handler/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!--ViewResolver 视图解析器-->
<!--用于支持Servlet、JSP视图解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 表示JPA Repository所在的包 -->
<jpa:repositories base-package="com.gaussic.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="defaultPersistenceUnit"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
下面,填充persistence.xml,将persistence-unit的name改为 defaultPersistenceUnit。在下面的文件中,我添加了一些更为详细的配置:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- 使用MySQL方言 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!-- 数据库连接的URL地址 -->
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/springdemo"/>
<!-- 数据库连接的驱动 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- 数据库连接的用户名 -->
<property name="hibernate.connection.username" value="root"/>
<!-- 数据库连接的密码 -->
<property name="hibernate.connection.password" value="root"/>
<!-- 显示SQL语句 -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.characterEncoding" value="UTF-8"/>
<!-- 在显示SQL语句时格式化语句 -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="false"/>
<!-- 自动输出schema创建DDL语句 -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- 数据库连接超时后自动重连 -->
<property name="hibernate.connection.autoReconnect" value="true"/>
<property name="connection.autoReconnectForPools" value="true"/>
<property name="connection.is-connection-validation-required" value="true"/>
</properties>
</persistence-unit>
</persistence>
现在,重新启动tomcat,如果没有报错,说明数据库已经配置完成了,接下来就要讲解数据库的相关开发工作。