hibernate(结合springboot)入门
生活随笔
收集整理的這篇文章主要介紹了
hibernate(结合springboot)入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文參考 springboot整合hibernate:https://blog.csdn.net/u014745069/article/details/79940540
hibernate命名策略:https://www.cnblogs.com/sxdcgaq8080/p/7910474.html
maven依賴引用
<!-- 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中對hibernate的配置
#hibernate
spring.datasource.url=jdbc:mysql://192.168.3.244:3306/BaseManage?useUnicode=true&characterEncoding=utf-8&noAccessToProcedureBodies=true&allowMultiQueries=true&useAffectedRows=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query 是否顯示運行的sql 調試使用
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
#ddl-auto:create----每次運行該程序,沒有表格會新建表格,表內有數據會清空
#ddl-auto:create-drop----每次程序結束的時候會清空表
#ddl-auto:update----每次運行程序,沒有表格會新建表格,表內有數據不會清空,只會更新
#ddl-auto:validate----運行程序會校驗數據與數據庫的字段類型是否相同,不同會報錯
spring.jpa.hibernate.ddl-auto =validate
#hibernate4實體映射到數據表時候的命名策略
#使用 spring.jpa.hibernate.naming-strategy屬性
#兩個可選的配置:
#org.hibernate.cfg.DefaultNamingStrategy 直接映射,不會做過多的處理(前提沒有設置@Table,@Column等屬性的時候)。如果有@Column則以@Column為準
#org.hibernate.cfg.ImprovedNamingStrategy 表名,字段為小寫,當有大寫字母的時候會轉換為分隔符號“_”。
#hibernate5之后,上面的作廢。而是采用下面兩個屬性:
#spring.jpa.hibernate.naming.implicit-strategy= # Hibernate 5 implicit naming strategy fully qualified name.
#spring.jpa.hibernate.naming.physical-strategy= # Hibernate 5 physical naming strategy fully qualified name.
#spring.jpa.hibernate.naming.implicit-strategy= 這里使用默認
#physical-strategy屬性
#org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 直接映射,不會做過多的處理(前提沒有設置@Table,@Column等屬性的時候)。如果有@Column則以@Column為準
#org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy 表名,字段為小寫,當有大寫字母的時候會轉換為分隔符號“_”。
#implicit-strategy屬性
#org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl 默認的命名策略,兼容JPA 2.0的規范;
#org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl 兼容Hibernate老版本中的命名規范;
#org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl 兼容JPA 1.0規范中的命名規范
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
@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;
}
主函數
@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());
}
}
總結
以上是生活随笔為你收集整理的hibernate(结合springboot)入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 净值理财180天会亏吗?
- 下一篇: CPU时间片