Spring4-queryForObject-查詢行總數

1.創建項目,項目名稱(springdemo11),如圖所示

wKiom1jSDLTgy5EZAAAT0PISFGs727.png-wh_50


2.在項目中創建目錄(src->源碼目錄,test->測試目錄,source->配置文件目錄,lib->jar包目錄),如圖所示

wKioL1jSDOGRy3RdAAAtp63jcks991.png-wh_50


3.在lib中創建相應的jar包目錄,主要用於區分jar包.如圖所示

wKioL1jSDQmTr4voAAA6cy5kA7w740.png-wh_50


4.在lib的相應的jar包目錄中添加jar包.如圖所示

wKiom1jSDTrT89l0AACRnO8hJFA252.png-wh_50


5.在src目錄創建實體Bean Forum,包名(com.mycompany.shequ.bean),如圖所示

wKiom1jSDYaxyEm_AABBXpDeEco339.png-wh_50


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)如圖所示

wKioL1jSDdDCbaW5AABAxtiIbyA402.png-wh_50


8.接口ForumDao的內容如下

package com.mycompany.shequ.dao;


public interface ForumDao {
	public int findTotalCount();
}


9.在src目錄中創建ForumDao的實現類ForumDaoImpl,包名(com.mycompany.shequ.dao.impl),如圖所示

wKioL1jSDl_hlRS9AABEMplAp24264.png-wh_50


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 int findTotalCount() {
		String sql = "select count(fid) from hnsq_forum";
		int totalCount = getJdbcTemplate().queryForObject(sql,new Object[]{},Integer.class);
		return totalCount;
	}
}


11.在source目錄中創建配置文件spring-datasource.xml,如圖所示

wKioL1jSDtmy5uaFAAA3EYuGZas006.png-wh_50


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="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/b_shequ_two" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>
</beans>


13.在source目錄中創建配置文件applicationContext.xml,如圖所示

wKiom1jSDyvxJJxuAAA4DOwyBUE227.png-wh_50


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),如圖所示

wKioL1jSD2zCfNADAAA4ZZ8VKMY769.png-wh_50


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 testFindTotalCount(){
	    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		ForumDao forumDao = (ForumDao) context.getBean("forumDao");
		
		int totalCount = forumDao.findTotalCount();
		System.out.println(totalCount);
		
	}
}


17.運行測試類中的testFindTotalCount方法,運行結果如圖所示

wKiom1jSD8eTaNQyAAEdY2BAneo608.png-wh_50

wKiom1jUiA2hMkLhAABwCKMO5XE458.png-wh_50

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