本篇文章为大家展示了使用spring怎么对mybatis进行整合,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1 数据库连接配置信息jdbc.properties
#mysql version database druid # setting validationQuery=SELECT 1 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ivan?useUnicode=true&characterEncoding=utf-8&useSSL=true jdbc.username=root jdbc.password=root
2 spring配置文件spring-register.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--引入配置文件--> <!-- <context:property-placeholder location="classpath:config/jdbc.properties"/>--> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/zookeeper.properties</value> <value>classpath:config/jdbc.properties</value> <value>classpath:config/log4j.properties</value> </list> </property> </bean> <bean id="userService" class="com.ivan.dubbo.service.impl.UserServiceImpl"/> <!--提供方应用信息,用于计算依赖关系--> <dubbo:application name="ivan-dubbo-server"></dubbo:application> <!--使用zookeeper广播注册中心暴露服务地址--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 本机 伪集群 测试 --> <!-- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:4170?backup=127.0.0.1:4180,127.0.0.1:4190" />--> <!-- <dubbo:registry protocol="zookeeper" address="127.0.0.1:4170,127.0.0.1:4180,127.0.0.1:4190"/>--> <!-- <dubbo:registry id="ivan-dubbo-server1" protocol="zookeeper" address="127.0.0.1:4170" /> <dubbo:registry id="ivan-dubbo-server2" protocol="zookeeper" address="127.0.0.1:4180" /> <dubbo:registry id="ivan-dubbo-server3" protocol="zookeeper" address="127.0.0.1:4190" /> --> <!---使用dubbo协议在20880端口暴露服务--> <dubbo:protocol name="dubbo" port="20880"/> <!-- 官方注释:扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类。 测试发现:此处package不填写包名会无法注册Service,扫描全包需填写包首即可或者填写至类的上一级目录。 --> <dubbo:annotation package="com"/> <dubbo:service interface="com.ivan.service.provider.UserService" ref="userService" timeout="1200000"></dubbo:service> </beans>
3 spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!--配置数据源--> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--初始化连接池大小--> <property name="initialSize" value="5"/> <property name="maxActive" value="20"/> <!--连接池最小空闲--> <property name="minIdle" value="0"/> <!--获取连接池最大等待时间--> <property name="maxWait" value="60000"/> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="33"/> <!--检测有效sql--> <property name="validationQuery" value="${validationQuery}"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="testWhileIdle" value="true"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000"/> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true"/> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800"/> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true"/> <!-- 监控数据库 --> <property name="filters" value="mergeStat"/> </bean> <!--MyBatis配置文件--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:com/ivan/**/mapping/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ivan.dubbo.service.dao.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--配置事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--注解方式配置事务--> <!-- <tx:annotation-driven transaction-manager="transactionManager"/>--> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- <span > </span>Spring aop事务管理 <span > 此处配置正确无法发布提供者服务,目前没找到解决方案</span> --> <!-- <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.ivan..service.impl.*Impl.*(..))" /> <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/> </aop:config> --> </beans>
上述内容就是使用spring怎么对mybatis进行整合,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。