springboot整合数据源之jdbc,web项目操作数据库是常有之事。本篇介绍springboot如何用jdbc连接mariadb数据库。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mariadb.jdbcgroupId>
<artifactId>mariadb-java-clientartifactId>
dependency>
<dependency>
<groupId>com.hsy.springbootgroupId>
<artifactId>springboot-rpc-apiartifactId>
dependency>
<dependency>
<groupId>com.hsy.javagroupId>
<artifactId>java-exceptionartifactId>
dependency>
<dependency>
<groupId>com.hsy.javagroupId>
<artifactId>java-utilartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
dependency>
Exercise.java
public class TExerciseZone {
private Long id;
private Integer code;
private String name;
private Long parentId;
private Integer sortId;
private String remark;
//省略setter,getter等方法
}
@Repository("exerciseDao")
public class TExerciseZoneDaoImpl implements ITExerciseZoneDao{
private static final Logger _logger = LoggerFactory.getLogger(TExerciseZoneDaoImpl.class) ;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int insert(TExerciseZone exerciseZone) {
try{
return jdbcTemplate.update(insertSql,
exerciseZone.getCode(),exerciseZone.getName(),exerciseZone.getParentId(), exerciseZone.getSortId(),exerciseZone.getRemark());
}catch(Exception e){
_logger.error("出错信息:{}",e);
throw new DBHandleException(DBEnum.DB_INSERT_RESULT_ERROR,e) ;
}
}
@Override
public int[] batchInsert(List
@Service(value="exerciseZoneService")
public class TExerciseZoneServiceImpl implements ITExerciseZoneService{
@Autowired private ITExerciseZoneDao exerciseDao ;
@Override
public boolean createTExerciseZone(Integer code, String name, Long parentId, Integer sortId, String remark) {
TExerciseZone exerciseZone = new TExerciseZone() ;
exerciseZone.setCode(code);
exerciseZone.setName(name);
exerciseZone.setParentId(parentId);
exerciseZone.setSortId(sortId);
exerciseZone.setRemark(remark);
int insertCount = exerciseDao.insert(exerciseZone) ;
if(insertCount>0){
return true ;
}
return false;
}
@Override
public boolean bachtCreateTExcrciseZone(List list) {
List objArgs = new ArrayList<>() ;
for (int i=0;inew Object[]{} ;
objs[0] = list.get(i).getCode();
objs[1] = list.get(i).getName();
objs[2] = list.get(i).getParentId() ;
objs[3] = list.get(i).getRemark() ;
objs[4] = list.get(i).getSortId() ;
objArgs.add(objs) ;
}
if(exerciseDao.batchInsert(objArgs).length>0){
return true ;
}
return false;
}
@Override
public boolean deleteTExerciseZone(Long id) {
if(exerciseDao.delete(id)>0){
return true ;
}
return false;
}
@Override
public boolean batchDeleteTExerciseZone(List ids) {
List objArgs = new ArrayList<>() ;
for (int i = 0; i < ids.size(); i++) {
Long[] objs = new Long[]{} ;
objs[0] = ids.get(i);
objArgs.add(objs);
}
if(exerciseDao.batchDelete(objArgs).length>0){
return true ;
}
return false;
}
@Override
public boolean updateTExerciseZone(TExerciseZone exerciseZone) {
if (exerciseDao.update(exerciseZone)>0){
return true ;
}
return false;
}
@Override
public boolean batchUpdateTEerciseZone(List list) {
List objArgs = new ArrayList<>() ;
for (int i = 0; i < list.size(); i++) {
Object[] objs = new Object[]{} ;
objs[0] = list.get(i).getCode();
objs[1] = list.get(i).getName();
objs[2] = list.get(i).getParentId() ;
objs[3] = list.get(i).getRemark() ;
objs[4] = list.get(i).getSortId() ;
objs[5] = list.get(i).getId() ;
objArgs.add(objs) ;
}
if(exerciseDao.batchUpdate(objArgs).length>0){
return true ;
}
return false;
}
@Override
public TExerciseZone getOne(Long id) {
return exerciseDao.select(id) ;
}
@Override
public List getList(Integer offset, Integer limit) {
return exerciseDao.selectAll(offset,limit);
}
}
@Api(value = "省市区接口服务",description = "省市区crud操作")
@RestController
@RequestMapping("/api/rest")
public class RestfulController extends BaseController{
@Autowired private ITExerciseZoneService exerciseZoneService ;
@ApiOperation(value = "用户注册接口",tags = "对外提供用户注册接口")
@PostMapping(value = "/v1/reg",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean reg(@RequestParam(value = "code") Integer code, @RequestParam String name,
@RequestParam Long parentId, @RequestParam(required = false) Integer sortId, String remark){
return success(exerciseZoneService.createTExerciseZone(code, name, parentId, sortId, remark));
}
@ApiOperation(value = "批量注册",tags = "对外提供批量注册接口")
@PostMapping(value = "/v1/reg/batch",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean batchReg(@RequestParam String json){
JsonToBeanUtil.responseJsonToBeanEnhance(json, TExerciseZone.class) ;
return null;
}
@ApiOperation(value = "指定id删除书籍",tags = "对外提供根据id单个删除书籍")
//paramType 说明在路径当中
@ApiImplicitParam(name = "id",value = "book主键ID",required = true,dataType = "Long",paramType = "path")
@DeleteMapping(value = "/v1/delete/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean delete(@PathVariable Long id){
return success(exerciseZoneService.deleteTExerciseZone(id)) ;
}
@ApiOperation(value = "批量删除",tags = "批量删除服务")
@ApiImplicitParam(name = "ids",value = "主键IDlist集合",required = true,dataType = "List" ,example = "{1,2,3,4}")
@DeleteMapping(value = "/v1/delete/batch",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean batchDelete(List ids){
return success(exerciseZoneService.batchDeleteTExerciseZone(ids));
}
@ApiOperation(value = "指定id更新书籍",tags = "对外提供根据id单个更新书籍")
//paramType 说明在路径当中
@ApiImplicitParam(name = "id",value = "book主键ID",required = true,dataType = "Long",paramType = "path")
@PutMapping(value = "/v1/update/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean update(TExerciseZone exerciseZone){
return success(exerciseZoneService.updateTExerciseZone(exerciseZone));
}
@ApiOperation(value = "批量更新",tags = "批量更新服务")
@ApiImplicitParam(name = "list",value = "实体TExerciseZone list集合",required = true,dataType = "List" )
@PutMapping(value = "/v1/update/batch",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean batchUpdate(List list){
return success(exerciseZoneService.batchUpdateTEerciseZone(list));
}
@ApiOperation(value = "单个查询",tags = "查询单个书籍")
@ApiImplicitParam(name = "id",value = "book主键ID",required = true,dataType = "Long",paramType = "path")
@GetMapping(value = "/v1/zone/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseBodyBean getZone(@PathVariable Long id){
return success(exerciseZoneService.getOne(id)) ;
}
@ApiOperation(value = "批量查询",tags = "批量查询服务")
@ApiImplicitParams({
@ApiImplicitParam(name = "offset",value = "从第几个开始查询",dataType = "Integer"),
@ApiImplicitParam(name = "limit",value = "每页显示几个",dataType = "Integer")
})
@GetMapping(value = {"/v1/zones","/v1/zones/{offset}/{limit}"})
public ResponseBodyBean> getZones(@PathVariable(required = false) Integer offset,@PathVariable(required = false) Integer limit){
return success(exerciseZoneService.getList(offset,limit));
}
}
aplication.properties
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://192.168.216.129:3306/exercise?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root@mariadb
@SpringBootApplication
@ComponentScan(basePackages = "com.hsy.springboot.jdbc")
public class SpringBootJdbcApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootJdbcApplication.class,args) ;
}
}
用了swagger生成在线接口文档,不懂swagger的可以看我历史文章
由于不便详尽展现整个项目,再次附上源代码
springboot-jdbc
SpringBoot实战之入门
springboot实战之文章汇总
springboot实战之读取配置文件
springboot实战之整合jsp模版引擎
springboot实战之整合freemarker模版引擎
springboot实战之注册自定义Servlet
springboot实战之注册filter和listener
springboot实战之注册interceptor
springboot实战之整合slf4j日志系统
springboot实战之整合CommandLineRunner
springboot实战之整合restful工具swagger2