當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring boo系列--jpa和thymeleaf
生活随笔
收集整理的這篇文章主要介紹了
Spring boo系列--jpa和thymeleaf
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在Spring boot中使用Thymeleaf
在pom中加入spring-boot-starter-thymeleaf依賴.
但是要注意的是,在1.3之后的spring boot,需要匹配thymeleaf v3版本。需要在properties中添加一下版本屬性:
spring boot中使用sping jpa:
1. 在pom中加入spring-boot-starter-data-jpa依賴.然后再添加上對應(yīng)數(shù)據(jù)庫的連接驅(qū)動。 2. 在應(yīng)用的application.yml或者properties配置文件中配置相應(yīng)的連接配置 # =============================== # = 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不需要對應(yīng)或者配置entityManagerFactory及entityManager實例bean了
這些Repository接口的關(guān)系如下:
JPA 中的Repository接口
JpaRepository <-------- QueryByExampleExecutor- /|\|PagingAndSortingRepository - /|\|CrudRepository- /|\| Repositorycode:
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> { }總結(jié)
以上是生活随笔為你收集整理的Spring boo系列--jpa和thymeleaf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线充原理
- 下一篇: MySQL 中 AUTO_INCREME