spring基础学习五(Spring整合jdbc)

工程:

pojo类是:Tags

TagsRowMapper类:查询数据库的返回数据处理类


需要的ja包:




具体内容:

Tags:

package com.taobao.jdbc.app;

public class Tags {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}



JDBCTemplate类(里面是main函数):

package com.taobao.jdbc.app;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JDBCTemplate {

    private JdbcTemplate jdbcTemplate;
    
    
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Tags one(){
        Tags tag = (Tags)jdbcTemplate.queryForObject("select * from logo where id = ?",
                new Object[]{1},
                new int[]{java.sql.Types.INTEGER}, new TagsRowMapper());
        System.out.println(tag.getId());
        return tag;
    }
    public List<Tags> list(){
        if(jdbcTemplate == null){
            System.out.println("jdbcTemplate : null");
        }
        List<Tags> tags = (List<Tags>)jdbcTemplate.query("select * from logo", new TagsRowMapper());
        
        return tags;
    }
    public static void main(String[] args) {
        ApplicationContext actx = new ClassPathXmlApplicationContext("application.xml");
        JDBCTemplate jt = (JDBCTemplate)actx.getBean("jDBCTemplate");
        List<Tags> tags = jt.list();
        for(Tags t : tags){
            System.out.println(t.getId());
            System.out.println(t.getName());
        }

    }

}

其中有个 TagsRowMapper

package com.taobao.jdbc.app;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class TagsRowMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int index) throws SQLException {
		Tags tag = new Tags();
		tag.setId(rs.getInt("id"));
		tag.setName(rs.getString("picname"));
		return tag;
	}

}

这个函数是对查询数据库,返回一行(row)数据的处理



再就是配置文件的处理

config.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk&autoReconnect=true
username=root
password=root
application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<context:property-placeholder location="conf/config.properties"/>
    <bean id="springDao"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"
            value="${driverClassName}">
        </property>
        <property name="url"
            value="${url}">
        </property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <bean id="jdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="springDao" />
        </property>
    </bean>
    <bean id="jDBCTemplate" class="com.taobao.jdbc.app.JDBCTemplate">
    	<property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

其中这个配置文件中的头很重要,因为头的原因,一直不能执行。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值