先说springboot整合jpa
PA(java persistence api),它并不是一个框架,而是一组规范。其中,Hibernate就实现了这个规范,而且是相当成功的(其实TopLink和OpenJPA也都实现了JPA规范,不过它们被Hinernate的光环笼罩了)。所以呢,当我们说到JPA的时候,好多人首先想到的就是Hibernate。
一,创建工程时把依赖添加上
二,配置yml文件
########springboot整合jpa配置
spring:
devtools:
restart:
enabled: false
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
hikari: # springboot 2.0 整合了hikari ,据说这是目前性能最好的java数据库连接池
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update # 第一次建表create 后面用update,要不然每次重启都会新建表
show-sql: true
logging:
level:
cn.com.lc.dao: debug #sql日志打印
三,工程目录,以及各个包下的类代码
1,entity包下的User (使用jpa后自动创建表)
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Data
@Entity
public class User {
@Id
@GeneratedValue
private long id;
private String name;
}
2,dao包下的UserDao(继承JpaRepository接口后 有很多可用的方法可以拿来直接用 这里用findAll()方法测试)
import com.lc.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface UserDao extends JpaRepository<User, Long> {
@Override
List<User> findAllById(Iterable<Long> iterable);
@Override
List<User> findAll();
}
3,service包下的UserService接口
public interface UserService {
List<User>findAll();
}
实现类UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
}
4,controller包下的UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public List <User>findAll(){
return userService.findAll();
}
}
在浏览器输入:http://localhost:8055/list 验证
接下来开始配置打印sql日志:
在原有的pom.xml上加上这个依赖
<!--监控sql日志-->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
在resouce新增 log4jdbc.log4j2.properties 配置文件:
# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
重新配置yml文件
#########Springboot JPA日志输出打印SQL语句和传入的参数 配置
#服务端口
server:
port: 8055
spring:
datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
# url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
url: jdbc:log4jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
hikari: # springboot 2.0 整合了hikari ,据说这是目前性能最好的java数据库连接池
username: root
password: 123456
minimum-idle: 5 # 最小空闲连接数量
idle-timeout: 180000 # 空闲连接存活最大时间,默认600000(10分钟)
maximum-pool-size: 10 # 连接池最大连接数,默认是10
auto-commit: true # 此属性控制从池返回的连接的默认自动提交行为,默认值:true
pool-name: MyHikariCP # 连接池名称
max-lifetime: 1800000 # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
connection-timeout: 30000 # 数据库连接超时时间,默认30秒,即30000
# connection-test-query: SELECT 1 #连接池每分配一条连接前执行的查询语句(如:SELECT 1),以验证该连接是否是有效的。如果你的驱动程序支持 JDBC4,HikariCP 强烈建议我们不要设置此属性
jpa:
hibernate:
ddl-auto: update #
show-sql: true #
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect #设置数据库方言 记住必须要使用 MySQL5InnoDBDialect 指定数据库类型对应InnoDB ;如果使用MySQLDialect 则对应的是MyISAM
logging:
level:
cn.com.lc.dao: debug #sql日志打印
test-key: JCccc-test-key-one
添加上这个配置后,在查询sql语句的时候会打印sql语句
测试一下:http://localhost:8055/list
控制台显示情况:
显示这条sql语句成功执行
控制台查询结果
到这就执行完成了,源码获取地址:https://github.com/wen521/springboot-jpa-sqlGenertaed