Spring框架学习(3)spring中使用jdbc
内容源自:spring中使用jdbc
spring dao层中对jdbc进行了封装,使用模板模式的设计模式,通过ioc被动注入的方式将jdbcTemplate这个模板类注入到数据对象中,进行数据库操作。
我们要在一个类中进行CRUD操作(crud主要被用在描述软件系统中数据库或者持久层的基本操作功能。),首先要将jdbcTemplate这个模板类注入到数据对象类中,然后将DataSource这个类注入到jdbcTemplate,获取数据源。 这样数据对象类就可以通过jdbcTemplate类中的方法进行数据库操作了。
注意:这里需要导如spring jdbc的两个包和数据库驱动包
容器配置如下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 将CityDaoImpl、JdbcTemplate配置成ioc容器中的bean. --> <bean id="dao" class="com.etoak.dao.CityDaoImpl"> <property name="jt" ref="jt"></property> </bean> <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> <!-- 提供datasource数据源[接口] ~ bean spring 内置了一个dataSource实现类DriverManageDataSource --> </bean> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/yitu"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> </beans>
dao:
package com.etoak.dao; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.JdbcTemplate; import com.etoak.bean.City; /** * 使用jdbc方式对表进行CURD操作 * @author D_xiao * */ public class CityDaoImpl { private JdbcTemplate jt; public void setJt(JdbcTemplate jt) { this.jt = jt; } /** * JdbcTemplate 将连接数据库执行添加操作的流程 * 封装在update()中 * 增删改都是使用update方法 */ public boolean addCity(City city){ String sql =" insert into city values(null,?,?)"; Object[] args = {city.getPid(),city.getName()}; int result = jt.update(sql,args); //result 执行该操作影响的数据量 return result==1; //影响一条 则添加成功 } public boolean deleteCity(Integer id){ String sql = "delete from city where id="+id; int result = jt.update(sql,id); return result==1; } public boolean updateCity(City city){ String sql = "update city set pid=? , name=? where id = ?"; Object[] args = {city.getPid(),city.getName(),city.getId()}; int result = jt.update(sql,args); return result==1; } /**查询单条数据 * 在使用queryForMap()查询单条数据时, * 必须能够保证传入sql可以并且只能查询一条数据,否则会抛异常 */ public Map selectCityById(Integer id){ String sql ="select * from city where id="+id; Map map = jt.queryForMap(sql); //jdbc技术并非orm工具,并不能把直接查出来的关系型数据封装到对象,只能封装到map中 //key 字段名 value 字段值 return map; } /** * 查询批量数据 */ public List selectAllCitys(){ String sql = "select * from city"; List list = jt.queryForList(sql); return list; } /** * 查询数据量 */ public int selectCityCount(){ String sql = "select count(*) from city"; return jt.queryForInt(sql); } /** * 其他查询 */ public List selectCityByPage(int start,int end){ String sql = "select * from city limit ?,?"; Object[] args = {start,end}; return jt.queryForList(sql,args); } }
实体:
package com.etoak.bean; public class City { private Integer id; private Integer pid; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public City(Integer id, Integer pid, String name) { super(); this.id = id; this.pid = pid; this.name = name; } public City() { super(); } }
Spring框架学习(3)spring中使用jdbc的更多相关文章
- Spring框架学习03——Spring Bean 的详解
1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...
- Spring框架学习02——Spring IOC 详解
1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...
- Spring 框架学习(1)--Spring、Spring MVC扫盲
纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- Spring框架学习之IOC(二)
Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...
- Spring框架学习笔记(5)——Spring Boot创建与使用
Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...
随机推荐
- macos不能打开windows samba共享问题(转载)
转自:https://www.macx.cn/thread-2095377-1-1.html?mod=viewthread&tid=2095377&extra=page%253D1&a ...
- npm 安装或更新模块失败的解决办法
头一次关注npm,在刚刚安装的机子上使用更新指令却报错,我还以为是SHA512有什么问题,后来发现是因为一些网络原因,所以,如果出现错误,务必修改默认配置为国内镜像,但是在publish之前,记得要改 ...
- TYVJ2002 扑克牌
卢克生日那天,汉来找卢克玩扑克牌,玩着玩着汉觉得太没意思了,于是决定给卢克一个考验汉把一副扑克牌(54张)随机洗匀,倒扣着放成一摞.然后卢克从上往下一次翻开每张牌,每翻开一张黑桃,红桃,梅花或方块,就 ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 【拓展Lucas】模板
求\(C_n^m \mod p\),写得太丑了qwq. 第一次写拓展Lucas竟然是在胡策的时候qwq写了两个半小时啊_(:з」∠)还写挂了一个地方qwq 当然今天胡策我也是第一次写中国剩余定理(ˇˍ ...
- luogu P1012 拼数
题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...
- 插头dp练习
最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...
- 【树形dp】vijos P1180 选课
题解: http://www.cppblog.com/rakerichard/articles/105004.html 惊了,讨论子树大小能否dp真鸡儿麻烦,按照上面那份题解,可以不用分这么多类,可以 ...
- js知识-进阶
1 DOM 1.1 查找标签 (1)直接查找 document.getElementById(“idname”) // dom对象 document.getElementsByTag ...
- [转] 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
hibernate工作原理 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6 ...