大家好,又见面了,我是你们的朋友全栈君。
1.引入依赖
下边是整个dao曾模块的pom文件,世界上JPA的以来只有一个,已经添加了注释
<?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>world-address-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>world-address-dao</name>
<description>Demo project for Spring Boot</description>
<parent>
<artifactId>world</artifactId>
<groupId>com.youyou</groupId>
<version>0.0.1-SNAPSHOT</version>
</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--这里是JPA的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置文件
#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#配置数据源
spring.datasource.url=jdbc:mysql://192.168.31.199:3306/world?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#配置自动建表:updata:没有表新建,有表更新操作,控制台显示建表语句
spring.jpa.hibernate.ddl-auto=update
#显示执行的sql
spring.jpa.show-sql=true
#显示参数
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
3.定义数据表实体
这里需要注意的是@Table注解中的参数是数据表的名字。
package com.youyou.address.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 数据库实体,联系人
*/
@Data
@Entity
@Table(name = "ts_contacter")
public class ContacterEO {
/**
* 主键
*/
@Id
private String id;
/**
* 姓名
*/
private String name;
/**
* 性别(0,女;1,男)
*/
private String sex;
/**
* 年龄
*/
private Integer age ;
/**
* 电话
*/
private String phone;
/**
* 地址
*/
private String location;
/**
* 删除(0,未删除;1,删除)
*/
private String dflag ;
}
4.继承JpaRepository接口
继承JpaRepository接口之后会继承过来一系列的增删改查的方法
package com.youyou.address.dao;
import com.youyou.address.entity.ContacterEO;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ContacterEORepo extends JpaRepository<ContacterEO , String> {
}
5.编写service类
这里只写了一个新增的方法用来测试。
package com.youyou.address.service;
import com.youyou.address.dao.ContacterDao;
import com.youyou.address.dao.ContacterEORepo;
import com.youyou.address.entity.ContacterEO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ContacterService {
@Autowired
private ContacterEORepo contacterEORepo;
/**
* 保存联系人
* @param contacterEO
*/
public void saveOne(ContacterEO contacterEO){
contacterEORepo.save(contacterEO);
}
}
6.controller层
package com.youyou.address.controller;
import com.youyou.address.entity.ContacterEO;
import com.youyou.address.service.ContacterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 java.util.List;
@Api(description = "联系人接口")
@RestController
@RequestMapping(value = "/contacter")
public class ContacterController {
@Autowired
private ContacterService service;
@ApiOperation(value = "新增")
@GetMapping("/add")
public ContacterEO add(ContacterEO contacterEO){
System.out.println("添加一个联系人");
service.saveOne(contacterEO);
return contacterEO;
}
}
触探spring-data-jpa到此结束。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106464.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...