1.設置Pojo爲實體 @Entity //標識這個pojo是一個jpa實體 public class Users implements Serializable{ } java
2.設置表名@Entity@table(name=users)//指表名爲users public class Users implements Serializable{ } mysql
3.設置主鍵 public web
JPA 註解的幾個要點 spring
@Entitysql
1.設置Pojo爲實體 數據庫
public class Users implements Serializable {
} apache
2.設置表名 springboot
@Entity
@Table(name = "users") //指定表名爲users
public class Users implements Serializable {
} session
3.設置主鍵 數據結構
public class Users implements Serializable {
@Id
private String userCode;
4. 設置字段類型
經過@Column註解設置,包含的設置以下
name:字段名
.unique:是否惟一
.nullable:是否能夠爲空
.inserttable:是否能夠插入
.updateable:是否能夠更新
.columnDefinition: 定義建表時建立此列的DDL
.secondaryTable: 從表名。若是此列不建在主表上(默認建在主表),該屬性定義該列所在從表的名字。
<?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.xlei</groupId> <artifactId>springboot_jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_jpa</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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-data-jpa</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> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.xlei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootJpaApplication { public static void main(String[] args) { SpringApplication.run(SpringbootJpaApplication.class, args); } }
#創建/更新數據表的配置 spring.jpa.hibernate.ddl-auto=update #數據庫地址 spring.datasource.url=jdbc:mysql://localhost:3306/user #數據庫名 spring.datasource.username=root #數據庫密碼 spring.datasource.password=123456
這個時候其實已經能夠啓動springboot, 可是不會生成數據表,由於尚未配置實體類的jpa
package com.xlei.domain; import javax.persistence.*; /** * @Author:LeiXiao * @Description:SpringCloud * @Date:2018/1/30 */ @Entity //標明這個須要生成數據表類型 @Table(name = "users") public class User { @Id // 聲明一個策略通用生成器,name爲」system-uuid」,策略strategy爲」uuid」。 @GeneratedValue private Integer id; @Column(nullable = false) private String name; @Column(nullable = false) private Integer age; @Column(nullable = false) private Boolean sex; 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; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } }
這時候啓動項目,就會在指定位置下生成一個user數據表
CrudRepository是一個提供了普通增刪改查方法的接口,由spring內部提供,咱們只需調用便可
新建UserRepository.java
public interface UserRepository extends CrudRepository<User, String> { }
新建UserController.java
package com.xlei.controller; import com.xlei.dao.UserRepository; import com.xlei.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author:LeiXiao * @Description:SpringCloud * @Date:2018/1/30 */ @RestController //將users歸入到spring容器中 public class UsersController { //自動裝配 @Autowired private UserRepository userRepository; @RequestMapping("/list") public Iterable<User> list(){ Iterable<User> all = userRepository.findAll(); return all; } }