SpringBoot 官方文档示例:(67)使用hsqldb
原创
©著作权归作者所有:来自51CTO博客作者wx62e0d796b5814的原创作品,请联系作者获取转载授权,否则将追究法律责任
一、添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
二、注入JdbcTemplate进行数据库操作(或通过MyBatis)
package cn.edu.tju.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/set")
public String set(){
jdbcTemplate.execute("create table user_table(x int,y int)");
jdbcTemplate.execute("insert into user_table values(6,88)");
return "set successfully";
}
@RequestMapping("/get")
public String get(){
Integer integer = jdbcTemplate.queryForObject("select y from user_table where x=? limit 1", new Object[]{6}, Integer.class);
return String.valueOf(integer);
}
}