这段时间需要用springboot+springDataJpa为基础配置一个项目后台。
这是项目部分依赖:
org.springframework.boot
spring-boot-starter-amqp
spring-boot-starter-logging
org.springframework.boot
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-data-solr
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.springframework.amqp
spring-rabbit-test
test
org.springframework.restdocs
spring-restdocs-mockmvc
test
com.alibaba
druid
1.1.10
org.springframework.boot
spring-boot-starter-log4j2
org.spockframework
spock-core
RELEASE
test
org.asciidoctor
asciidoctor-maven-plugin
1.5.8
generate-docs
prepare-package
process-asciidoc
html
book
org.springframework.restdocs
spring-restdocs-asciidoctor
${spring-restdocs.version}
org.springframework.boot
spring-boot-maven-plugin
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
jcenter
bintray
http://jcenter.bintray.com
然后写配置文件application.yml和dev.yml
server:
port: 47
spring:
profiles:
active: dev
jpa:
hibernate:
#自动建表
ddl-auto: update
#查看sql
show-sql: true
format_sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
properties:
show-sql: true
format_sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
main:
allow-bean-definition-overriding: true
logging:
config: classpath:config/log4j2.xml
spring:
rabbitmq:
addresses: 47.106.188.177
mvc:
view:
prefix: templates
suffix: .html
datasource:
username: carblog
password:
type: com.alibaba.druid.pool.DruidDataSource
# 配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall'用于防火墙
filters: stat, wall, log4j
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://url/carblog?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
logSlowSql: true
# 初始化
initialSize: 3
# 最大
maxActive: 20
# 最小
minIdle: 3
# 最大连接等待超时时间
maxWait: 60000
# 打开PSCache,并且指定每个连接PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
这样就直接可以启动了。
配置一下log4j,这里注意要将springboot内置的logging移除。
然后再配置对应log4j2.xml
这个配置文件可以直接进入Apache官网学习文档。
生成实体类:
package com.shengxi.carblog.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @Author: matthew
* @ClassName TestUser
* @Date: 2019-09-17 19:35
* @Version: 1.0.0
* @description: 实体类
* 注意要有无参构造器
* 其他构造器按需书写
*/
@Table(name ="test_user")
@Entity(name = "TestUser")
@DynamicUpdate
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
@EqualsAndHashCode
public class TestUser implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
}
然后我们可以写持久层,继承JpaRepository
package com.shengxi.carblog.repository;
import com.shengxi.carblog.pojo.TestUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* @Author: matthew
* @ClassName TestUserRepository
* @Date: 2019-09-17 19:35
* @Version: 1.0.0
* @description: 测试模板持久层
*/
@Repository
public interface TestUserRepository extends JpaRepository {
/**
* 在自定义方法查询中,
* 我们要使用new一个对象来封装实例
* 然后用对应的实体类来替代表
* 所以我们的实体类需要去绑定表
* 注意:在new的使用,实体类中要有对应的构造器
* @param name userName
* @return TestUser
*/
@Query("select new com.shengxi.carblog.pojo.TestUser(id, name, age) from TestUser where name = :name")
TestUser findUserByName(@Param("name") String name);
}