Spring学习(四)spring中使用jdbc
来源:互联网 发布:计算阶乘的程序java 编辑:程序博客网 时间:2024/06/05 15:59
spring dao层中对jdbc进行了封装,使用模板模式的设计模式,通过ioc被动注入的方式将jdbcTemplate这个模板类注入到数据对象中,进行数据库操作。
我们要在一个类中进行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(); }}
0 0
- Spring学习(四)spring中使用jdbc
- Spring学习(四)spring中使用jdbc
- Spring-Boot学习笔记(四)JDBC使用
- 【Spring学习笔记四】使用Spring整合jdbc
- spring中使用jdbc
- Spring中使用JDBC
- Spring中使用JDBC
- spring中使用jdbc
- Spring中使用Jdbc
- Spring in Action(八):Spring中使用JDBC
- 07-Spring-spring中使用jdbc
- 在spring中使用JDBC
- Spring中使用jdbc操作
- spring里使用JDBC(四)SimpleJdbcTemplate方式
- Spring学习(四)
- Spring学习(四)
- Spring学习(四)Jdbc连接池交个spring管理和事务操作
- 【SSH】Spring学习(三)Spring整合JDBC、Spring中AOP事务
- 算法提高 新建Microsoft Word文档
- pat-basic-1048-c语言
- USB
- Streams, pipes, and redirects
- android 屏幕home键
- Spring学习(四)spring中使用jdbc
- mindmanager7步创建思维导图
- 第3章 函数
- 第四章 ASP.NET中的线程与异步
- Java 多线程实例详解(二)
- verticle-align
- Hadoop中Map端shuffle过程及源码解析
- 欢迎使用CSDN-markdown编辑器
- sourceInsight如何进入某个头文件