SpringJDBC模板

使用JDBC模板

public class demo01Test {
    @Test
    public void test(){
        //连接spring自带连接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        //创建jdbc模板
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.update("insert into account values (null ,?,?)","张三",200);
    }

}

将连接池和jdbc模板交给spring管理

 <!--配置内置连接池-->
    <bean id="datasource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         IOC的灵活应用,因为类里有set方法用property,配置普通属性 
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
 <!--配置jdbc模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         IOC的灵活应用,配置对象属性
        <property name="dataSource" ref="datasource01"/>
    </bean>

配置DBCP,C3P0连接池

     
     配置dbcp连接池 
        <bean id="datasource02" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
     配置c3p0连接池 
    <bean id="datasource03" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverclass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

     配置jdbc模板 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         IOC的灵活应用,配置对象属性 
        <property name="dataSource" ref="datasource03"/>
    </bean>

在spring中引入配置属性文件

 <!--引入配置文件-->
     第一种方式,不常用 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="jdbc.properties"/>
    </bean>

     第二种方式 
    <context:property-placeholder location="classpath:jdbc.properties"/>

使用JDBC模板完成CRUD操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class demo02Test {
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

        增
    @Test
    public void test() {
        jdbcTemplate.update("insert into account values (null,?,?)", "水岸东方", 300);
    }
        删
    @Test
    public void delete(){
       jdbcTemplate.update("delete from account where id= ? ",6);
    }
        改
    @Test
    public void update(){
        jdbcTemplate.update("update account set name = ? where id= ? ","张无忌",1);
    }
        查
    @Test
    public void query(){
        String name = jdbcTemplate.queryForObject("select name from account where id= ? ", String.class, 1);
        System.out.println(name);
    }
        统计个数
    @Test
    public void count(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account",Long.class);
        System.out.println(count);
    }
        封装对象
    @Test
    public void query02(){
        Account account = jdbcTemplate.queryForObject("select * from account where id= ?", new MyRowMapper(), 2);
        System.out.println(account);
    }
    @Test
    public void query03() {
        List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
            System.out.println(list);

    }
    class MyRowMapper implements RowMapper<Account>{

        @Override
        public Account mapRow(ResultSet resultSet, int i) throws SQLException {
            Account account = new Account();
            account.setId(resultSet.getInt("id"));
            account.setMoney(resultSet.getDouble("money"));
            account.setName(resultSet.getString("name"));
            return account;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值