Spring JdbcTemplate筆記

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章