Spring Boot应用连接数据库MySQL

本文继续在《在Spring Boot应用中使用JSP开发网页》基础上介绍如何连接数据库MySQL。

修改pom.xml文件

在项目的pom.xml文件上增加如下代码,添加依赖文件。

<dependency>
  <groupId>mysqlgroupId>
  <artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-data-jpaartifactId>
dependency>

设置全局配置文件

在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

需要添加的配置都在上面的代码中,不需要另外的XML配置和Java配置。上面代码中的数据库配置,你需要换成你的数据库的地址和用户名密码。hibernate的ddl-auto=update配置表名,数据库的表和列会自动创建。

MySQL数据库

安装MySQL,MySQL官方下载连接。安装后,建立一个名为student的表。我的表如下:
Spring Boot应用连接数据库MySQL_第1张图片

建立实体

在包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;
    }
}

实体的数据访问层StudentRespositroy

在包com.neon.apple下新建一个respositroy包,然后在respositroy下建立StudentRespositroy接口。StudentRespositroy接口继承CrudRespositroy,CrudRespositroy已经实现了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 findStudentById(Long id);
}

编写控制器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 model){
        Long id = (long) 1;
        Student stu = studentRepository.findStudentById(id);
        model.put("student", stu);
        return "testDataBase";
    }
}

建立testDataBase.jsp文件,用来简单显示MySQL数据库student表的数据,代码如下。


<%@ 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>

测试结果

运行后,浏览器显示如下:
Spring Boot应用连接数据库MySQL_第2张图片

返回 Java EE Web开发系列导航。

你可能感兴趣的:(Spring Boot应用连接数据库MySQL)