`
扬起风帆
  • 浏览: 117743 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
spring常用配置 spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 	
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd">
		
	<!-- 配置数据源 -->
	<!-- tomcat:[java:comp/env/jdbc/ttds], jboss[java:/ttds] -->
	 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName" value="java:/ttds"/>
	</bean>
	<!-- 配置回话工厂 -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:mybatis.xml"/>
		<property name="mapperLocations">
			<list>
				<value>classpath:com/my/tt/persistence/basemapping/*Mapper.xml</value>
				<value>classpath:com/my/tt/persistence/expandmapping/*Mapper.xml</value>
			</list>
		</property>
	</bean>
	<!-- 配置事务管理 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">     
          <property name="dataSource" ref="dataSource"></property>
    </bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
			<tx:method name="save*" propagation="REQUIRED"></tx:method>
			<tx:method name="update*" propagation="REQUIRED"></tx:method>
			<tx:method name="*" propagation="SUPPORTS" read-only="true"></tx:method>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="amService" expression="execution(* com.my.tt.business.impl.*.*(..))" />
		<aop:advisor pointcut-ref="amService" advice-ref="txAdvice" />
	</aop:config>
	<!-- 通过扫描模式,扫描指定目录下所有mapper接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.my.tt.persistence.*" />
	</bean>


</beans>
spring单元测试开发 spring test
package com.cnvai.cms.business;

//import static org.junit.Assert.fail;

import javax.inject.Inject;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;

@ContextConfiguration("classpath:applicationContext-common.xml")
public class TTTTTT extends AbstractJUnit38SpringContextTests {
	
	//第一种方法是通过@Inject注入
	@Inject
	private TTTService tttService;
	

	public TTTService getTttService() {
		return tttService;
	}


	public void setTttService(TTTService tttService) {
		this.tttService = tttService;
	}


	public void test() {
		// fail("Not yet implemented");
		//第二种方法是从上下文中查找,如果你没有是用注解那么推荐在上下问中查找
		TTTService service = (TTTService)applicationContext.getBean("TTTService");
	}

}
Struts2 BaseAction struts2, baseaction
//=============================================================================
//BaseAction接口
//=============================================================================
package org.hlc.web.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface BaseAction {

	public String getServerPath();

	public HttpServletRequest getHttpServletRequest();

	public HttpServletResponse getHttpServletResponse();

	public ServletContext getServletContext();

	public Map<String, Object> getSession();
}
//=============================================================================
//BaseAction的实现类
//=============================================================================
package org.hlc.web.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class BaseActionSupport extends ActionSupport implements BaseAction {

	private static final long serialVersionUID = 1L;

	// 此处可以根据需要增加一些公共属性,如分页,网页消息等

	public HttpServletRequest getHttpServletRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getHttpServletResponse() {
		return ServletActionContext.getResponse();
	}

	public String getServerPath() {
		StringBuffer serverPath = new StringBuffer();
		serverPath.append("http://");
		serverPath.append(getHttpServletRequest().getServerName());
		serverPath.append(":");
		serverPath.append(getHttpServletRequest().getServerPort());
		serverPath.append(getHttpServletRequest().getContextPath());
		return serverPath.toString();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public Map<String, Object> getSession() {
		return ServletActionContext.getContext().getSession();
	}

}
MybatisMpper代码实例 mybatis,mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hlc.persistence.expanddao.artclsMapper">
	<update id="discontinuationartcls" parameterType="java.math.BigDecimal">
		update artcls set flag='1' where aid = #{aid}
	</update>
	<update id="activateartcls" parameterType="java.math.BigDecimal">
		update artcls set flag='0' where aid = #{aid}
	</update>	
	<select id="getartclsListALL" resultType="artcls">
		select aid, ap, code, name, flag from artcls
	</select>
	<select id="loadartclsTree" parameterType="java.math.BigDecimal" resultType="JqueryTree">
		select aid as id, name as text from artcls
		where flag = '0'
		<if test="ap!=null">
		and ap = #{ap}
		</if>
		<if test="ap==null">
		and ap is null
		</if>
	</select>
	<delete id="deleteARTclass">
		delete from artcls where aid in
		<foreach item="item" index="index" collection="list"
			open="(" separator="," close=")"> 
			#{item} 
        </foreach> 
	</delete>
	<select id="selectExistsNumber" resultType="int">
		select count(0) from art where arcclass in
		<foreach item="item" index="index" collection="list"
			open="(" separator="," close=")"> 
			#{item} 
        </foreach> 
	</select>
	<select id="getValidartclsList" resultType="artcls">
		select aid, ap, code, name, flag from artcls where flag = '0'
	</select>
</mapper>
spring test测试用例 spring,test
package com.hlc.biz;

import javax.inject.Inject;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;

import com.scjs.mrp.model.ComUser;
import com.scjs.mrp.model.LoginInfo;

@ContextConfiguration("classpath:biz.xml")
public class AccountBizTestCase extends AbstractJUnit38SpringContextTests {

	@Inject
	private AccountBiz accountBiz;

	public void testLogin() {
		ComUser comUser = new ComUser();
		comUser.setUsraccount("51010825");
		comUser.setUsrpassword("123456");
		LoginInfo loginInfo = getAccountBiz().login(comUser);
		if (loginInfo == null) {

		}
		System.out.println(loginInfo.getUsrName());
	}

	public AccountBiz getAccountBiz() {
		return accountBiz;
	}

	public void setAccountBiz(AccountBiz accountBiz) {
		this.accountBiz = accountBiz;
	}

}
Global site tag (gtag.js) - Google Analytics