JPA 注解的几个要点
1.设置Pojo为实体 @Entity //标识这个pojo是一个jpa实体 public class Users implements Serializable{ }
2.设置表名@Entity@table(name=users)//指表名为users public class Users implements Serializable{ }
3.设置主键 public
JPA 注解的几个要点
@Entity
1.设置Pojo为实体
public class Users implements Serializable {
}
2.设置表名
@Entity
@Table(name = "users") //指定表名为users
public class Users implements Serializable {
}
3.设置主键
public class Users implements Serializable {
@Id
private String userCode;
4. 设置字段类型
通过@Column注解设置,包含的设置如下
name:字段名
.unique:是否唯一
.nullable:是否可以为空
.inserttable:是否可以插入
.updateable:是否可以更新
.columnDefinition: 定义建表时创建此列的DDL
.secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字。
1.创建新的maven项目
2. 添加必须的依赖
<?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>
3. 新建springboot启动类
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); } }
4. 在resources跟目录下新建application.properties
#建立/更新数据表的配置 spring.jpa.hibernate.ddl-auto=update #数据库地址 spring.datasource.url=jdbc:mysql://localhost:3306/user #数据库名 spring.datasource.username=root #数据库密码 spring.datasource.password=123456
- update:Hibernate根据给定的Entity结构改变数据库。
- create: 每次都会创建数据库,关闭时不会删除
- none: mysql的默认设置 , 不改变数据结构
- create-drop: 创建数据库,但是每次sessionFactory关闭后都会删除
5. 新建实体类User
这个时候其实已经可以启动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数据表
6. 实现CRUD
CrudRepository是一个提供了普通增删改查方法的接口,由spring内部提供,我们只需调用即可
新建UserRepository.java
public interface UserRepository extends CrudRepository<User, String> { }
7. 实现controller控制
新建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; } }