自己封裝的java db可以和spring 進行整合

在上篇博客自己封裝的db 實現一個投票系統中 http://blog.csdn.net/seemyname/article/details/46861743  僅僅是完成了簡單封裝

在這邊當中完成了和spring 進行整合 並且實現了bean 的映射


jar 包下載地址 http://download.csdn.net/detail/seemyname/8897717



以下指示給出關鍵代碼 如何和spring 進行整合以及api 使用方式


<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans      
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
                        http://www.springframework.org/schema/context      
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd      
                        http://www.springframework.org/schema/mvc      
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<context:component-scan base-package="com.zhucheng.web" />

	<mvc:annotation-driven />


	<!-- 引入配置文件 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:db.properties" />
	</bean>

	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${db.driver}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		<!-- 初始化連接大小 -->
		<property name="initialSize" value="${initialSize}"></property>
		<!-- 連接池最大數量 -->
		<property name="maxActive" value="${maxActive}"></property>
		<!-- 連接池最大空閒 -->
		<property name="maxIdle" value="${maxIdle}"></property>
		<!-- 連接池最小空閒 -->
		<property name="minIdle" value="${minIdle}"></property>
		<!-- 獲取連接最大等待時間 -->
		<property name="maxWait" value="${maxWait}"></property>
	</bean>
	
	<!-- 這裏引入jar 包定義的DBTemplate 就可以和spring 進行整合了簡單吧 -->
	<!-- 下面定義了DbUtils的一個模板操作Bean -->
	<bean id="dbTemplate" class="com.zhucheng.database.DBTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 視圖解釋類 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" /><!--可爲空,方便實現自已的依據擴展名來選擇視圖解釋類的邏輯 -->
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
	</bean>

	<!-- 對靜態資源文件的訪問 方案二 (二選一) -->
	<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" />
	<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
	<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />

</beans>  

這個也很簡單隻要繼承jar 包裏定義的DBTemplate 就可以了具體查看jar 包來學習吧

package com.zhucheng.web.dao;

import java.sql.SQLException;
import java.util.List;

import com.zhucheng.database.DBTemplate;
import com.zhucheng.database.ZCQuery;
import com.zhucheng.database.exception.SQLCloseException;
import com.zhucheng.database.handler.BeanListHandler;
import com.zhucheng.web.model.Cs;
import com.zhucheng.web.model.Stu;

public class StuDao extends DBTemplate {

	public List<Stu> list(int id) throws SQLException, SQLCloseException {
		ZCQuery zc = new ZCQuery(dataSource);
		List<Stu> stu = zc.query("select s.* from student s inner join class c on s.cid = c.id where c.id = ?",new BeanListHandler<Stu>(Stu.class),new Object[]{id});
		
		return stu;
	}
	
	public List<Cs> listcs() throws SQLException, SQLCloseException {
		ZCQuery zc = new ZCQuery(dataSource);
		List<Cs> stu = zc.query("select * from class",new BeanListHandler<Cs>(Cs.class));
		return stu;
	}
}

需要指出 增刪改 zc.update() 方法 查詢zc.query() 


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