为什么80%的码农都做不了架构师?>>>
背景
简单使用spring boot配置一个rest服务,数据库主要使用mysql没有使用spring演示时候使用的h2(一种内存数据库)和lombok(通过注解的方式生成getter和setter方法,因为要在IDEA上面安装插件,就放弃了)。
项目结构
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.example</groupId>
<artifactId>zyldemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zyldemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-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>
这里暂时不考虑spring-security。
/resources/application.properties
server.port=8080
server.servlet.contextPath=/zyldemo
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/zyldb?useSSL=true
spring.datasource.username=root
spring.datasource.password=zyl
这个部分主要是在配置mysql数据库,值得注意的是这里添加了useSSL=true
设置,启用SSL验证。
**Note:**这里spring.jpa.hibernate.ddl-auto=update
使用update,不使用create。create导致每次启动spring boot都会清空数据库数据。
model层
Customer.java
package com.example.zyldemo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue
private Long id;
private String name;
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;
}
}
这里没有使用lombok
,所以看起来这个bean类可能会有点长,不过现在ide工具基本上都能够自动生成setter和getter方法。如果不喜欢看这么长类的同学可以考虑使用使用。
dao层
好,我们现在来看看持久层。
CustomerRepository.java
package com.example.zyldemo.dao;
import com.example.zyldemo.model.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
最开始接触这个dao层的时候,要自己手动一个接口一个实现的写dao,现在的spring boot已经帮我们定义和实现了,人生真是幸福。
service层
现在,来看一看服务层,首先,我们先定义一下服务层的接口。
ICustomerService.java
package com.example.zyldemo.service;
import com.example.zyldemo.model.Customer;
public interface ICustomerService {
public boolean add(Customer customer);
}
这里主要定义一个对象保存到mysql数据库中去。然后,我们再来看一看这个在服务层怎么实现。
CustomerService.java
package com.example.zyldemo.service;
import com.example.zyldemo.dao.CustomerRepository;
import com.example.zyldemo.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CustomerService implements ICustomerService {
@Autowired
private CustomerRepository customerRepository;
@Override
public boolean add(Customer customer) {
customerRepository.save(customer);
return true;
}
}
继承之前的接口ICustomerService
,然后,注入dao层,再实现接口即可。
Controller层
重点的控制层来了。
CustomerController.java
package com.example.zyldemo.controller;
import com.example.zyldemo.model.Customer;
import com.example.zyldemo.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(path = "/customer")
public class CustomerController {
@Autowired
private ICustomerService customerService;
@GetMapping(path = "/add")
public String add(@RequestParam String name){
Customer customer = new Customer();
customer.setName(name);
customerService.add(customer);
return "Saved";
}
}
这里主要是注入服务层,然后,利用服务层实例,将get
请求过来的数据保存到数据库中。
尝试一波
再看mysql数据库里面的情况:
参考
- MySQL – Establishing SSL connection without server’s identity verification is not recommended
- Add context path to Spring Boot application
- Springboot 之 Hibernate自动建表(Mysql)
- Database Initialization
- Spring Boot in 6 minutes
- Spring Boot @RestController