原文地址:http://blog.csdn.net/webzhuce/article/details/54428885
本文繼續在《在Spring Boot應用中使用JSP開發網頁》基礎上介紹如何連接數據庫MySQL。
修改pom.xml文件
在項目的pom.xml文件上增加如下代碼,添加依賴文件。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
設置全局配置文件
在src/main/resources/application.properties中設置數據源和jpa配置。
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/apple
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
需要添加的配置都在上面的代碼中,不需要另外的XML配置和Java配置。上面代碼中的數據庫配置,你需要換成你的數據庫的地址和用戶名密碼。hibernate的ddl-auto=update配置表名,數據庫的表和列會自動創建。
MySQL數據庫
安裝mysql,MySQL官方下載連接。安裝後,建立一個名爲student的表。我的表如下:
建立實體
在包com.neon.apple下新建一個model包,然後在model下建立Student類。Student類用來創建一個Student實體,Student包含三個屬性id,name,sex,age和telephone。Student實體和Mysql數據庫的student表相對應,如上圖。
package com.neon.apple.model;
import javax.persistence.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Long age;
private String telephone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
實體的數據訪問層StudentRespositroy
在包com.neon.apple下新建一個repositroy包,然後在respositroy下建立StudentRepositroy接口。StudentRepositroy接口繼承CrudRespositroy,CrudRepositroy已經實現了save,delete,deleteAll,findOne和findAll。
package com.neon.apple.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.neon.apple.model.Student;
@Repository
public interface StudentRepository extends CrudRepository<Student, Long>{
Student findStudentById(Long id);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
編寫控制器Controller和JSP頁面
在之前com.neon.apple.controller包的LoginController類中繼續編輯,完整代碼如下:
package com.neon.apple.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.neon.apple.model.Student;
import com.neon.apple.repository.StudentRepository;
@Controller
public class LoginController {
@Autowired
StudentRepository studentRepository;
@RequestMapping("/")
public String home() {
return "index";
}
@RequestMapping(value="/testdb")
public String testDataBase(Map<String, Object> model){
Long id = (long) 1;
Student stu = studentRepository.findStudentById(id);
model.put("student", stu);
return "testDataBase";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
建立testDataBase.jsp文件,用來簡單顯示MySQL數據庫student表的數據,代碼如下。
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<span>${student.name}</span>
</div>
<div>
<span>${student.age}</span>
</div>
</body>
<script src="/static/js/jquery-3.1.1.min.js"></script>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
測試結果
運行後,瀏覽器顯示如下: