This article introduces the example code of SpringBoot integrated JPA, and shares it with you, as follows:
1. Create a new maven project
2. Add necessary dependencies
<!--Springboot must depend on --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <!--Start springmvc related configuration, automatic configuration of springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependency> </dependencies>
3. Create a new springboot startup class
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}4. Create new application.properties in resources and directory
#Create/update the configuration of data table spring.jpa.hibernate.ddl-auto=update#Database address spring.datasource.url=jdbc:mysql://localhost:3306/qian?useUnicode=true&characterEncoding=utf-8#Database username spring.datasource.username=root#Database password spring.datasource.password=123
5. Create a new entity class User
At this time, springboot can actually be started, but the data table will not be generated because the jpa of the entity class has not been configured yet
Create new user.java first
import org.hibernate.annotations.GenericGenerator;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * Created by Andy on 2018/1/20. */// Indicates that this is a class that needs to generate data tables @Entitypublic class User {// Define the primary key id @Id// Declare a policy generic generator with name "system-uuid" and the strategy is "uuid". @GenericGenerator(name = "system-uuid", strategy = "uuid")// Use the generator attribute to specify the policy generator to use. @GeneratedValue(generator = "system-uuid") private String id; private String name; private Integer age; private Boolean sex; public String getId() { return id; } public void setId(String 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; }}At this time, the project will be started and a user data table will be generated in the specified location.
6. Implement CRUD
CrudRepository is an interface that provides ordinary methods of adding, deleting, modifying and searching. It is provided internally by spring. We only need to call it.
@NoRepositoryBeanpublic interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S var1); <S extends T> Iterable<S> save(Iterable<S> var1); T findOne(ID var1); boolean exists(ID var1); Iterable<T> findAll(); Iterable<T> findAll(Iterable<ID> var1); long count(); void delete(ID var1); void delete(T var1); void delete(Iterable<? extends T> var1); void deleteAll();}Create a new UserRepository.java
public interface UserRepository extends CrudRepository<User, String> {}7. Implement controller control
Create a new UserController.java
@RestControllerpublic class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/add") public User add(String name){ User user = new User(); user.setName(name); return userRepository.save(user); } @RequestMapping("/list") public Iterable<User> list(){ Iterable<User> all = userRepository.findAll(); return all; }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.