0. First recommend a tool--lombok,pom file as follows:
<Dependency> <groupId>Org.projectlombok</groupId> <Artifactid>Lombok</Artifactid> <Scope>Compile</Scope></Dependency>
You can use annotations @data compile-time to automatically generate Get,set methods, constructors, and ToString methods.
@Data @entity Public classaccount{@IdPrivateString ID; PrivateString account; @Column (Name= "Call_phone") PrivateString Phone; @Column (Name= "Nick_name") PrivateString Nickname; PrivateString password; PrivateString Salt; Private intusertype; PrivateString CreateUser; PrivateTimestamp Createtime; Private intState ;}
The resulting effect is as follows:
Add the following dependencies under the 1.pom.xml file, introduce the SPRING-BOOT-JPA jar package, and the MySQL driver
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-data-jpa</Artifactid></Dependency><Dependency> <groupId>Mysql</groupId> <Artifactid>Mysql-connector-java</Artifactid></Dependency>
2. Configuring Spring DataSource and Hibernate dialects in/src/main/resources/application.properties
(Spring boot defaults to use Hibernate as the JPA implementation)
spring.datasource.url=jdbc:mysql://localhost:3306/test?usessl=falsespring.datasource.username= Rootspring.datasource.password=Rootspring.datasource.tomcat.max-active=100 Spring.datasource.tomcat.max-idle=200spring.datasource.tomcat.initialSize=20 Spring.jpa.database-platform=org.hibernate.dialect.mysql5dialect
3. Define the entity and Repository (interface).
The Entity--account entity classes are as follows:
@Entity will be scanned and loaded by spring,
@Id annotations on the primary key
@Column name= "Call_phone" refers to the field name of the database that corresponds to the field, and does not need to be defined if the same. The database underline interval is considered the same as the Hump method in the code, such as database field Create_time equivalent to Createtime in the Java class, so @column annotations are not required.
@Data @entity Public classaccount{@IdPrivateString ID; PrivateString account; @Column (Name= "Call_phone") PrivateString Phone; @Column (Name= "Nick_name") PrivateString Nickname; PrivateString password; PrivateString Salt; Private intusertype; PrivateString CreateUser; PrivateTimestamp Createtime; Private intState ;}
Repository as follows:
@Repository Public Interface extends Jparepository<account, string>{account Findonebyaccount (String account);
4. Calling in other @component
@RestController @requestmapping ("/subsystem") Public class subsystemauthorityservice{ @Autowired accountrepository accountrepository; = "/admin/info") public String getadmininfo (string currentaccount) { = Accountrepository.findonebyaccount (currentaccount); SYSTEM.OUT.PRINTLN (account); return account.tostring (); }}
Spring boot access MySQL (JPA mode) The simplest configuration