博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot整合JDBC数据库操作第三弹-实现增加数据操作
阅读量:5122 次
发布时间:2019-06-13

本文共 7056 字,大约阅读时间需要 23 分钟。

SpringBoot整合JDBC数据库操作第三弹-实现增加数据操作

上篇文章我们讲到了如何配置服务的DataSource数据库数据源,方便其对数据库进行操作.这篇文章主要讲解一下我们使用整合的JDBC进行简单的数据增加操作.

  • 创建用于做测试数据的模拟数据表

    USE test;CREATE TABLE article (  id          INT(10) AUTO_INCREMENT,  title       VARCHAR(100) NOT NULL,  description VARCHAR(200),  PRIMARY KEY (id));复制代码
  • 创建数据表对应实体类(在源码目录下新建bean文件夹, 并在该文件夹下创建相应的Article类文件)

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

    * http://www.apache.org/licenses/LICENSE-2.0 *

    * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.edurt.bean; /** * Article

    * 描述 : Article
    * 作者 : qianmoQ
    * 版本 : 1.0
    * 创建时间 : 2018-03-26 下午4:51
    * 联系作者 : qianmoQ */public class Article { private Integer id; private String title; private String description; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", description='" + description + '\'' + '}'; } }复制代码

  • 创建数据表底层数据库操作Repository(在源码目录下创建repository文件夹, 并在该目录下创建ArticleRepository类文件)

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

    * http://www.apache.org/licenses/LICENSE-2.0 *

    * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.edurt.repository; import com.edurt.bean.Article;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Component; /** * ArticleRepository

    * 描述 : ArticleRepository
    * 作者 : qianmoQ
    * 版本 : 1.0
    * 创建时间 : 2018-03-26 下午4:54
    * 联系作者 : qianmoQ */@Componentpublic class ArticleRepository { @Autowired private JdbcTemplate jdbcTemplate; // 引入数据库操作模型 /** * 创建文章信息 * * @param article 文章信息 */ public void createArticle(Article article) { String sql = "INSERT INTO article(title, description) VALUES(?, ?)"; jdbcTemplate.update(sql, article.getTitle(), article.getDescription()); } }复制代码

  • 创建数据库业务层Service (在源码目录下创建service文件夹, 并在该目录下创建ArticleService)

    package com.edurt.service;import com.edurt.bean.Article; public interface ArticleService {     void createArticle(Article article); }复制代码
  • 创建接口实现ArticleServiceImpl类文件

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

    * http://www.apache.org/licenses/LICENSE-2.0 *

    * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.edurt.service; import com.edurt.bean.Article;import com.edurt.repository.ArticleRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; /** * ArticleServiceImpl

    * 描述 : ArticleServiceImpl
    * 作者 : qianmoQ
    * 版本 : 1.0
    * 创建时间 : 2018-03-26 下午5:01
    * 联系作者 : qianmoQ */@Service(value = "articleService")public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleRepository articleRepository; @Override public void createArticle(Article article) { articleRepository.createArticle(article); } }复制代码

  • 创建API接口层Controller (在源码目录下创建controller文件夹, 并在该目录下创建ArticleController类文件)

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * 

    * http://www.apache.org/licenses/LICENSE-2.0 *

    * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.edurt.controller; import com.edurt.bean.Article;import com.edurt.service.ArticleService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController; /** * ArticleController

    * 描述 : ArticleController
    * 作者 : qianmoQ
    * 版本 : 1.0
    * 创建时间 : 2018-03-26 下午5:08
    * 联系作者 : qianmoQ */@RestController@RequestMapping(value = "article")public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping(value = "create", method = RequestMethod.POST) String create(@RequestBody Article article) { articleService.createArticle(article); return "SUCCESS"; } }复制代码

  • 使用RestAPI测试工具测试添加数据我用的是PostMan软件

  • 查看数据表是否已经写入该数据

转载于:https://juejin.im/post/5b319ab5e51d4558b0356dc0

你可能感兴趣的文章
[转]: 视图和表的区别和联系
查看>>
Regular Experssion
查看>>
图论例题1——NOIP2015信息传递
查看>>
uCOS-II中的任务切换-图解多种任务调度时机与问题
查看>>
CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)
查看>>
Android 官方新手指导教程
查看>>
幸运转盘v1.0 【附视频】我的Android原创处女作,请支持!
查看>>
UseIIS
查看>>
集合体系
查看>>
vi命令提示:Terminal too wide
查看>>
引用 移植Linux到s3c2410上
查看>>
MySQL5.7开多实例指导
查看>>
[51nod] 1199 Money out of Thin Air #线段树+DFS序
查看>>
poj1201 查分约束系统
查看>>
Red and Black(poj-1979)
查看>>
分布式锁的思路以及实现分析
查看>>
腾讯元对象存储之文件删除
查看>>
jdk环境变量配置
查看>>
安装 Express
查看>>
包含列的索引:SQL Server索引的阶梯级别5
查看>>