Spring Boot应用连接数据库MySQL
2017-07-18 14:01
609 查看
原文地址:http://blog.csdn.net/webzhuce/article/details/54428885
本文继续在《在Spring Boot应用中使用JSP开发网页》基础上介绍如何连接数据库MySQL。
在项目的pom.xml文件上增加如下代码,添加依赖文件。
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配置。
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官方下载连接。安装后,建立一个名为student的表。我的表如下:
在包com.neon.apple下新建一个model包,然后在model下建立Student类。Student类用来创建一个Student实体,Student包含三个属性id,name,sex,age和telephone。Student实体和Mysql数据库的student表相对应,如上图。
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
在包com.neon.apple下新建一个repositroy包,然后在respositroy下建立StudentRepositroy接口。StudentRepositroy接口继承CrudRespositroy,CrudRepositroy已经实现了save,delete,deleteAll,findOne和findAll。
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
在之前com.neon.apple.controller包的LoginController类中继续编辑,完整代码如下:
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表的数据,代码如下。
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
运行后,浏览器显示如下:
本文继续在《在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.MySQL5Dialect1
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
测试结果
运行后,浏览器显示如下: 相关文章推荐
- Spring Boot应用连接数据库MySQL,并编写自定义SQL
- 二、spring Boot构建的Web应用中,基于MySQL数据库的几种数据库连接方式进行介绍
- spring boot mybatis连接数据库
- Spring Boot JDBC 连接数据库
- docker连接spring boot和mysql容器方法介绍
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- spring boot 连接数据库
- SpringBootl连接数据库MyBatis方式实现
- springboot+springAOP实现数据库读写分离及数据库同步(MySQL)
- SpringBoot实践之---JDBC连接数据库
- Spring Boot多数据源连接8小时后断开的问题解决(MySQL)
- spring boot 学习--05---mysql,jpa,jdbcTemplate-应用
- springboot学习笔记--Spring Boot使用数据库(MySQL)
- spring-boot-data-jpa 连接数据库
- docker连接spring boot和mysql容器
- Spring连接MySQL、Oracle和SQL Server的数据库连接属性
- spring boot数据库操作(以mysql为例)
- SpringBoot - JDBC 连接数据库
- Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性
- 使用spring boot连接数据库出现no profiles are currently active的问题