博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc + springJdbc 实现一个简单的增删改查数据的Demo
阅读量:6528 次
发布时间:2019-06-24

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

前言

由于刚做 java 后台的时候, 项目使用的是 springboot ,这个相较于 springMVC 等框架自带 tomcat,配置更少更方便的框架, 所有对于 java 基础一些的 ssm 或者 ssh 框架知之甚少, 现在大多数公司的项目结构也都是 ssh 或是 ssm 开发,加上 springboot 虽然简单,但也就意味着自己能做的操作更少, 有这个其局限性, 所以就有了这篇随笔;

记录这篇随笔的目的在于:

      1、记录基于 maven 的 javaweb 工程的流程;

      2、简单整合 springjdbc 实现数据库的增删改查操作;

下面是正文

搭建 基于 maven 的 javaweb 工程, 基本流程如下:

首先新建一个 javaweb 工程,选择对应的 类型 按照要求填写好资料,最后就能得到一个结构如下的 javaweb 目录;

可以看到,新建的文件最外层有一个 pox.xml 文件, 这个文件主要作用是对项目需要的依赖进行管理,要使用 springmvc 和 springJdbc 就需要添加对应的 依赖, 如下:

4.0.0
...
...
war
1.0-SNAPSHOT
...
http://maven.apache.org
...
junit
junit
3.8.1
test
org.springframework
spring-context
4.0.2.RELEASE
org.springframework
spring-core
4.0.2.RELEASE
org.springframework
spring-beans
4.0.2.RELEASE
org.springframework
spring-web
4.0.2.RELEASE
org.springframework
spring-webmvc
4.0.2.RELEASE
org.springframework
spring-aop
4.0.2.RELEASE
org.springframework
spring-jdbc
4.0.2.RELEASE
mysql
mysql-connector-java
5.0.5
org.springframework
spring-aspects
4.0.2.RELEASE
commons-logging
commons-logging
1.2
org.springframework
spring-expression
4.0.2.RELEASE
org.springframework
spring-jdbc
5.0.2.RELEASE
org.codehaus.jackson
jackson-mapper-asl
1.9.11
org.codehaus.jackson
jackson-core-asl
1.9.11
javax.servlet
javax.servlet-api
3.1.0
pox 文件配置好了之后,接下来就是 web-inf 目录下的 web.xml 文件了 web.xml 文件可以指定 欢迎页面 、配置 servlet 以及配置一些 tomcat 启动时的能够读取的参数等;
Archetype Created Web Application
/index.jsp
springServlet
org.springframework.web.servlet.DispatcherServlet
1
springServlet
/
  
contextConfigLocation
classpath:applicationContext.xml
所以,很明显的,接下来就要探索 applicationContext.xml 页面了
  
  
  
classpath:jdbc.properties
  
  
  
jdbc.properties 文件就是以键值对记录的一些数据库连接参数
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=XXX jdbc.username=XXX jdbc.password=XXX 好了,配置文件基本就讲到这里,然后进入代码的编写 在java文件下依次建立如下目录

   首先,编写一个 controller 类 , 测试 javaweb 工程是否能够正常运行;

@RestController @RequestMapping(value = "/test") public class TestController {
@RequestMapping(value = "/sayHello",produces = "text/html;charset=UTF-8") public String testSay() {
return "hello springMVC + 你好"; } } 然后,配置一下 tomcat (这个必须要自己配,相较于 springboot 比较那啥) 第一步 配置 project structure

 

完成之后 , 正式进入 tomcat 配置

如上图,然后在 Deployment 中将 project structure 中配置的 artiFact 添加到 tomcat 中就 ok 了;

点击运行, 等启动之后 在浏览器之中输入 http://localhost:8080/test/sayHello 就可以访问到 上面写的 controller了;

 

配置部分基本就到这了,下面是 jdbc 部分, 基本就贴代码了

