springbootDAO之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.连接数据库的uri
spring.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
#全限定名,连接池。默认自动检测classpath
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#sql脚本字符
spring.datasource.sql-script-encoding=UTF-8
#hibernate配置参数
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
spring.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.Repository
public interface TestHibernateDao extends JpaRepository {
SysMenu getSysMenuById(Integer id);
}
AppContextTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppContextTest {
@Autowired
private TestHibernateDao testHibernateDao;
@Test
public void hibernateTest(){
SysMenu sysMenu = testHibernateDao.getSysMenuById(1);
System.out.println("menuName = " + sysMenu.getName());
}
当前名称:springbootDAO之Hibernate
新闻来源:http://pwwzsj.com/article/jecpph.html