一、使用的工具:
1、JDK1.8
2、Eclipse
3、Maven
二、创建项目
1、创建SpringBoot项目
2、Maven 依赖配置如下:
<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>
<parent>
<groupId>com.zzg</groupId>
<artifactId>SpringLearn</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>JPADemo</artifactId>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--swagger setting-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<!-- spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringBoot 父类项目依赖配置文件如下:
<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.zzg</groupId>
<artifactId>SpringLearn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<modules>
<module>HikariCPDemo</module>
<module>JPADemo</module>
</modules>
</project>
项目截图如下:
包说明:
扫描二维码关注公众号,回复:
3363082 查看本文章
com.zzg.springboot.controller:控制层代码
com.zzg.springboot.entity:实体对象与数据库表对于关系
com.zzg.springboot.repository:dao层接口定义
com.zzg.springboot.service:service 接口定义
cong.zzg.springboot.service.impl:service 接口实现类
com.zzg.springboot.swagger:swagger 配置属性
四、代码实现
要求:基于mysql +jpa 构建简单的查询实例
4.1 entity 与table 对应关系
package com.zzg.springboot.entity;
import javax.persistence.*;
@Entity
@Table(name = "sys_user")
public class SysUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // id自动增长
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.2 、dao 层接口定义:基于主键ID查询
package com.zzg.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.zzg.springboot.entity.SysUser;
public interface SysUserRepository extends JpaRepository<SysUser, Long> {
}
4.3、service层接口定义和service层接口实现:基于主键ID查询
package com.zzg.springboot.service;
import com.zzg.springboot.entity.SysUser;
public interface SysUserService {
public SysUser findById(Long id);
}
package com.zzg.springboot.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzg.springboot.entity.SysUser;
import com.zzg.springboot.repository.SysUserRepository;
import com.zzg.springboot.service.SysUserService;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
private SysUserRepository repository;
public SysUser findById(Long id) {
// TODO Auto-generated method stub
return repository.findOne(id);
}
}
4.4、controller 层实现
package com.zzg.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzg.springboot.entity.SysUser;
import com.zzg.springboot.service.SysUserService;
@RestController
@RequestMapping("/jpa")
public class JPAController {
@Autowired
private SysUserService service;
@RequestMapping("/findById")
public void findById() {
SysUser bean = service.findById(1l);
System.out.println("name is :" + bean.getUsername());
System.out.println("password is :" + bean.getPassword());
}
}
4.5、swagger 配置属性
package com.zzg.springboot.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfigure {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zzg.springboot.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("Spring Boot Swagger2")
.termsOfServiceUrl("http://blog.csdn.net/zhouzhiwengang")
.contact("swagger")
.version("1.0")
.build();
}
}
4.6、程序入口
package com.zzg.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@EnableAutoConfiguration
@ComponentScan
@Configuration
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Application.class,args);
}
}
五、建库脚本(sys_user)
CREATE TABLE `sys_user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(200) NOT NULL COLLATE 'utf8mb4_general_ci',
`password` VARCHAR(200) NOT NULL COLLATE 'utf8mb4_general_ci',
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
今天就分享到这里。