在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
- 定义Model 类,使用 javax.persistence.Column, Table, Entity等注解
- 定义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> {
}