zoukankan      html  css  js  c++  java
  • spring jdbc

    spring JDBC

      加入对commons-dbcp  spring-jdbc  spring-tx的依赖

    1.数据源的配置  

      

    获取数据源在spring中的Bean管理默认已经是单例模式
    关闭数据源destroy-method="close"
    作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="$jdbc:mysql:///mydb"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </bean>

    不在配置中将数据源配置写死

    新建配置文件db.properties 

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.uril=jdbc:mysql:///mydb
    jdbc.username=root
    jdbc.password=root    

    在applicationContext添加配置

    <!-- 读取db.properties文件 使用${key}获取文件中的配置项 -->
        <context:property-placeholder location="db.properties"/>
        
        <!-- 数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>    
            

    2.JdbcTemplate配置,用于执行sql

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 需要构造方法注入一个数据源  ref是上面数据源的id -->
            <constructor-arg name="dataSource" ref="dataSource"/>
        </bean>        

    举例

    @Named
    public class StudentDao {
    
        @Inject/* 交给spring管理的类注入 ,属性名和id名最好一致 
            因为先是byName,找不到后byType*/
        private JdbcTemplate jdbcTemplate;
        
        public void save(Student stu) {
            String sql = "insert into student(name,address) values(?,?)";
            // 添加,删除,更新都是用update 
            jdbcTemplate.update(sql, stu.getName(),stu.getAddress());
        }
        
        public Student findById(int id) {
            String sql = "select * from student where id = ?";
            return jdbcTemplate.queryForObject(sql, new StudentRowMapper(), id);
        }
        
        public List<Student> findAll() {
            String sql = "select * from student";
            return jdbcTemplate.query(sql, new StudentRowMapper());
        }
        
        private class StudentRowMapper implements RowMapper<Student> {
    
            public Student mapRow(ResultSet rs, int arg1) throws SQLException {
                Student stu = new Student();
                stu.setAddress(rs.getString("address"));
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                return stu;
            }    
        }
    }        
            

    跟jdbcTemplate类似,NamedParameterJdbcTemplate也是用于执行sql,但是不常用

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
            <constructor-arg name="dataSource" ref="dataSource"/>
        </bean>    

    举例

    @Named
    public class StudentDao {
    
        @Inject
        private NamedParameterJdbcTemplate jdbcTemplate;
        
        public void save(Student stu) {
        // 引用占位符 
            String sql = "insert into student(name,address) values(:name,:address)";
            
            /* 本质上
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", stu.getName());
            map.put("address", stu.getAddress());
            jdbcTemplate.update(sql,map);*/
            
            
            /* 当:name,:address和Student中的属性名字相同时可以这样用*/
            //SqlParameterSource sps = new BeanPropertySqlParameterSource(stu);
            /* 即使:name,:address不相同,可以这样用        链式调用  */
            SqlParameterSource sps = new MapSqlParameterSource().addValue("name", stu.getName()).addValue("address", stu.getAddress());
            
            jdbcTemplate.update(sql,sps);
        }
        
        public Student findById(int id) {
            String sql = "select * from student where id = :id";
            
            /*本质上
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", id);
            return jdbcTemplate.queryForObject(sql,map,new StudentRowMapper());
            */
             
             
            //当:id和Student中的属性名字相同时可以这样用
            SqlParameterSource sps = new MapSqlParameterSource("id",id);
            
            
            return jdbcTemplate.queryForObject(sql,sps,new StudentRowMapper());
        }
        
        public List<Student> findAll() {
            String sql = "select * from student";
            // query必须要传进去一个map,这是构造方法规定的 
            return jdbcTemplate.query(sql,new HashMap<String, Object>(),new StudentRowMapper());
        }
        
        private class StudentRowMapper implements RowMapper<Student> {
    
            public Student mapRow(ResultSet rs, int arg1) throws SQLException {
                Student stu = new Student();
                stu.setAddress(rs.getString("address"));
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                return stu;
            }
        }
    }
  • 相关阅读:
    大数据下的质量体系建设
    快速打造属于你的接口自动化测试框架
    测试环境问题排查的那些事儿
    100个任务,用多机实现
    shell 在一个文件中查找数字
    shell中的awk使用
    shell怎么实现多进程
    删除字符串S1中的子串S2
    C++11的新特性
    C++里面普通指针怎么转换成智能指针
  • 原文地址:https://www.cnblogs.com/itliucheng/p/4465299.html
Copyright © 2011-2022 走看看