spring基础学习五(Spring整合jdbc)
2011-12-16 17:29
543 查看
工程:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/19/fa0b2eaa5eb7a89840ecd3e099d3b5b3.gif)
pojo类是:Tags
TagsRowMapper类:查询数据库的返回数据处理类
需要的ja包:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/19/589ae0f24f75f2b803af41742d03c93a.gif)
具体内容:
Tags:
JDBCTemplate类(里面是main函数):
其中有个 TagsRowMapper
这个函数是对查询数据库,返回一行(row)数据的处理
再就是配置文件的处理
config.properties
其中这个配置文件中的头很重要,因为头的原因,一直不能执行。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/19/fa0b2eaa5eb7a89840ecd3e099d3b5b3.gif)
pojo类是:Tags
TagsRowMapper类:查询数据库的返回数据处理类
需要的ja包:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/19/589ae0f24f75f2b803af41742d03c93a.gif)
具体内容:
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=rootapplication.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>
其中这个配置文件中的头很重要,因为头的原因,一直不能执行。
相关文章推荐
- 学习sharding-jdbc 之spring+mybatis+sharding-jdbc整合
- Spring 学习 (六)Spring 整合JDBC
- Spring的基础学习(十二)——Spring与Junit的整合
- SSH与SSM学习之Spring18——Spring整合JDBC
- Spring学习5-Spring整合JDBC及其事务处理(注解方式)
- 框架学习之spring整合hibernate、struts2框架-01基础配置
- 【SSH】Spring学习(三)Spring整合JDBC、Spring中AOP事务
- 达内学习日志Day52:Spring整合JDBC
- Spring学习笔记之Spring与JDBC整合
- 【FreeMarker】基础学习和Spring整合使用
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(七)JDBC url的连接参数
- 学习sharding-jdbc(二)之spring+mybatis+sharding-jdbc整合
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(七)JDBC url的连接参数
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(七)JDBC url的连接参数
- JavaWeb 学习笔记 spring+jdbc整合开发初步
- 【spring基础】spring与jdbc整合详解
- Spring视频学习(八)整合JDBC
- 18.01.24,web学习第四十五天,还有半年,努力吧青年 Spring第三天 aop事务配置+Spring整合jdbc操作
- java 从零开始,学习笔记之基础入门<Struts2_Spring_整合>(四十一)
- Spring学习5-Spring整合JDBC及其事务处理(注解方式)