application.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:important.properties</value> </list> </property> </bean> <import resource="classpath*:datasource-mysql.xml" /> <!-- enable component scanning (beware that this does not enable mapper scanning!) --> <context:component-scan base-package="com.xx.xxdp.service" /> <context:component-scan base-package="com.xx.xxdp.web.action" /> <!-- enable autowire --> <context:annotation-config /> <!-- enable transaction demarcation with annotations --> <tx:annotation-driven /> </beans>
mybatis的配置文件:
datasource-mysql.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="${db.mysql.url}?characterEncoding=utf-8&connectTimeout=1000&autoReconnect=true"></property> <property name="username" value="${mysql.username}"></property> <property name="password" value="${mysql.password}"></property> <property name="initialSize" value="1" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="20" /> <property name="maxWait" value="1000" /> <property name="poolPreparedStatements" value="true"/> <property name="validationQuery" value="select 1" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <bean id="mysqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="mysqlDataSource"/> </bean> <tx:annotation-driven transaction-manager="mysqlTransactionManager"/> <!-- mybatis --> <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="mysqlDataSource" /> <property name="typeAliasesPackage" value="com.xx.xxdp.domain.mysql" /> <property name="mapperLocations" value="classpath*:sqlmap/mysql/*.xml" /> </bean> <!--inject dao list --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory"></property> <property name="basePackage" value="com.xx.xxdp.dao.mysql" /> </bean> </beans>
DemoMysql2DaoMapper.xml
<?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.xx.xxdp.dao.mysql.demo.DemoMysql2Dao"> <insert id="insert" parameterType="DemoMysql2" > insert into demoMysql2 values (#{id}, #{name}) </insert> <update id="update" parameterType="DemoMysql2" > update demoMysql2 <set> <if test="name != null" > name = #{name} </if> </set> where id = #{id} </update> <delete id="delete" > delete from demoMysql2 where id = #{id} </delete> <select id="get" resultType="DemoMysql2"> select name from demoMysql2 where id = #{id} </select> <select id="list" resultType="DemoMysql2"> select name from demoMysql2 </select> </mapper>
DemoMysql2Dao.java
package com.xx.xxdp.dao.mysql.demo; import java.util.List; import com.xx.xxdp.domain.mysql.demo.DemoMysql; import org.apache.ibatis.annotations.Param; import com.xx.xxdp.domain.mysql.demo.DemoMysql2; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; /** * demo dao */ @Repository public interface DemoMysql2Dao { public int insert(DemoMysql2 demo);//注意方法名和Mapper中定义一致 public int delete(@Param("id") Integer id); public int update(DemoMysql2 demo); public DemoMysql2 get(@Param("id") Integer id); public List<DemoMysql2> list(); //注解形式 @Select("select name,title from demoMysql2") public List<DemoMysql2> getList(); }
service接口
package com.xx.xxdp.service.demo; import org.springframework.transaction.annotation.Transactional; import com.xx.xxdp.domain.hivemeta.demo.DemoHiveMeta; import com.xx.xxdp.domain.mysql.demo.DemoMysql; import java.util.List; public interface DemoService { /** * 跨数据源的事务, DemoMysql和DemoHiveMeta位于不同的数据库 * @param demoMysql * @param meta */ @Transactional public void update(DemoMysql demoMysql, DemoHiveMeta meta); //注解的形式 public List<DemoMysql> setList(); }
Impl调用实例
package com.xx.xxdp.service.demo.impl; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xx.xxdp.dao.hivemeta.demo.DemoHiveMetaDao; import com.xx.xxdp.dao.mysql.demo.DemoMysqlDao; import com.xx.xxdp.domain.hivemeta.demo.DemoHiveMeta; import com.xx.xxdp.domain.mysql.demo.DemoMysql; import com.xx.xxdp.service.demo.DemoService; import java.util.List; /** * demo服务实现类 * @author */ @Service("demoService") public class DemoServiceImpl implements DemoService { private final static Logger LOG = Logger.getLogger(DemoServiceImpl.class); @Autowired private DemoMysqlDao demoMysqlDao; @Autowired private DemoHiveMetaDao demoHiveMetaDao; public void update(DemoMysql demoMysql, DemoHiveMeta meta) { demoMysqlDao.update(demoMysql); if ( 1 == 1 ) { throw new RuntimeException(); } demoHiveMetaDao.update(meta); } public List<DemoMysql> setList(){ return demoMysqlDao.getList(); } }
接下来就是@Controller调用了
@Controller public class SingleAnalysisControl { private static final Logger log = Logger.getLogger(Control.class); @Resource(name="demoService") private demoServiceDemoService; @RequestMapping(value = "/getList", method = RequestMethod.GET) public ModelAndView getSingleAnalysis(HttpServletRequest httpServletRequest,@RequestParam(value = "sku", required = false, defaultValue = "") String sku,@RequestParam(value = "theDay", required = false, defaultValue = "") String theDay) { ModelAndView view = new ModelAndView(); List<DemoMysql> listPro=null; try { if (theDay.isEmpty()) { return null; } else { listPro = demoService.getList(); view.addObject("listViewsSourceDetail",listPro); } } catch (Exception ex) { log.error(ex.getMessage()); } return view; } }