本文参考 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 = update
#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.
#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.implicit-strategy= 这里使用默认
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") public class BsUser implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private String 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; public String getUUserId() { return uUserId; } public void setUUserId(String uUserId) { this.uUserId = uUserId; } public String getUName() { return uName; } public void setUName(String uName) { this.uName = uName; } public String getULoginName() { return uLoginName; } public void setULoginName(String uLoginName) { this.uLoginName = uLoginName; } public String getUPassWord() { return uPassWord; } public void setUPassWord(String uPassWord) { this.uPassWord = uPassWord; } public String getUSalt() { return uSalt; } public void setUSalt(String uSalt) { this.uSalt = 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()); } }