@RestController @RequestMapping(value = "/crud") public class CRUDController {
@Autowired private UserDAO userDao; @RequestMapping(value = "/select", produces = "application/json;charset=UTF-8") public RspBean insertSomething() {
Map
map = new HashMap
(); List
list = userDao.getSurveyQuest(); map.put("result", list); return RspBean.build(200, "请求成功", map); } @RequestMapping(value = "/insert", method = RequestMethod.POST) public RspBean insertSomething (@RequestBody Map bean) {
SurveyBean surveyBean = new SurveyBean(); surveyBean.setSurveyId(bean.get("surveyId").toString()); surveyBean.setSceneId(bean.get("sceneId").toString()); surveyBean.setQuestId(bean.get("requestId").toString()); surveyBean.setType(bean.get("type").toString()); surveyBean.setRequired(bean.get("required").toString()); surveyBean.setName(bean.get("name").toString()); surveyBean.setSeq(bean.get("seq").toString()); surveyBean.setCreateTime(bean.get("createTime").toString()); surveyBean.setVersion(bean.get("version").toString()); Map
result = new HashMap
(); result.put("result", userDao.insertSurveyQuest(surveyBean)); return RspBean.build(result); } @RequestMapping(value = "/update",method = RequestMethod.POST) public RspBean updateSomething (@RequestBody Map bean ) {
SurveyBean surveyBean = new SurveyBean(); surveyBean.setSurveyId(bean.get("surveyId").toString()); surveyBean.setSceneId(bean.get("sceneId").toString()); surveyBean.setQuestId(bean.get("requestId").toString()); surveyBean.setType(bean.get("type").toString()); surveyBean.setRequired(bean.get("required").toString()); surveyBean.setName(bean.get("name").toString()); surveyBean.setSeq(bean.get("seq").toString()); surveyBean.setCreateTime(bean.get("createTime").toString()); surveyBean.setVersion(bean.get("version").toString()); Integer result = userDao.updateOneSurveyData(surveyBean); Map
map = new HashMap
(); map.put("result", result); return RspBean.build(map); } @RequestMapping(value = "delete/{surveyId}", method = RequestMethod.GET) public RspBean deleteSomething (@PathVariable String surveyId) { Map
map = new HashMap
(); map.put("result", userDao.deleteOneSurveyData(surveyId)); return RspBean.build(map); } }
@Component public class UserDAO {
public JdbcTemplate jdbcTemplate; ClassPathXmlApplicationContext cpx = null; public List
getSurveyQuest() {
initClassPathXmlApplication(); List
mResult = new ArrayList
(); String sql = "SELECT quest_id 'questId'," + "scene_id 'sceneId', survey_id 'surveyId'," + "quest_name 'name', version 'version'," + "quest_seq 'seq', quest_type 'type'," + "required 'required', create_time 'createTime'" + "FROM abcd.survey_quest"; mResult = jdbcTemplate.query(sql, new RowMapper
() {
public SurveyBean mapRow(ResultSet resultSet, int i) throws SQLException {
SurveyBean bean = new SurveyBean(); bean.setQuestId(resultSet.getString("questId")); bean.setSceneId(resultSet.getString("sceneId")); bean.setSurveyId(resultSet.getString("surveyId")); bean.setName(resultSet.getString("name")); bean.setVersion(resultSet.getString("version")); bean.setSeq(resultSet.getString("seq")); bean.setType(resultSet.getString("type")); bean.setRequired(resultSet.getString("required")); bean.setCreateTime(resultSet.getString("createTime")); return bean; } }); return mResult; } public Integer insertSurveyQuest(SurveyBean bean) {
initClassPathXmlApplication(); String sql = "INSERT INTO abcd.survey_quest (quest_id, scene_id, survey_id, quest_name, version," + "quest_seq, quest_type, required, create_time) VALUES" + "('" + bean.getQuestId() + "','" + bean.getSceneId() + "','" + bean.getSurveyId() + "','" + bean.getName() + "','" + bean.getVersion() + "','" + bean.getSeq() + "','" + bean.getType() + "','" + bean.getRequired() + "','" + bean.getCreateTime() + "')"; int result = jdbcTemplate.update(sql); return result; } public Integer updateOneSurveyData(SurveyBean surveyBean) {
initClassPathXmlApplication(); String sql = "UPDATE abcd.survey_quest SET survey_id = '" + surveyBean.getSurveyId() + "',scene_id = '" + surveyBean.getSceneId() + "', quest_name = '" + surveyBean.getName() + "',version = '" + surveyBean.getVersion() + "', quest_seq = '" + surveyBean.getSeq() + "', quest_type = '" + surveyBean.getType() + "', required = '" + surveyBean.getRequired() + "', create_time = '" + surveyBean.getCreateTime() + "' WHERE quest_id = '" + surveyBean.getQuestId() + "'"; int result = jdbcTemplate.update(sql); return result; } public void initClassPathXmlApplication () {
if (cpx == null) {
cpx = new ClassPathXmlApplicationContext("applicationContext.xml"); jdbcTemplate = (JdbcTemplate) cpx.getBean("jdbcTemplate"); } } public Integer deleteOneSurveyData(String surveyId) {
initClassPathXmlApplication(); String sql = "DELETE FROM abcd.survey_quest WHERE survey_id = '" + surveyId + "'"; return jdbcTemplate.update(sql); } }
public class RspBean {
private Integer responseCode = 200; private String message = ""; private Map
response = null; private RspBean () {} public static RspBean build(Integer code, String message, Map data) {
RspBean rspBean = new RspBean(); rspBean.responseCode = code; rspBean.message = message; rspBean.response = data; return rspBean; } public static RspBean build(Map data) {
RspBean rspBean = new RspBean(); rspBean.response = data; return rspBean; } @Override public String toString() {
return "RspBean{" + "responseCode=" + responseCode + ", message='" + message + '\'' + ", response=" + response + '}'; } public Integer getResponseCode() {
return responseCode; } public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode; } public String getMessage() {
return message; } public void setMessage(String message) {
this.message = message; } public Map
getResponse() {
return response; } public void setResponse(Map
response) {
this.response = response; } }
public class SurveyBean {
public String questId; public String sceneId; public String surveyId; public String name; public String version; public String seq; public String type; public String required; public String createTime; @Override public String toString() {
return "SurveyBean{" + "questId='" + questId + '\'' + ", sceneId='" + sceneId + '\'' + ", surveyId='" + surveyId + '\'' + ", name='" + name + '\'' + ", version='" + version + '\'' + ", seq='" + seq + '\'' + ", type='" + type + '\'' + ", required='" + required + '\'' + ", createTime='" + createTime + '\'' + '}'; } public void setQuestId(String questId) {
this.questId = questId; } public void setSceneId(String sceneId) {
this.sceneId = sceneId; } public void setSurveyId(String surveyId) {
this.surveyId = surveyId; } public void setName(String name) {
this.name = name; } public void setVersion(String version) {
this.version = version; } public void setSeq(String seq) {
this.seq = seq; } public void setType(String type) {
this.type = type; } public void setRequired(String required) {
this.required = required; } public void setCreateTime(String createTime) {
this.createTime = createTime; } public String getQuestId() {
return questId; } public String getSceneId() {
return sceneId; } public String getSurveyId() {
return surveyId; } public String getName() {
return name; } public String getVersion() {
return version; } public String getSeq() {
return seq; } public String getType() {
return type; } public String getRequired() {
return required; } public String getCreateTime() {
return createTime; } }
 
 

