如果有整合jpa了, 那么在dao中直接, 不需要引入依赖

@Resource
private JdbcTempalte jdbcTempalte;

如果没有的话, 就先在pom.xml中加入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

编写dao声明为repository

package com.iwhere.test.dao;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.iwhere.test.demo.Demo; /**
* 使用jdbcTemplate操作数据库
* @author 231
*
*/
@Repository
public class JdbcDao { @Resource
private JdbcTemplate jdbcTemplate; /**
* 通过id获取对象
* @return
*/
public Demo getById(Integer id) {
String sql = "select id, name, sex from demo where id = ?";
BeanPropertyRowMapper<Demo> mapper = new BeanPropertyRowMapper<Demo>(Demo.class);
return jdbcTemplate.queryForObject(sql, mapper, id);
} }

之后即可在service和controller层进行调用了

更多操作方式

public List<AreaDto> getAreaDtos() {
String sql = "select * from t_area ";
return (List<AreaDto>) template.query(sql, new BeanPropertyRowMapper(AreaDto.class));
} public List<AreaDto> getAreaDtos1() {
String sql = "select * from t_area ";
return (List<AreaDto>) template.query(sql, new AreaRowMapper());
} public Map<String, Object> getBuilds() {
String sql = "select buildid,buildname from t_building ";
List<BuildingDto> list = this.template.query(sql, new BeanPropertyRowMapper(BuildingDto.class));
Map<String, Object> map = new HashMap<String, Object>();
for (BuildingDto build : list) {
map.put(String.valueOf(build.getBuildid()), build.getBuildname());
}
return map;
} public Map<String, Object> getFloors(String build) {
String sql = "select floorid ,floorname from t_floor where build_id=?";
List<FloorDto> list = this.template.query(sql, new Object[] { build }, new BeanPropertyRowMapper(FloorDto.class));
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (FloorDto floorDto : list) {
System.out.println(floorDto.getFloorid());
map.put(String.valueOf(floorDto.getFloorid()), floorDto.getFloorname());
}
return map;
} public Map<String, Object> getAreas(String build, String floor) {
String sql = "select areaid ,area_name from t_area where build_id=? and floor_id=?";
List<AreaDto> list = this.template.query(sql, new Object[] { build, floor }, new BeanPropertyRowMapper(AreaDto.class));
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (AreaDto areaDto : list) {
System.out.println(areaDto.getAreaid());
map.put(String.valueOf(areaDto.getAreaid()), areaDto.getArea_name());
}
return map;
} public List<StoreDto> getStoreDtos(int buildid, int floorid, int areaid) {
String sql = "select * from t_store where build_id=? and floor_id=? and area_id=?";
return (List<StoreDto>) template.query(sql, new Object[] { buildid, floorid, areaid }, new BeanPropertyRowMapper(
StoreDto.class));
} public List<BuildingDto> getBuildingDto() {
String sql = "select * from t_building ";
return (List<BuildingDto>) template.query(sql, new BeanPropertyRowMapper(BuildingDto.class));
} public List<FloorDto> getFloorDtos(int buildid) {
String sql = "select * from t_floor where build_id=? ";
return (List<FloorDto>) template.query(sql, new Object[] { buildid }, new BeanPropertyRowMapper(FloorDto.class));
} public List<AreaDto> getAreaDto(int buildid, int floorid) {
String sql = "select * from t_area where build_id=? and floor_id=?";
return (List<AreaDto>) template.query(sql, new Object[] { buildid, floorid }, new BeanPropertyRowMapper(AreaDto.class));
} public AreaDto getAreaDto(int id) {
String sql = "select * from t_area where areaid=?";
return template.queryForObject(sql, new Object[] { id }, new BeanPropertyRowMapper(AreaDto.class));
} public BuildingDto getBuildingDto(int buildId) {
String sql = "select * from t_building where buildid=?";
return template.queryForObject(sql, new Object[] { buildId }, new BeanPropertyRowMapper(BuildingDto.class));
} public FloorDto getFloorDto(int floorId) {
String sql = "select * from t_floor where floorid=?";
return template.queryForObject(sql, new Object[] { floorId }, new BeanPropertyRowMapper(FloorDto.class));
} public List<StoreDto> getAllStores() {
String sql = "select * from t_store ";
return (List<StoreDto>) template.query(sql, new BeanPropertyRowMapper(StoreDto.class));
} public String storeName(int storeId) {
String sql = "select storename from t_store where id=?";
return template.queryForObject(sql, new Object[] { storeId }, String.class);
} public StoreDto getStoreById(int storeInt) {
String sql = "select * from t_store where id=?";
return template.queryForObject(sql, new Object[] { storeInt }, new BeanPropertyRowMapper(StoreDto.class));
} public int getCountStore() {
String sql = "select count(id) from t_store";
return this.template.queryForInt(sql);
} public void saveBuild(BuildingDto buildingDto) {
} public void deleteBuildById(int buildid) {
String sql = "delete from t_store where id=?";
this.template.update(sql, buildid);
} public void updateBuildById(BuildingDto buildingDto) { }
} class AreaRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index) throws SQLException {
AreaDto areaDto = new AreaDto();
areaDto.setAreaid(rs.getInt("areaid"));
areaDto.setArea_name(rs.getString("area_name"));
areaDto.setArea_detail(rs.getString("area_detail"));
areaDto.setBuild_id(rs.getInt("build_id"));
areaDto.setFloor_id(rs.getInt("floor_id"));
areaDto.setInsert_time(rs.getTimestamp("insert_time"));
areaDto.setOperate_id(rs.getInt("operate_id"));
areaDto.setUpdate_time(rs.getTimestamp("update_time"));
areaDto.setRegion_name(rs.getString("region_name"));
areaDto.setSortno(rs.getString("sortno"));
return areaDto;
}

