spring对JDBC的支持 学习笔记

来源:互联网 发布:亚马逊a9算法 2017 编辑:程序博客网 时间:2024/06/05 17:16

使用spring DAO 首先引用AOP jar  在事务管理的时候是基于AOP完成

引入Aopcorejdbcdbcp ,tx

1写一个实体类如Emp

public class Emp {
    private String empno;
    private String ename;
    private Double sal;
    public String getEmpno() {
        return empno;
    }
    public void setEmpno(String empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Double getSal() {
        return sal;
    }
    public void setSal(Double sal) {
        this.sal = sal;
    }
}
2写一个EmpDao
import java.util.List;
import core.entity.Emp;
public interface EmpDao {
    public void delete(Integer id);
    public void insert(Emp emp);
    public List<Emp>findAll();
    Emp findById(int empno);
}
3写一个EmpMapper需要自己写,继承一个RowMapper的类是将rs中的集合转换为Emp
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import core.entity.Emp;

public class EmpMapper implements RowMapper<Emp>{

    @Override
    public Emp mapRow(ResultSet rs, int arg1) throws SQLException {
        Emp emp = new Emp();
        emp.setEmpno(rs.getString("empno"));
        emp.setEname(rs.getString("ename"));
        emp.setSal(rs.getDouble("sal"));
        return emp;
    }
4写一个EmpDaoImpl
import core.entity.Emp;
@Repository
public class EmpDaoImpl implements EmpDao {
    @Autowired
    private JdbcTemplate template;//注入一个JdbcTemplate
    
    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }
    public JdbcTemplate getTemplate() {
        return template;
    }

    @Override
    public void delete(Integer id) {
        String sql ="delect from emp where id =?";
        Object[]obj ={id};
        template.update(sql, obj);

    }

    @Override
    public void insert(Emp emp) {
        Object obj[]={
                emp.getEmpno(),
                emp.getEname(),
                emp.getSal()
        };
        String sql = "insert into emp(empno,ename,sal) values(?,?,?)";
        template.update(sql,obj);    

    }
    @Override
    public List<Emp> findAll() {
        String sql ="select * from emp";
        List<Emp> list = template.query(sql, new EmpMapper());//EmpMapper需要自己写,继承一个RowMapper的类是将rs中的集合转换为Emp
        return list;
    }
    @Override
    public Emp findById(int id) {
        String sql ="select * from emp where id =?";
        Object[] obj ={id};
        Emp emp = (Emp) template.queryForObject(sql,obj, new EmpMapper());
        return emp;
    }
}
5配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">    
    <util:properties id="db" location="classpath:db.properties"></util:properties>
    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="core"></context:component-scan>
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username">
            <value>#{db.username}</value>
        </property>
        <property name="password">
            <value>#{db.password}</value>
        </property>
        <property name="url">
            <value>#{db.url}</value>
        </property>
        <property name="driverClassName">
            <value>#{db.driverClassName}</value>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 
     <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="ds"></property>
     </bean>
     <!--
     <bean id="empDao" class="core.dao.EmpDaoImpl">
         <property name="template" ref="template"></property>
     </bean>
      -->
</beans>
写一个测试类
public class FooTest {
    public static void main(String[] args) throws SQLException {    
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        EmpDaoImpl empdao = (EmpDaoImpl)ac.getBean("empDaoImpl");
        System.out.println(empdao.findById(1001));
    }
}



0 0
原创粉丝点击