您的位置:首页 > 编程语言 > Java开发

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

2011-12-16 17:29 543 查看
工程:



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>


其中这个配置文件中的头很重要,因为头的原因,一直不能执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: