日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring boo系列--jpa和thymeleaf

發(fā)布時間:2023/12/15 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boo系列--jpa和thymeleaf 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在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依賴.然后再添加上對應(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
  • 定義Model 類,使用 javax.persistence.Column, Table, Entity等注解
  • 定義Repository類,可以實(shí)現(xiàn)JpaRepository接口,也可以實(shí)現(xiàn)CrudRepository接口。
  • 不需要對應(yīng)或者配置entityManagerFactory及entityManager實(shí)例bean了

    這些Repository接口的關(guān)系如下:

    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> { }

    總結(jié)

    以上是生活随笔為你收集整理的Spring boo系列--jpa和thymeleaf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。