SpringBoot学习笔记【二】-[SpringMvc+SpringTask+JPA集成开发]
程序员文章站
2022-06-13 19:50:41
...
一、SpringMvc
1. 新建HelloController.java
2.访问http://localhost:8080/hello?name=zjport
注意:
启动类一定要在Controller的包路径中,不能存在于同级的不同包中。
二、Spring Task
三、JPA访问数据库
1. pom文件增加
2. 编写DAO
3. application.properties增加
4. Controller添加测试方法
5. 访问 http://localhost:8080/access
1. 新建HelloController.java
package cn.gov.zjport.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String sayHello(String name) { return "hello ".concat(name); } }
2.访问http://localhost:8080/hello?name=zjport
注意:
启动类一定要在Controller的包路径中,不能存在于同级的不同包中。
二、Spring Task
package cn.gov.zjport.demo.job; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class TestJob { private Logger logger=LoggerFactory.getLogger(this.getClass()); @Scheduled(fixedRate = 10000) public void produce() { try { logger.info("test job run at "+ System.currentTimeMillis()); }catch(Exception e) { logger.error(e.getMessage(), e); } } }
三、JPA访问数据库
1. pom文件增加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4.0</version> </dependency>
2. 编写DAO
package cn.gov.zjport.demo.dao.impl; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import cn.gov.zjport.demo.dao.TestDao; @Repository public class TestDaoImpl implements TestDao{ @Resource private JdbcTemplate jdbcTemplate; public String getSysdate(){ return this.jdbcTemplate.queryForList("select to_char(sysdate,'yyyymmddHH24miss') as curTime from dual").get(0).get("curTime").toString(); } }
3. application.properties增加
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:zjport spring.datasource.username=test spring.datasource.password=test spring.datasource.max-idle=5 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5
4. Controller添加测试方法
package cn.gov.zjport.demo.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.gov.zjport.demo.dao.TestDao; @RestController public class HelloController { @Resource private TestDao testDao; @RequestMapping("/hello") public String sayHello(String name) { return "hello ".concat(name); } @RequestMapping("/access") public String access() { return "access at ".concat(testDao.getSysdate()); } }
5. 访问 http://localhost:8080/access
推荐阅读
-
C/C+学习笔记:10个免费的c/c+集成开发环境(IDE)
-
游戏开发学习笔记(二)创建游戏角色场景的制作
-
OpenCV 学习笔记(二)—— 构造 OpenCV-Python 开发环境
-
SpringBoot学习笔记9-SpringBoot集成Redis
-
Python 学习笔记(二)开发环境的搭建
-
《Swift2开发指南》第二版 学习笔记(Day 47)——final关键字
-
SpringBoot学习笔记【二】-[SpringMvc+SpringTask+JPA集成开发]
-
SpringBoot学习笔记【二】-[SpringMvc+SpringTask+JPA集成开发]
-
spring学习笔记(十九)springboot_web开发
-
SpringBoot入门学习笔记十(SpringBoot开发Web项目)