1. 程式人生 > >基於spring boot搭建spring+springMVC+hibernate+mysql

基於spring boot搭建spring+springMVC+hibernate+mysql

版本:

spring boot:2.1.0.RELEASE

mysql:8.0.12

1、新增起步依賴

pom.xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.chen.web</groupId>
	<artifactId>web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-web</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2、編寫實體物件

package org.chenhui.web.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@Entity
@Table(name="gril")
public class Gril {
	@Id
	@Column(name="id")
	private Integer id;
	@Column(name="age")
	private Integer age;
	@Column(name="cup_size")
	private String cupSize;
	public Gril() {
		super();
	}
	public Gril(Integer id, Integer age, String cupSize) {
		super();
		this.id = id;
		this.age = age;
		this.cupSize = cupSize;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getCupSize() {
		return cupSize;
	}
	public void setCupSize(String cupSize) {
		this.cupSize = cupSize;
	}
}

3、編寫jpa介面

package org.chenhui.web.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GrilResposity extends JpaRepository<Gril, Integer>{
	
}

4、編寫業務層

package org.chenhui.web.service;

import org.chenhui.web.domain.Gril;
import org.chenhui.web.domain.GrilResposity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class GrilService {
	@Autowired
	private GrilResposity grilResposity;
	
	public Gril getOne(Integer id) {
		return grilResposity.getOne(id);
	}
}

5、編寫控制器

package org.chenhui.web.controller;

import org.chenhui.web.domain.Gril;
import org.chenhui.web.service.GrilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GrilController {
	@Autowired
	private GrilService grilService;
	
	@RequestMapping("gril/find/{id}")
	public Gril getOne(@PathVariable("id") Integer id) {
		Gril one = grilService.getOne(id);
		return one;
	}
}

6、配置檔案

server:
  port: 8888
  
spring:
  application:
    name: spring-boot-web
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/study_data?useSSL=false&serverTimezone=UTC
    username: devolpment
    password: devolpment
  jpa:
    database: mysql
    hibernate:
      ddl-auto: validate
    show-sql: true

7、資料

CREATE TABLE `gril` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `age` int(11) NOT NULL,
  `cup_size` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

8、測試

 

原始碼地址