新建项目,在https://start.spring.io/ 中创建一个SprongBoot的项目,然后把它导入到你的开发工具中,如下图所示:
项目的整体布局如下所示:
image.png
项目的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>ni.ma</groupId>
<artifactId>yang</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>yang</name>
<description>project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
配置application.properties文件,在该文件中添加配置数据的信息,如下所示:
spring.jpa.show-sql = true
logging.level.org.springframework.data=DEBUG
spring.jpa.hibernate.ddl-auto=
spring.datasource.url=jdbc\:mysql\://10.**.***.**\:3306/yangnima?useSSL=false
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.tomcat.max-active=100
spring.datasource.tomcat.max-idle=200
spring.datasource.tomcat.initialSize=20
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
创建一个实体类TbText.java,代码如下:
package ni.ma.yang.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_text")
public class TbText implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID", nullable = false, updatable = false,length=11)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;//自增长ID
@Column(name = "name", nullable = true,length=15)
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
实现一个持久层服务,那么我只需要声明一个接口,这个接口继承Repository 接口就可以了。自己实现一个通过id查询得到实体,代码如下所示:
package ni.ma.yang.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ni.ma.yang.entity.TbText;
public interface TbTextRepository extends JpaRepository<TbText, Long> {
TbText findTbTextById(int id);
}
接下来就是验证了,创建一个TextController类,具体代码如下所示:
package ni.ma.yang.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ni.ma.yang.entity.TbText;
import ni.ma.yang.repository.TbTextRepository;
@RestController
@RequestMapping("/text")
public class TextController {
private static final Logger LOGGER = LoggerFactory.getLogger(TextController.class);
@Autowired
private TbTextRepository tbTextRepository;
@GetMapping("/findById")
public void findById(int id) {
TbText tb = tbTextRepository.findTbTextById(id);
LOGGER.info("*****************" + tb.getName() + "*****************");
}
}
在网页中输入:http://localhost:8080/text/findById?id=2 控制台输出如下所示:
*****************wanguniang*****************
去数据库中验证,当id是2时,name是wanguniang,正确。
image.png
网友评论