hibernate (combined springboot) Getting Started

Reference herein springboot integrate Hibernate: https://blog.csdn.net/u014745069/article/details/79940540

     hibernate naming strategy:  https://www.cnblogs.com/sxdcgaq8080/p/7910474.html

  • maven rely references
<!--        hibernate核心-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha3</version>
<type>pom</type>
</dependency>
<!-- mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>

<!-- Springboot 提供的 orm-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
  • application.properties configured to hibernate in

#hibernate
spring.datasource.url = jdbc: MySQL: //192.168.3.244:? 3306 / BaseManage to true useUnicode = & characterEncoding = UTF-8 & noAccessToProcedureBodies to true & allowMultiQueries = = = to true to true & useAffectedRows
spring.datasource.username = root
spring.datasource.password = root
the Spring -class-name = .datasource.driver com.mysql.cj.jdbc.Driver
spring.datasource.max-IDLE = 10
spring.datasource.max = the wait-10000
spring.datasource.min = IDLE. 5-
spring.datasource.initial 5 = -size
# the Specify the DBMS at the
spring.jpa.database = MYSQL
# show or not for the each log sql Query whether running sql debug
spring.jpa.show to true-sql =



# Hibernate DDL Auto (the Create, the Create-drop , Update)
# DDL-Auto: the Create ---- each time you run the program, will not form a new table, the data in the table will be empty
# ddl-auto: create-drop ---- the end of each program will clear the table
# ddl-auto: update ---- each time you run the program, will not form a new table, the data in the table is not empty, only update
# ddl-auto: validate ---- check data field types and databases running program is the same, different reports an error
spring.jpa.hibernate.ddl = Auto-update




# hibernate4 entity mapped to the data table time naming strategy
# using spring.jpa.hibernate.naming-strategy attribute
# two optional configuration:
# org.hibernate.cfg.DefaultNamingStrategy direct mapping, will not do too much to handle (the premise is not set @ Table, @ Column etc. when the property). If there are places @Column @Column whichever
# org.hibernate.cfg.ImprovedNamingStrategy table name, field to lowercase, uppercase letters when converted to the delimiter "_."
After # hibernate5, above the void. Instead of using the following two properties:
. Spring.jpa.hibernate.naming.implicit-Strategy # = #. 5 Implicit Naming the Hibernate Fully qualified name Strategy
# = # spring.jpa.hibernate.naming.physical the Hibernate Strategy. 5-PHYSICAL Naming Strategy fully qualified name.
# physical-strategy attribute
# org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl direct mapping, will not do too much to handle (the premise is not set @ Table, @ Column attributes such as time). If there are places @Column @Column whichever
# org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy table name, field to lowercase, uppercase letters when converted to the delimiter "_."
# implicit-strategy attribute
# org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl default naming strategy, compatible JPA 2.0 specification;
# org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl compatible with older versions of Hibernate naming convention;
# org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl compatible JPA 1.0 specification naming
# spring.jpa.hibernate.naming.implicit-strategy = herein using default
spring.jpa.hibernate.naming.physical-strategy = org.hibernate .boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

  • Repository
@Repository
public interface UserRepository extends JpaRepository<BsUser,Integer> {

    public BsUser save(BsUser user);

    @Query(value = "SELECT u FROM BsUser u  where  uLoginName=:loginName")
    public BsUser findloginName(@Param("loginName") String loginName);

}
  • Entity class

@Entity
@Table(name = "bs_user")
@Data
public class BsUser implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int uUserId;
@Column(name = "u_name")
private String uName;
@Column(name = "u_loginName")
private String uLoginName;
@Column(name = "u_passWord")
private String uPassWord;
@Column(name = "u_salt")
private String uSalt;

}
  • The main function
@SpringBootApplication
@EnableJpaRepositories
public class StudyApplication {
    public static void main(String[] args) {
        ApplicationContext context =   SpringApplication.run(StudyApplication.class, args);
        UserRepository userRpy = context.getBean(UserRepository.class);
        BsUser user= userRpy.findloginName("cyh");
        System.out.println("加密密码:"+user.getUPassWord());
    }

}

 

Guess you like

Origin www.cnblogs.com/cyh1282656849/p/12005399.html