Spring框架学习(3)spring中使用jdbc
2018-04-30 19:32
344 查看
内容源自: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学习心得(22)-- spring声明式事务处理(使用jdbc操作数据库)
- Spring学习(四)spring中使用jdbc
- Spring框架笔记(二十四)——Spring中的JDBC的两种使用方式
- SpringBoot学习笔记之JDBC使用
- 学习《spring 3.x企业应用开发实战》之使用Spring JDBC访问数据库
- Spring入门学习笔记(4)——JDBC的使用
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
- [原创]java WEB学习笔记109:Spring学习---spring对JDBC的支持:使用 JdbcTemplate 查询数据库,简化 JDBC 模板查询,在 JDBC 模板中使用具名参数两种实现
- Spring框架学习(10)Spring中如何使用事务?
- Spring学习(四)spring中使用jdbc
- Spring框架学习笔记(9)——Spring对JDBC的支持
- 【Spring】在Spring框架下使用注解配置JDBC事务
- spring学习笔记5——Spring里使用JDBC
- ITCAST视频-Spring学习笔记(使用构造器装配属性)
- 使用Spring的jdbcTemplate进一步简化JDBC操作
- spring包使用详解 --- 传智播客spring视频学习自总结
- spring学习笔记: Spring Jdbc
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解