springboot数据库配置
来源:互联网 发布:java 使用一次失效 编辑:程序博客网 时间:2024/05/22 06:17
前面已经搭建了基本的springboot框架,下面是springboot数据库配置,使用oracle数据库,通过spring data方式访问。
我们以USER表为例。其中User表中包括pk_id 、name、state等字段
pom内容需要根据各自的数据库进行配置。我用的是ORACLE11g。利用bonecp方式
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc5</artifactId> <version>11.00</version></dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.7.1.RELEASE</version></dependency><dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-provider</artifactId> <version>0.7.1.RELEASE</version></dependency><dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.7.1.RELEASE</version></dependency>
application.properties
# **数据库基础信息配置**spring.datasource.type=com.jolbox.bonecp.BoneCPDataSourcespring.datasource.url=jdbc:oracle:thin:@10.224.15.15:1521:orclspring.datasource.username= testspring.datasource.password= testspring.datasource.driverClassName = oracle.jdbc.OracleDriver**#下面是datasource的属性配置**spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20# 配置获取连接等待超时的时间spring.datasource.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 #spring.datasource.filters=stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录#spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多个DruidDataSource的监控数据#spring.datasource.useGlobalDataSourceStat=true
下面创建一个TestDao的接口
@Repositorypublic interface TestDao extends JpaRepository<User, Long>,JpaSpecificationExecutor<User> { User findByName(String name); @Query("from User t where pkId = :id") List<User> queryMyList(@Param("id") Long id); List<User> findByState(String state,Pageable pageable) ; public List<User> findByCondition(String state) ;}
上面的4个类方法包含4种数据库访问的方法。
第一个findByName ,无需在实现类中实现这个方法,spring data会创建一个实现类实现。 name是我user表中的一个字段,他会根据你传的name值自动在user表中查找匹配name值
第二个queryMyList是@Query方式的方法
第三个和第一个一样,多了一个Pageable 参数
第四个是扩展方法,自己写一个类实现方法。创建TestDaoImpl类。这里应该是必须叫这个类名(我的理解是spring也会创建一个叫TestDaoImpl类实现接口,他会检查你是否创建了这个类,是否实现了接口中的方法,如果有他会把你实现的方法添加i进去)。 spring在创建实现类的时候会把你的实现类包含进去(我是这么理解的。。。)
编写TestDaoImpl类
注意: 这个类没有implements TestDao接口。。。
public class TestDaoImpl{ @PersistenceContext private EntityManager em ; public List<User> findByCondition(String state){ Query q = em.createNativeQuery("select * from user s where s.state = ?0",User.class).setParameter(0, "1") ; return q.getResultList() ; }}
可以看到实现类中只有一个findByCondition方法,其他方法不用实现。。
编写TestService接口及TestServiceImpl类
public interface TestService { public User findByName(String name); public List<User> findByState(String state,Pageable pageable) ; public List<User> findByCondition(String state) ; #queryMyList 自行实现}
@Service@Transactionalpublic class TestServiceImpl implements TestService { @Autowired private TestDao testDao; @Override public User findByName(String name) { return testDao.findByName(name) ; } @Override public List<User> findByState(String state,Pageable pageable) { return testDao.findByState(state, pageable) ; } public List<User> findByCondition(String state){ return testDao.findByCondition(state) ; }}
编写Controller测试
@Controllerpublic class TestController { @Autowired private TestService testService; /** * * @param name * @return */ @ResponseBody @RequestMapping("/spbt/getUser") public User findUser(String name){ return testService.findByName(name) ; } @ResponseBody @RequestMapping("/spbt/getState") public List<User> findByState(String state, @RequestParam(value="page",required=false, defaultValue="1") int page, @RequestParam(value="pageSize",required=false, defaultValue="10") int pageSize){ Order idOrder = new Order(Direction.DESC, "pkId"); Order nameOrder = new Order(Direction.DESC,"state"); Sort sort = new Sort(idOrder, nameOrder); PageRequest pr = new PageRequest(page,pageSize,sort) ; return testService.findByState(state, pr) ; } @ResponseBody @RequestMapping("/spbt/getCondition") public List<User> findByCondition(String state){ return testService.findByCondition(state) ; }}
访问http://localhost:8090/spbt/getCondition?state=1测试
- springboot配置数据库事务
- springboot数据库配置
- springboot-数据库配置
- SpringBoot数据库密码动态配置
- springboot配置内存数据库H2
- SpringBoot项目取消数据库配置
- Springboot项目配置myBatis连接MySQL数据库
- jAVA SpringBoot(2)---application.properties数据库配置
- springboot-数据库
- springboot配置
- springboot配置
- SpringBoot配置
- Springboot中使用数据库配置Quartz定时任务
- springboot数据库配置及传入中文不乱码
- 【SpringBoot】SpringBoot 连接mysql数据库
- 【SpringBoot】SpringBoot核心-基本配置
- 【SpringBoot】SpringBoot核心-外部配置
- SpringBoot-01SpringBoot配置FastJson
- springmvc试图解析流程
- Leetcode 1.Two Sum
- 问题备忘: It is indirectly referenced from required .class files
- 动态代理
- 水题
- springboot数据库配置
- 【Dockerfile】Ubuntu中Docker通过Dockerfile构建常用的基础环境
- 【NOIP2016提高A组模拟8.23】函数
- 利用WireShark分析H264码流
- hls之m3u8、ts、h264、AAC流格式详解
- java刷题之查漏补缺(四)
- PyCharm使用技巧:Column Selection Mode(列选择模式)
- MySQL基础(5)——全文本搜索
- 客户端跳转与服务器端跳转的区别