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;
}
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com