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
原创粉丝点击