Database using MySQL, ORM use spring data jpa
Thus a need to add the appropriate pom.xml file jar package. as follows:
<!-- 引入jap --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 引入mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2 Add application.properties configuration file. as follows:
spring.datasource.url=jdbc:mysql://localhost:3306/cfj_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= true
Note:
In fact, this role is mainly used hibernate.hbm2ddl.auto parameters: Automatically create | update | verify database table structure, there are four values:
the Create: hibernate once every table generated when the load will be deleted, and then depending on your model class re-generate a new table, even if the two do not have any such change should be implemented, which is a major cause of data loss database table.
create-drop: loading each model class-table according to hibernate, but sessionFactory a closed table is automatically deleted.
update: most commonly used properties, when you first load hibernate automatically set up according to the model class structure of the table (the premise is to set up a good database), automatically updates the table structure based on model class is loaded hibernate later, but even if the table structure changes there are still rows in a table does not delete the previous row. Note that when deployed to the server, the table structure will not be immediately established, it is to wait after the first application up and running before.
validate: Each time hibernate loaded, verified create a database table structure, and the only table in the database are compared, it does not create a new table, but will insert a new value.
dialect mainly generated table name designated storage engine is InneoDB
Show-SQL whether to print out the automatic production of SQL, convenient when debugging View
3 Add the entity class
package com.cfj.testboot.domain; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class UserDo implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue private Integer id; private String userName; private String passWord; public UserDo() { } public UserDo(String userName,String passWord) { this.userName = userName; this.passWord = passWord; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } @Override public String toString() { return "UserDo [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]"; } }
4 Create Dao (JPA commonly called Repository, such as UserRepository)
Almost without writing implementation class
Jpa inheritance corresponding interfaces can be. Such as inheritance: CrudRepository or PagingAndSortingRepository or JpaRepository or JpaSpecificationExecutor
package com.cfj.testboot.domain; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<UserDo, Integer>{ UserDo findByUserName(String userName);//按照名字查询 }
5 Test
We need to increase the jar package configuration in pom.xml. as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency>
Test classes are as follows:
package com.cfj.testboot.domain; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class UserRepositoryTests { @Autowired private UserRepository userRepository; @Test public void test() throws Exception { userRepository.save(new UserDo("aa1", "11")); userRepository.save(new UserDo("bb2", "22")); } @Test public void testFind() throws Exception { UserDo u = userRepository.findByUserName("aa1"); System.out.println(u.getUserName()); } }