Spring JdbcTemplate笔记

本文介绍如何使用Spring框架进行XML配置,包括组件扫描、属性占位符配置及数据源设置,并展示了如何通过JdbcTemplate操作数据库。同时,还提供了单元测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml配置文件


<context:annotation-config />  //加入注解
<context:component-scan base-package="foo.bar"/> //批量扫描该包下的类

<context:property-placeholder location="dp.properties"></context:property-placeholder>  //加载数据库密码等文件

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

//配置jdbcTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">//载入数据源
    <property name="dataSource" ref="dataSource"></property>

</bean>




测试类


public static void main(String[] args) {

   // ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //加载配置文件
 //  JdbcTemplate  jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate"); //获得数据库连接
   // String sql ="update items set name=? where id =?";
   // jdbcTemplate.update(sql,"tezhongbing",2); //根据数据库数据
    //HelloService helloService = context.getBean(HelloService.class);
   // System.out.println(helloService.sayHello());
  // String sql = "INSERT INTO items(name,price,detail,pic,createtime)values(?,?,?,?,?)"; //插入数据
  //  List<Object[]> batchArgs = new ArrayList<Object[]>();//插入多条数据
  //  batchArgs.add(new Object[]{"chen", 12, "tinghaode","a",new Date()});
  //  batchArgs.add(new Object[]{"jun", 14, "tinghaode", "b", new Date()});
  //  batchArgs.add(new Object[]{"wei", 34, "tinghaode", "c", new Date()});
  //  jdbcTemplate.batchUpdate(sql, batchArgs);

   // String sql ="SELECT id,name,price,pic,createtime createTime FROM items WHERE id =?";
    //RowMapper<Items> rowMapper =new BeanPropertyRowMapper<Items>(Items.class);
    //Items items = jdbcTemplate.queryForObject(sql,rowMapper,2);
    //System.out.println(items);

}

编写单测代码


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml") //用注解载入配置文件
public class SpringAppTests {
    @Autowired  //自动装载对象
    private HelloService helloService;

    @Autowired
    private ItemsDao itemsDao;

    @Test  //单测标记
    public void testSayHello() {
        Assert.assertEquals("Hello world!", helloService.sayHello());
    }

    @Test
    public void testInsert(){
       System.out.println(itemsDao.getcont(5));
    }
}


编写查询Dao

@Repository //注解注入
public class ItemsDao {

    @Autowired  //自动注入jdbcTemplate
    JdbcTemplate jdbcTemplate;

    public List<Items> getcont(Integer id){

        String sql ="SELECT id,name,price,pic,createtime createTime FROM items WHERE price ='12.0' " +
                "and id < ? ";
        RowMapper<Items> rowMapper =new BeanPropertyRowMapper<Items>(Items.class);
        List<Items> items = jdbcTemplate.query(sql,rowMapper,6);

        return  items;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值