SpringBoot 集成Spring-Data-JPA

一、使用的工具:

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>

项目截图如下:

包说明:

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

今天就分享到这里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值