首页 > 代码库 > Spring4-JdbcDaoSupport-查询单列
Spring4-JdbcDaoSupport-查询单列
1.创建项目,项目名称(springdemo10),如图所示
2.在项目中创建目录(src->源码目录,test->测试目录,source->配置文件目录,lib->jar包目录),如图所示
3.在lib中创建相应的jar包目录,主要用于区分jar包.如图所示
4.在lib的相应的jar包目录中添加jar包.如图所示
5.在src目录创建实体Bean Forum,包名(com.mycompany.shequ.bean),如图所示
6.实体Bean Forum的内容如下
package com.mycompany.shequ.bean; public class Forum { private int fid; private String name; public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
7.在src目录创建接口ForumDao,包名(com.mycompany.shequ.dao)如图所示
8.接口ForumDao的内容如下
package com.mycompany.shequ.dao; public interface ForumDao { public String findNameById(int fid); }
9.在src目录中创建ForumDao的实现类ForumDaoImpl,包名(com.mycompany.shequ.dao.impl),如图所示
10.ForumDao的实现类ForumDaoImpl的内容如下
package com.mycompany.shequ.dao.impl; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.mycompany.shequ.dao.ForumDao; public class ForumDaoImpl extends JdbcDaoSupport implements ForumDao { @Override public String findNameById(int fid) { String sql = "select name from hnsq_forum where fid = ?"; String name = (String)getJdbcTemplate().queryForObject(sql, new Object[]{fid},String.class); return name; } }
11.在source目录中创建配置文件spring-datasource.xml,如图所示
12.配置文件spring-datasource.xml的内容如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver" /> <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/b_shequ_two" /> <property name="username" value="http://www.mamicode.com/root" /> <property name="password" value="" /> </bean> </beans>
13.在source目录中创建配置文件applicationContext.xml,如图所示
14.配置文件applicationContext.xml的内容如下
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="spring-datasource.xml" /> <bean id="forumDao" class="com.mycompany.shequ.dao.impl.ForumDaoImpl"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
15.在test目录中创建ForumDaoImplTest测试类,包名(com.mycompany.shequ.dao.impl),如图所示
16.ForumDaoImplTest测试类的内容如下
package com.mycompany.shequ.dao.impl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mycompany.shequ.dao.ForumDao; public class ForumDaoImplTest { @Test public void testFindNameById(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ForumDao forumDao = (ForumDao) context.getBean("forumDao"); String name = forumDao.findNameById(30); System.out.println(name); } }
17.运行测试类中的testFindNameById方法,运行结果如图所示
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1909161
Spring4-JdbcDaoSupport-查询单列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。