spring boot DAO之Hibernate
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,依赖 org.springframework.boot spring-boot-starter-data-jpa
千家信息网最后更新 2025年12月03日spring boot DAO之Hibernate
依赖
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java application-database.properties
#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc driver.默认通过uri来自动检测。spring.datasource.driver-class-name=com.mysql.jdbc.Driver#jdbc url.连接数据库的urispring.datasource.url=jdbc:mysql://172.28.1.227:3310/fc?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useServerPrepStmts=false#数据库连接用户名spring.datasource.username=fcdev#数据连接密码spring.datasource.password=123456#全限定名,连接池。默认自动检测classpathspring.datasource.type=com.zaxxer.hikari.HikariDataSource#sql脚本字符spring.datasource.sql-script-encoding=UTF-8#hibernate配置参数spring.jpa.hibernate.ddl-auto=nonespring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategyspring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategyspring.jpa.hibernate.use-new-id-generator-mappings=true源码-JpaProperties
@ConfigurationProperties(prefix = "spring.jpa")public class JpaProperties { /** * Additional native properties to set on the JPA provider. */ private Map properties = new HashMap<>(); /** * Mapping resources (equivalent to "mapping-file" entries in persistence.xml). */ private final List mappingResources = new ArrayList<>(); /** * Name of the target database to operate on, auto-detected by default. Can be * alternatively set using the "Database" enum. */ private String databasePlatform; /** * Target database to operate on, auto-detected by default. Can be alternatively set * using the "databasePlatform" property. */ private Database database; /** * Whether to initialize the schema on startup. */ private boolean generateDdl = false; /** * Whether to enable logging of SQL statements. */ private boolean showSql = false; /** * Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the * thread for the entire processing of the request. */ private Boolean openInView; private Hibernate hibernate = new Hibernate();} 源码-HibernateJpaAutoConfiguration
@Configuration@ConditionalOnClass({ LocalContainerEntityManagerFactoryBean.class, EntityManager.class })@Conditional(HibernateEntityManagerCondition.class)@EnableConfigurationProperties(JpaProperties.class)@AutoConfigureAfter({ DataSourceAutoConfiguration.class })@Import(HibernateJpaConfiguration.class)public class HibernateJpaAutoConfiguration { @Order(Ordered.HIGHEST_PRECEDENCE + 20) static class HibernateEntityManagerCondition extends SpringBootCondition { private static final String[] CLASS_NAMES = { "org.hibernate.ejb.HibernateEntityManager", "org.hibernate.jpa.HibernateEntityManager" }; @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConditionMessage.Builder message = ConditionMessage .forCondition("HibernateEntityManager"); for (String className : CLASS_NAMES) { if (ClassUtils.isPresent(className, context.getClassLoader())) { return ConditionOutcome .match(message.found("class").items(Style.QUOTE, className)); } } return ConditionOutcome.noMatch(message.didNotFind("class", "classes") .items(Style.QUOTE, Arrays.asList(CLASS_NAMES))); } }}SysMenu实体
@Entity@Table(name = "t_sys_menu")public class SysMenu { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column private String name; @Override public String toString() { return "id=" + id + ";name=" + name; }}TestHibernateDao
@org.springframework.stereotype.Repositorypublic interface TestHibernateDao extends JpaRepository { SysMenu getSysMenuById(Integer id);} AppContextTest
@RunWith(SpringRunner.class)@SpringBootTestpublic class AppContextTest { @Autowired private TestHibernateDao testHibernateDao; @Test public void hibernateTest(){ SysMenu sysMenu = testHibernateDao.getSysMenuById(1); System.out.println("menuName = " + sysMenu.getName()); }
数据
数据库
源码
检测
参数
字符
实体
密码
时候
用户
用户名
脚本
错误
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
南昌控议网络技术有限公司
网络安全打好三板斧
软件开发培训代理
浪潮服务器管理系统登录
软件开发项目验收表格
服务器安全的重要性
渭南软件开发网上价格
小浣熊百将传如何选服务器
网络安全目标价格表
数据库系统下慕课
东莞无限软件开发
DM广告设计软件开发
数据库.prg
陕西智慧城管软件开发电话
软件开发岗位
网络技术应用试题整理及答案
上海通讯网络技术供应
云服务器远程
网络安全中的无限循环
裕民县汇众网络技术
扶贫办网络安全应急预案
曲靖服务器租用加盟
吉林省欣诚网络技术有限公司
网络安全中3A指的是
使用QQ浏览器数据库
学校网络安全三年规划
青海惠普服务器维修
小酒窝网络技术有限公司
网络安全教育快闪视频
网络安全舆情报告