Spring boo系列--jpa和thymeleaf

在Spring boot中使用Thymeleaf

在pom中加入spring-boot-starter-thymeleaf依赖.
但是要注意的是,在1.3之后的spring boot,需要匹配thymeleaf v3版本。需要在properties中添加一下版本属性:

    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

spring boot中使用sping jpa:

1. 在pom中加入spring-boot-starter-data-jpa依赖.然后再添加上对应数据库的连接驱动。
2. 在应用的application.yml或者properties配置文件中配置相应的连接配置
# ===============================
# = data source
# ===============================
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = root

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  1. 定义Model 类,使用 javax.persistence.Column, Table, Entity等注解
  2. 定义Repository类,可以实现JpaRepository接口,也可以实现CrudRepository接口。

不需要对应或者配置entityManagerFactory及entityManager实例bean了

这些Repository接口的关系如下:

JPA 中的Repository接口

                JpaRepository   <--------   QueryByExampleExecutor
                         -
                        /|\
                         |
            PagingAndSortingRepository     
                         -
                        /|\
                         |
                    CrudRepository
                         -
                        /|\
                         |                  
                    Repository

code:

public interface JpaRepository<T, ID extends Serializable>
        extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

    List<T> findAll();

    List<T> findAll(Sort sort);

    List<T> findAll(Iterable<ID> ids);

    <S extends T> List<S> save(Iterable<S> entities);

    /**
     * Flushes all pending changes to the database.
     */
    void flush();

    /**
     * Saves an entity and flushes changes instantly.
     */
    <S extends T> S saveAndFlush(S entity);

    /**
     * Deletes the given entities in a batch which means it will create a single {@link Query}. Assume that we will clear
     * the {@link javax.persistence.EntityManager} after the call.
     */
    void deleteInBatch(Iterable<T> entities);

    /**
     * Deletes all entities in a batch call.
     */
    void deleteAllInBatch();

    /**
     * Returns a reference to the entity with the given identifier.
     */
    T getOne(ID id);

    @Override
    <S extends T> List<S> findAll(Example<S> example);

    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);       
}       

public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {

    /**
     * Returns all entities sorted by the given options.
     */
    Iterable<T> findAll(Sort sort);

    /**
     * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
     */
    Page<T> findAll(Pageable pageable);
}


//CrudRepository中定义了
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
    /**
     * Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
     * entity instance completely.
     */ 
    <S extends T> S save(S entity);

    /**
     * Saves all given entities.
     */
    <S extends T> Iterable<S> save(Iterable<S> entities);

    /**
     * Retrieves an entity by its id.
     */
    T findOne(ID id);

    /**
     * Returns whether an entity with the given id exists.
     */
    boolean exists(ID id);

    /**
     * Returns all instances of the type.
     */
    Iterable<T> findAll();

    /**
     * Returns all instances of the type with the given IDs.
     */
    Iterable<T> findAll(Iterable<ID> ids);

    /**
     * Returns the number of entities available.
     */
    long count();

    /**
     * Deletes the entity with the given id.
     */
    void delete(ID id);

    /**
     * Deletes a given entity.
     */
    void delete(T entity);

    /**
     * Deletes the given entities.
     */
    void delete(Iterable<? extends T> entities);

    /**
     * Deletes all entities managed by the repository.
     */
    void deleteAll();
}


// Repository接口没有定义任何方法,只是一个占位符,用于声明一个Repository类,让spring来处理。
public interface Repository<T, ID extends Serializable> {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值