转载于:https://www.cnblogs.com/hugystudy/p/8194125.html

你可能感兴趣的文章
洛谷.4180.[模板]次小生成树Tree(Kruskal LCA 倍增)
查看>>
TCL函数“参数自动补全” 与 “help 信息显示”
查看>>
POJ1050To the Max
查看>>
汇编基础--标识符、标号、伪指令和指令
查看>>
将公用文件夹从Exchange2010迁移到 Exchange 2013
查看>>
动态规划算法
查看>>
WebService学习总结(二)——WebService相关概念介绍
查看>>
泥鳅般的const(一个小Demo彻底搞清楚)
查看>>
Pyqt 打开外部链接的几种方法
查看>>
JavaScript DOM编程艺术学习笔记(一)
查看>>
event.srcElement获得引发事件的控件(表单)
查看>>
ASP.NET MVC铵钮Click后下载文件
查看>>
SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别
查看>>
基本数据结构 - 栈和队列
查看>>
Linux软中断、tasklet和工作队列
查看>>
如何解决ORA-28002 the password will expire within 7 days问题(密码快过期)
查看>>
Asp.Net Core 轻松学-利用日志监视进行服务遥测
查看>>
LightSwitch社区资源搜集
查看>>
Android通讯录查询篇--ContactsContract.Data 二(续)
查看>>
IT人的自我导向型学习:开篇杂谈
查看>>