普通类中调用service注解类中的方法

1,applicationContext.xml配置文件中:

    sqlSession配置:

     <bean id="sqlSession"  class="org.mybatis.spring.SqlSessionTemplate">   
<constructor-arg index="0" ref="sqlSessionFactory" />  

    </bean> 

    sqlSessionFactory配置:

    <!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />

   <property name="configLocation">  

        <!-- 指定mybaties路径 --!>

            <value>classpath:com/mer/sqlmapper/sqlMapConfig.xml</value>  
        </property>  

</bean>

    dataSource数据源配置:

    <!--创建jdbc数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />

<!-- 连接池中可同时连接的最大的连接数 -->
<property name="maxActive" value="20" />
<!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。单位是毫秒 -->
<property name="maxWait" value="20000"></property>
<!-- validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条数据的SELECT语句。 -->
<property name="validationQuery" value="SELECT 1" />
<!-- 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个. -->
  <property name="testOnBorrow" value="true"/>
  <!-- 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.  -->
  <property name="testWhileIdle" value="true"></property>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="3600000"></property>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="120000"></property>
<!--  在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->  
<property name="numTestsPerEvictionRun" value="50"></property>
<!--removeAbandoned: 是否自动回收超时连接-->    
<property name="removeAbandoned" value="true"/>
<!--removeAbandonedTimeout: 超时时间(以秒数为单位)-->
<property name="removeAbandonedTimeout" value="600"/>

</bean>

2,普通类中进行调用:

        //普通类中调用service层服务
protected WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
protected SqlSessionTemplate sql=(SqlSessionTemplate)wac.getBean("sqlSession");
protected SqlSession sqlSession=null;

private static final String sqlSpaceName="Push";        //要调用的注解方法

       /**
* ********************************************************
* @Title: getSqlName
* @Description: TODO(获取执行SQL所需要的格式: 命令空间.SQLID)
* @param sqlId
* @return String
* @date 2014-11-4 下午03:37:14
********************************************************
*/
public String getSqlName(String sqlId){

return sqlSpaceName+"."+sqlId;

        }

        /**
* ********************************************************
* @Title: getSession
* @Description: TODO(获取sqlSession并打开)
* @return SqlSession
* @date 2014-11-4 下午03:15:19
********************************************************
*/
public SqlSession getSession(){
this.sqlSession=sql.getSqlSessionFactory().openSession();
return sqlSession;

}

        /**
* ********************************************************
* @Title: closeSession
* @Description: TODO(关闭数据连接) void
* @date 2014-11-4 下午03:19:35
********************************************************
*/
public void closeSession(){
sqlSession.close();
}

        。。。。。。

        //方法中进行调用

        Push ph = (Push)getSession().selectOne(getSqlName("getOne"), map);

        。。。。。。

        关闭session连接

        //关闭session
closeSession();


猜你喜欢

转载自blog.csdn.net/cling_snail/article/details/80649165