Spring JdbcTemplate笔记

来源:互联网 发布:手机淘宝店铺上货教程 编辑:程序博客网 时间:2024/05/16 05:12

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;    }}


0 0
原创粉丝点击