Spring Boot学习之旅:(十五)使用JdbcTemplate

2024-08-22 22:38

本文主要是介绍Spring Boot学习之旅:(十五)使用JdbcTemplate,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在原有的基础上添加 mysql 连接和jpa 依赖

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

创建一个model

package com.wen.boot.model;import java.util.Date;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;import com.alibaba.fastjson.annotation.JSONField;@Entity
public class User {private int id;private String name;private int age;@JSONField(format="yyyy-mm-dd HH:mm")private Date createTime;private String remakes;/** serialize  是否需要序列化* @return the remakes*/@JSONField(serialize=false)public String getRemakes() {return remakes;}public void setRemakes(String remakes) {this.remakes = remakes;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}@Id@GeneratedValue(strategy=GenerationType.AUTO)public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

dao

public interface IUserDao {public User getUserById(int id);
}

dao 实现


@Repository("userDao")
public class UserDaoImpl implements IUserDao {@Resourceprivate JdbcTemplate jdbcTemplate;public User getUserById(int id) {String sql="select * from user where id=?";RowMapper<User> user=new BeanPropertyRowMapper<User>(User.class);return jdbcTemplate.queryForObject(sql,user,id);}
}

service

public interface IUserService {public User getUserById(int id);
}

service 实现

@Service
public class UserServiceImpl implements  IUserService{@Resource(name="userDao")private IUserDao userDao;   public User getUserById(int id) {return userDao.getUserById(id);}
}

controller

@RestController
@RequestMapping("/user")
public class HelloContreller {private Logger logger = LoggerFactory.getLogger(this.getClass());@Autowiredpublic IUserService userService;@GetMapping("/{id}")public User getUser(@PathVariable int id) {return userService.getUserById(id);}
}

启动工程 浏览器输入
http://localhost:8080/user/1
查看查询结果
这里写图片描述
本片文章只做整合JdbcTemplate 只做了简单的查询 如果还要实现其他的方法可以参考
http://www.cnblogs.com/heyongjun1997/p/5964256.html 这篇博客
文章地址:http://www.haha174.top/article/details/257327
项目源码: https://github.com/haha174/boot.git

欢迎关注,更多福利

这里写图片描述

这篇关于Spring Boot学习之旅:(十五)使用JdbcTemplate的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关