springboot集成JdbcTemplate

JdbcTemplate为什么出现?
JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取PreparedStatement,设置SQL语句参数,关闭连接等步骤。造成操作冗余。影响我们打代码的效率。有了JDBCTemplate以后就可以只写SQL语句就可以了。
JdbcTemplate是什么?
JdbcTemplate是Spring的一部分,是对数据库的操作在jdbc的封装,处理了资源的建立和释放(不需要我们管理连接了),比如忘了总要关闭连接。他运行核心的JDBC工作流,如Statement的建立和执行,我们只需要提供SQL语句(不需要我们设置参数了)和提取结果(查询时候可以直接返回对应的实体类),使JDBC更加易于使用。可以将Spring的JdbcTemplate看作是一个小型的轻量级持久化层框架,和我们之前使用过的DBUtils风格非常接近。

1、引入依赖

<!-- jdbcTemplate依赖包 -->
	<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>

2、配置数据库连接信息

######################################数据库配置###################################
spring.datasource.url=jdbc:mysql://135.*.*.4:3306/xxl_job?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=**
spring.datasource.password=**

3、编写dao层接口

package qingxia.tang.jpa.dao;

import java.util.List;

import qingxia.tang.jpa.bean.Cat;

public interface CatDao {
    public List<Cat> getCatByNameTemplate(String name);
	
    public Cat getByAddressIdTemplate(Long id);

}

4、编写impl实现类

package qingxia.tang.jpa.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.dao.CatDao;
/**
 * @Repository和@Controller、@Service、@Component的作用差不多,都是把对象交给spring管理。
 * @Repository用在持久层的接口上,这个注解是将接口的一个实现类交给spring管理。
 * @author tangqingxia
 *
 */
@Repository
public class CatDaoImpl implements CatDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Cat> getCatByNameTemplate(String name) {
		String sql="select * from Cat t where t.name=?";
		List<Cat> query = jdbcTemplate.query(sql, new Object[]{name}, new BeanPropertyRowMapper(Cat.class));
		return query;
	}

	@Override
	public Cat getByAddressIdTemplate(Long id) {
		String sql="select * from Cat t where t.address_id=?";
		List<Cat> query = jdbcTemplate.query(sql, new Object[]{id}, new BeanPropertyRowMapper(Cat.class));
		if(query.size()>0){
			return query.get(0);
		}else{
		    return null;
		}
	}

}

5、编写service

package qingxia.tang.jpa.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.dao.CatDao;
import qingxia.tang.jpa.repository.CatDefineRepository;
import qingxia.tang.jpa.repository.CatRepository;



@Service
public class CatService {
	
	@Resource
	CatDao catDao;
	
    public List<Cat> getCatByNameTemplate(String name){
    	List<Cat> catByName = catDao.getCatByNameTemplate(name);
    	return catByName;
    }
	
    public Cat getByAddressIdTemplate(Long id){
    	Cat byAddressIdTemplate = catDao.getByAddressIdTemplate(id);
    	return byAddressIdTemplate;
    }

}

6、编写controller

package qingxia.tang.jpa.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import qingxia.tang.jpa.bean.Cat;
import qingxia.tang.jpa.service.CatService;

@RestController
public class CatController {
	
	@Resource
	CatService catService;
	
	/**
	 * http://localhost:8081/springBoot/getCatByNameTemplate?name=理科
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/getCatByNameTemplate",produces={"application/json"})
    public List<Cat> getCatByNameTemplate(String name){
    	List<Cat> catByNameTemplate = catService.getCatByNameTemplate(name);
    	return catByNameTemplate;
    }
	
	/**
	 * http://localhost:8081/springBoot/getByAddressIdTemplate?id=450
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getByAddressIdTemplate",produces={"application/json"})
    public Cat getByAddressIdTemplate(Long id){
    	Cat byAddressId = catService.getByAddressId(id);
    	return byAddressId;
    }

}

7、测试

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值