1. 程式人生 > >ibatis+springmvc的dao實現+配置檔案

ibatis+springmvc的dao實現+配置檔案

介面:

package com.cc.ovp.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;

import com.cc.ovp.domain.Host_setting;

public interface HostSettingDAO {

    /////新增域名
    public abstract void insertHost(Host_setting host_s) throws DataAccessException;

    ///根據userid修改域名
    public abstract void updateHostByUserid(Host_setting host_s);

    ///根據userid查詢域名
    public abstract List<Host_setting> selectHostByUserid(String userid) throws Exception;

    /////////根據userid修改域名白名單//////////////////
    public abstract void updateEnable_host(Host_setting host_s) throws Exception;

    /////////根據userid修改域名黑名單//////////////////
    public abstract void updateDisable_host(Host_setting host_s) throws Exception;

    /////////根據userid修改域名型別//////////////////
    public abstract void updateSetting_type(Host_setting host_s) throws Exception;

    //////////////////////////// 
    public abstract void deleteHost(Host_setting host) throws Exception;

}

實現類:
package com.cc.ovp.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.cc.ovp.dao.HostSettingDAO;
import com.cc.ovp.domain.Host_setting;



/**
 * 
 * @author Qixuan.Chen
 */

@Repository("hostSettingDAO")
public class HostSettingDAOImpl implements HostSettingDAO{
    
    @Resource(name = "sqlMapClientTemplate")
    private SqlMapClientTemplate sqlMapClientTemplate;
    public HostSettingDAOImpl(){}//無引數構造方法,防止ibatis的SQL執行出錯
    
    
    /////新增域名
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#insertHost(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void insertHost(Host_setting host_s) throws DataAccessException {
        sqlMapClientTemplate.insert("host_setting.insert", host_s);
        //host_setting.insert中的host_setting與ibatis的對映檔案的namespace的名稱一致,inster查詢語句ID
        System.out.println("資料插入成功!");
        
    }
    
    ///根據userid修改域名
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#updateHostByUserid(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void updateHostByUserid(Host_setting host_s){
        sqlMapClientTemplate.update("host_setting.updateByUserid", host_s);
    }
    
    ///根據userid查詢域名
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#selectHostByUserid(java.lang.String)
     */
    @Override
    public List<Host_setting> selectHostByUserid(String userid) throws Exception{
        @SuppressWarnings("unchecked")
        List<Host_setting> hostList = sqlMapClientTemplate.queryForList("host_setting.selectByUserid",userid);
        return hostList;
    }
    /////////根據userid修改域名白名單//////////////////
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#updateEnable_host(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void updateEnable_host(Host_setting host_s) throws Exception{
        sqlMapClientTemplate.update("host_setting.updateEnable_host",host_s);
    }
    /////////根據userid修改域名黑名單//////////////////
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#updateDisable_host(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void updateDisable_host(Host_setting host_s) throws Exception{
        System.out.println("DAO==========>"+host_s.getDisable_host());
        sqlMapClientTemplate.update("host_setting.updateDisable_host",host_s);
        System.out.println("更新成功!");
  
    }
    /////////根據userid修改域名型別//////////////////
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#updateSetting_type(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void updateSetting_type(Host_setting host_s) throws Exception{
        sqlMapClientTemplate.update("host_setting.updateSetting_type",host_s);
    }
    
   //////////////////////////// 
    /* (non-Javadoc)
     * @see com.cc.ovp.dao.impl.HostSettingDAO#deleteHost(com.cc.ovp.domain.Host_setting)
     */
    @Override
    public void deleteHost(Host_setting host)  throws Exception{
        sqlMapClientTemplate.delete("host_setting.deleteByUserid",host);
    }
}

對映檔案host_setting.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="host_setting">
	<typeAlias alias="host_setting" type="com.cc.ovp.domain.Host_setting" />
	
	 
	<sql id="_columns">
		userid, enable_host, disable_host, setting_type
	</sql>
	<!-- 新增域名 -->
	<insert id="insert" parameterClass="host_setting">
		insert into host_setting
		(userid,enable_host,disable_host,setting_type)
		values
		(#userid#, #enable_host#, #disable_host#, #setting_type#)
	</insert>
	
    <!-- 根據使用者ID查詢域名 -->
	<select id="selectByUserid" parameterClass="java.lang.String" resultClass="host_setting">
		select * from host_setting where userid = #userid#
	</select>
	
	 <!-- 根據域使用者ID修改域名白名單 -->
	<update id="updateEnable_host" parameterClass="host_setting">
	     update host_setting set enable_host=#enable_host# where userid=#userid#
	</update>
	
	<!-- 根據域使用者ID修改域名黑名單 -->
	<update id="updateDisable_host" parameterClass="host_setting">
	    update host_setting set disable_host=#disable_host# where userid=#userid#
	</update>
	
	<!-- 根據域使用者ID修改域名選擇型別 -->
	<update id="updateSetting_type" parameterClass="host_setting">
	   update host_setting set setting_type=#setting_type# where userid=#userid#
	</update>
	
	
  <!-- 根據域使用者ID修改域名選擇型別 -->
	<update id="updateByUserid" parameterClass="host_setting">
	   update host_setting set enable_host=#enable_host#,disable_host=#disable_host#,setting_type=#setting_type# where userid=#userid#
	</update>
	
</sqlMap>

ibatis全域性配置檔案sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig 
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings
		cacheModelsEnabled="false"
		enhancementEnabled="true"
		lazyLoadingEnabled="true"
		errorTracingEnabled="true"
		maxRequests="512"
		maxSessions="128"
		maxTransactions="32"
		useStatementNamespaces="true"
		defaultStatementTimeout="100"
		statementCachingEnabled="true"
		classInfoCacheEnabled="true"
	/>

    
    <!-- 互動視訊問答 -->
    <sqlMap resource="maps/videoExam.xml" />
    <sqlMap resource="maps/videoExamLog.xml" />
    
    <!-- 域名限制 -->
    <sqlMap resource="maps/host_setting.xml" />
    
    <!-- 多賬號 -->
    <sqlMap resource="maps/ccUserChildren.xml" />
    
    <!-- 問卷調查採集訪客資料 -->
    <sqlMap resource="maps/questionnaire.xml" />
    <sqlMap resource="maps/q_userInfoRecord.xml" />

</sqlMapConfig>