spring boot实战阅读笔记

6.spring boot核心

6.1.2关闭特定的自动配置

如关闭特定的自动配置

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

6.1.6使用xml配置

特殊下必须使用xml配置时

@ImportResource({“classpath:some-context.xml”,”classpath:another-context.xml”})

6.2.2常规属性配置

在常规Spring环境下,注入properties文件里的值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入值。在Spring boot里,只需在application.properties定义属性,直接使用@Value注入即可。

 Publicclass Ch522Application { @Value(“${book.author}”) private String bookAuthor;}

6.2.3类型安全的配置

使用@Value注入每个配置在实际项目中会显得麻烦。可以通过@ConfigurationProperties将properties属性和一个bean及其属性关联,从而实现类型安全的配置

         author.name=wyf             author.age=32

         @Component

         @ConfigurationProperties(prefix=”author”)

         Publicclass AuthorSettings {

                   PrivateString name;        private Long age;  getter……setter……}

也可以新建一个properties文件,需要在@ConfigurationProperties的属性locations指定properties的位置,且需要在入口类上配置

@ConfigurationProperties(prefix=”author”,location={“classpath:config/author.properties”})

6.3日志级别

Spring boot默认使用logback作为日志框架

配置日志级别: logging.file=D:/mylog/log.log

配置日志文件,格式为logging.level.包名=级别;

logging.level.org.springframework.web=DEBUG  

6.4Profile配置

Profile是spring用来针对不同的环境对不同的配置提供支持的额,全局profile配置使用application-{profile}.properties(如application-prod.properties)

通过在application.properties中设置spring.profiles.active=prod来指定活动的profile

 

8.2 Spring Data JPA

Java Persistence API

2.使用Spring Data JPA建立数据访问层十分简单,只需定义一个继承JpaRepository的接口即可。

3.配置使用Spring Data JPA

在Spring环境中,使用Spring Data JPA可通过@EnableJpaRespositories注解来开启Spring Data JPA的支持,@EnableJpaRepositories接收的value参数用来扫描数据访问层所在包下的数据访问的接口定义

@Configuration

@EnableJpaRepositories(“com.wisely.repos”)

Public class JpaConfiguration {

         @Bean

PublicEntityManagerFactory entityManagerFactory(){ ……}

         //还需配置DataSource、PlatformTransactionManager等相关必须bean

}

4.定义查询方法

(1)根据属性名查询         1)常规查询。根据属性名来定义查询方法。这里使用了findBy、like、and这样的关键字

2)限制结果数量。结果是用top和first关键字来实现的。

                   List<Person>findFirst10ByName(String name);

(2)使用JPA的NamedQuery查询

         @Entity

         @NamedQuery(name=”Person.findByName”,query=”select p from Person p where p.name=?1”)

         Publicclass Person{   }

 

         Publicinterface PersonRepository extends JpaRepository<Person, Long> {

                   /这里使用的是NameQuery里定义的查询语句,而不是根据方法名称查询

                   List<person>findByName(String name);

         }

(3)使用@Query查询

         1)使用参数索引。SpringData JPA还支持用@Query注解在接口的方法上实现查询

         Publicinterface PersonRepository extends JpaRepository<Person,Long> {

                   @Query(“selectp from Person p where p.address=?1”)

                   List<Person>findByAddress(String address);

         }

         2)使用命名参数。上面的例子是使用参数的索引号来查询的,在Spring Data JPA里还支持在语句里用名称来匹配查询参数

         Publicinterface PersonRepository extends JpaRepository<Person, Long> {

                   @Query(“selectp from Person p where p.address = :address”)

                   List<Person>findByAddress(@Param(“address”) String address);

}

         3)更新查询。SpringData JPA支持@Modifying和@Query注解组合来事件更新查询

         Publicinterface PersonRepository extends JpaRepository<Person, Long>    {

                   @Modify

                   @Transactional

                   @Query(“updatePerson p set p.name=?1”)

                   IntsetName(String name)

         }

(4)Specification         基于准则查询的方式

(5)排序与分页         Page<Person> findByName(Stringname, Pageable pageable);

         2)使用排序List<Person> people = personRepository.findByName(“xx”, newSort(Direction.ASC,”age”));

         3)使用分页:Page<Person>peoples2=personRepository.findByName(“xx”, new PageRequest(0,10));

5.自定义Repository的实现

 

8.2.2 Spring boot的支持

         1.jdbc的自动配置

Spring-boot-starter-data-jpa依赖于spring-boot-starter-jdbc,而spring boot对JDBC做了一些自动配置。

通过”spring.datasource”为前缀的属性自动配置dataSource;spring boot自动开启了注解事务的支持(@EnableTransactionManagerment);还配置了一个jdbcTemplate。

Spring boot还提供了一个初始化数据的功能:放置在类路径下的schema.sql文件会自动用来初识化表结构;放置在类路径下的dada.sql文件会自动用来填充表结构

         2.对JPA的自动配置

Spring boot默认JPA的实现者是Hibernate;HibernateJpaAutoConfiguration依赖于DataSourceAutoConfiguration。  从JpaProperties的源码可以看出,配置JPA可以使用spring.jpa为前缀的属性在application.properties中配置。

         3.对Spring datajpa的自动配置

Spring boot自动开启了对spring data JPA的支持,即无须在配置类显示声明@EnableJpaResponsitories。

8.2.3 实战

打包Oracle的JDBC驱动到本地库

Mvn install:intall-flie –DgroupId=com.oracle“-DartifactId=ojdbc6” “-Dversion=11.2.0.2.0” “-Dpacking=jar” “-Dfile=E:\ojdbc6.jar”

         -DgroupId=com.orcle:指定当前包的groupId为com.orcle.

 

8.3 Spring Data REST

Spring data jpa是基于springdata的repository至少,可以将repository自动输出为REST资源。

Spring boot通过在application.properties中配置以”spring.data.rest”为前缀的属性来配置RepositoryRESTConfiguration。

 

8.4 声明式事务

使用了@EnableTransactionManagemen后,Spring容器会自动搜啊吗注解@Transactional的方法和类

8.4.5 Spring Data JPA的事务支持

Spring Data JPA对所有的默认方法都开启了事务支持,且查询类事务默认启用readOnly=true属性

8.4.6 Spring Boot的事务支持

8.5 数据缓存

8.5.1Spring缓存支持

         @Cacheable\@CachePut\@CacheEvit都有value属性,指定的是要使用的缓存名称;key属性指定的是数据在缓存中的存储的键。

如果没有指定key,则方法参数作为key保存到缓存中。

8.5.2 Spring Boot的支持

支持以”spring.cache”为前缀的属性来配置缓存。

在Spring Boot环境下,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在配置类中使用@EnableCaching开启缓存支持即可。

 

9.Spring Boot企业级开发


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值