springboot-6-整合jdbc的更多相关文章

  1. SpringBoot 之 整合JDBC使用

    导入相关依赖: # pom.xml <dependency> <groupId>org.springframework.boot</groupId> <art ...

  2. SpringBoot 整合jdbc和mybatis

    摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...

  3. springboot之整合基本的jdbc并操作Mysql数据库

    对于数据访问层,无论是SQL还是NOSQL,springboot默认采用整合spring data方式进行统一处理,添加大量自动配置,屏蔽了许多设置,引入各种xxxTemplate,xxxReposi ...

  4. springboot整合JDBC出现Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.

    今天使用springboot整合JDBC的使用,开始使用的是 com.mysql.jdbc.Driver驱动 结果运行出现此异常 那我们根据提示要求来修改即可 把驱动改成最新的com.mysql.cj ...

  5. 3、SpringBoot整合之SpringBoot整合JDBC

    SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...

  6. SpringBoot整合jdbc及整合Druid数据源

    一.整合jdbc 1.创建一个springInitializr项目 勾选 web----springweb.SQL----JDBC API,MYSQL Diver 2.连接数据库 3.创建yml 4. ...

  7. 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕

    1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  8. springboot下整合各种配置文件

    本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等  以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...

  9. SpringBoot Druid整合,SpringBoot 集成Druid

    SpringBoot Druid整合,SpringBoot 集成Druid ================================ ©Copyright 蕃薯耀 2018年4月8日 http ...

  10. SpringBoot+SpringData 整合入门

    SpringData概述 SpringData :Spring的一个子项目.用于简化数据库访问,支持NoSQL和关系数据存储.其主要目标是使用数据库的访问变得方便快捷. SpringData 项目所支 ...

随机推荐

  1. DagScheduler 和 TaskScheduler

    DagScheduler 和 TaskScheduler 的任务交接 spark 调度器分为两个部分, 一个是 DagScheduler, 一个是 TaskScheduler, DagSchedule ...

  2. bootstrap collaspe

    和modal类似:也是通过data-toggle, data-target来指定: 可以把折叠插件改为手风琴,如果指定data-parent,那这个data-parent元素下的折叠插件就会像手风琴一 ...

  3. 理解ValueStack的基本机制 OGNL表达式

    ValueStack基础:OGNL(Object Graphic Navigatino Language) OGNL是Struts2中使用的一种表达式语言. 它可以用于,在JSP页面,使用标签方便的访 ...

  4. [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]

      Virus  We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...

  5. AbpZero的Swagger汉化之旅

    做汉化主要是为了出一份前后端都能看得懂的在线文档,废话不多说,我们开始准备, 我们要在启动项目的Startup.cs中重定向一下swagger的读取方式 1.在这个类下面,新增一个方法: public ...

  6. CS中窗体的基类(BaseForm)注意点

    窗体基类最好新建一个窗体(BaseForm) 1.这样能够保证在VS中保证他的派生窗口也能够可视化. 2.如果基类直接是一个cs类文件,对于处理派生窗口就很复杂,比如按钮权限之类的操作; 如果直接继承 ...

  7. Javascript/jQuery常用方法

    //字符串转成时间 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/ ...

  8. Day 22 面向对象知识.

    https://www.cnblogs.com/bigberg/p/7252349.html #类方法,静态方法, 属性方法. 类有两种作用:属性引用 和实例化.属性引用(类名.属性)class pe ...

  9. JavaScript基础函数和词法分析以及常用的内置对象和使用方法(4)

    day52 参考:https://www.cnblogs.com/liwenzhou/p/8004649.html 函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式 ...

  10. java 中什么是aop

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...