Sample Code for integrating spring boot into JPA, springbootjpa

Source: Internet
Author: User

Sample Code for integrating spring boot into JPA, springbootjpa

This article introduces the sample code for integrating spring boot into JPA and shares it with you, as follows:

1. Create a New maven Project

2. Add required Dependencies

<! -- Springboot must be dependent on --> <parent> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 1.5.9.RELEASE </version> </parent> <dependencies> <! -- Start springmvc configuration and automatic spring boot configuration --> <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> </dependencies>

3. Create a 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 application. properties under resources and Directory

# Create/update the configuration of the 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
  1. Update: Hibernate changes the database based on the given Entity structure.
  2. Create: the database is created every time and will not be deleted when it is disabled.
  3. None: the default settings of mysql, without changing the Data Structure
  4. Create-drop: creates a database, but is deleted every time sessionFactory is closed.

5. Create an object class User

At this time, springboot can be started, but data tables are not generated, because jpa of the object class has not been configured.

Create user. java

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 the class @ Entitypublic class User that needs to generate the data table {// defines the primary key id @ Id // declares a general policy generator with name as "system-uuid ", the strategy is "uuid ". @ GenericGenerator (name = "system-uuid", strategy = "uuid") // use the generator attribute to specify the Policy generator to be used. @ 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 ;}}

When the project is started, a user data table is generated at the specified position.

6. Implement CRUD

CrudRepository is an interface that provides common addition, deletion, modification, and query methods. It is provided internally by spring and 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 UserRepository. java

public interface UserRepository extends CrudRepository<User, String> {}

7. implement controller control

Create 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 for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.