springboot+jdbcTemplate的整合实现crud

2024-05-29 08:08

本文主要是介绍springboot+jdbcTemplate的整合实现crud,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 搭建

可以参考另一种方式:https://blog.csdn.net/u011066470/article/details/88086589

1.1 项目结构

1.2 pom文件

    <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency><!-- 以下是>spring boot依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- springboot-jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- springboot-mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>

1.3 controller

package com.ljf.spring.boot.demo.controller;import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName: UserController* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:31:48 * @Version: V1.0**/
@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/query")public Object query(String name){return  userService.queryUser(name);}
}

1.4 service

1.接口层

package com.ljf.spring.boot.demo.service;import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;public interface UserService {public UserDto queryUser(String name);
}

2.实现层

package com.ljf.spring.boot.demo.service.impl;import com.ljf.spring.boot.demo.dao.UserDao;
import com.ljf.spring.boot.demo.model.UserDto;
import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @ClassName: UserServiceImpl* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:37:56 * @Version: V1.0**/
@Service
public class UserServiceImpl implements UserService {@AutowiredUserDao userDao;@Overridepublic UserDto queryUser(String name) {return   userDao.getUserByUsername(name);}
}

1.5 dao层

package com.ljf.spring.boot.demo.dao;import com.ljf.spring.boot.demo.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;import java.util.List;/*** @ClassName: UserDao* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:33:06 * @Version: V1.0**/
@Repository
public class UserDao {@AutowiredJdbcTemplate jdbcTemplate;//根据账号查询用户信息public UserDto getUserByUsername(String username){String sql = "select id,username,password,fullname,mobile from t_user where username = ?";//连接数据库查询用户List<UserDto> list = jdbcTemplate.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(UserDto.class));if(list !=null && list.size()==1){return list.get(0);}return null;}
}

1.6 controller层

package com.ljf.spring.boot.demo.controller;import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @ClassName: UserController* @Description: TODO* @Author: liujianfu* @Date: 2021/08/18 09:31:48 * @Version: V1.0**/
@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/query")public Object query(String name){return  userService.queryUser(name);}
}

1.7 model层

package com.ljf.spring.boot.demo.model;/*** @author Administrator* @version 1.0**/
public class UserDto {private String id;private String username;private String password;private String fullname;private String mobile;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getFullname() {return fullname;}public void setFullname(String fullname) {this.fullname = fullname;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}@Overridepublic String toString() {return "UserDto{" +"id='" + id + '\'' +", username='" + username + '\'' +", password='" + password + '\'' +", fullname='" + fullname + '\'' +", mobile='" + mobile + '\'' +'}';}
}

1.8 启动类

package com.ljf.spring.boot.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App 
{public static void main( String[] args ){SpringApplication.run(App.class);System.out.println( "Hello World!" );System.out.println("springboot的template模块启动完成!!!");}
}

1.9 测试

这篇关于springboot+jdbcTemplate的整合实现crud的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013106

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解