Spring和MyBatis框架整合,简单springconfig.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置的根元素+约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- 配置注解扫描包位置 -->
	<context:component-scan base-package="cn.zj.ssm"/>

	<!-- 读取db.properties配置文件 
	
		<context:property-placeholder location="classpath:db.properties"/>
			location: 配置文件地址 。必须加上前缀   classpath:
			Spring框架只要读取资源配置文件全部需要加上  classpath: (规则)
	-->
	<context:property-placeholder location="classpath:db.properties"/>
	
	
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<!-- 
			使用SpringEL表达式可以获取  xxx.properties配置文件key对应的值
			语法  :${key} 读取配置文件的值
				 #{100*200*1024} Spring可以使用算数表达式,运行阶段自动计算值
			
			注意:
				db.properties配置不能使用 username 作为key,
					Spring默认读取当前操作系统登录账号
				解决方案:统一价一个前缀即可
		 -->
		<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="maxActive" value="${jdbc.maxActive}"/>
	</bean>
	
	
	<!-- 配置SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 1.配置数据源 -->
		<property name="dataSource" ref="dataSource"/>
		
		<!-- 2.配置映射文件 -->
		<property name="mapperLocations">
			<array>
				<!-- 配置单个映射文件 -->
				<!-- <value>classpath:cn/zj/ssm/mapper/UserMapper.xml</value> -->
				<!-- 使用 * 配置通用规则 -->
				<value>classpath:cn/zj/ssm/mapper/*Mapper.xml</value>
			</array>
		</property>
		
		<!-- 3.配置包扫描创建别名:pojo包实体类取别名 -->
		<property name="typeAliasesPackage" value="cn.zj.ssm.pojo"/>
		
		<!-- 4. 读取mybatis-config.xml配置文件-->
		<property name="configLocation" value="classpath:mybatis-config.xml"/>
	
	</bean>
	
	
	<!-- 使用包扫描创建mapper包下面所有映射接口的代理对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置映射接口的包 -->
		<property name="basePackage" value="cn.zj.ssm.mapper"/>
		
		<!-- 配置SqlSessionFactory Bean的名称  [可选] -->
		<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
	</bean>
	
	
	<!-- 
		配置事务思路
		1,配置事务管理器
		2,配置事务通知(事务细节)
		3,使用AOP将事务切入到Service层
	 -->
	 
	 <!-- 1,配置事务管理器-->
	 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<!-- 配置数据源 -->
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	 
	 <!-- 2,配置事务通知(事务细节) -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事务通知属性 -->
		<tx:attributes>
			<!-- DQL  -->
			<tx:method name="get*" read-only="true"  />
			<tx:method name="select*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			
			<!-- DML -->
			<tx:method name="*" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 3,使用AOP将事务切入到Service层 -->
	<aop:config>
		<!-- 配置切入点  -->
		<aop:pointcut expression="execution(* cn.zj.ssm.service..*.*(..))" id="pt"/>
		<!-- 配置切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
	</aop:config>
	
	
</beans>

注意导包,缺少必要jar包会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. web.xml:这是一个Java Web 项目的核心配置文件,主要用于配置Servlet、Filter、Listener等Web组件,并且定义了Servlet容器的一些基本配置,如编码、Session管理、错误页面等。其中,常用的配置包括: - 配置Servlet:用于处理HTTP请求的Java类。 - 配置Filter:用于对HTTP请求进行过滤和处理。 - 配置Listener:用于监听Web应用程序的生命周期事件。 2. springmvc-config.xml:这是一个Spring MVC框架配置文件,主要用于配置Spring MVC的核心组件,如HandlerMapping、ViewResolver、Interceptor等。其中,常用的配置包括: - 配置HandlerMapping:用于映射请求到相应的控制器方法。 - 配置ViewResolver:用于将控制器方法返回的逻辑视图名映射到实际的视图模板。 - 配置Interceptor:用于拦截请求,在处理请求前或处理请求后进行一些操作,如权限控制、日志记录等。 3. spring-mybatis.xml:这是一个整合SpringMyBatis框架配置文件,主要用于配置数据库连接、事务管理、Mapper接口扫描等。其中,常用的配置包括: - 配置数据源:用于连接数据库,设置连接池等。 - 配置事务管理器:用于管理数据库事务,保证事务的一致性和可靠性。 - 配置Mapper接口扫描:用于自动扫描Mapper接口,并将其注册为Spring的Bean。 4. applicationcontext.xml:这是一个Spring框架的核心配置文件,主要用于配置Spring容器中的各种Bean,包括Service、DAO、Interceptor等。其中,常用的配置包括: - 配置Bean:用于定义Spring容器中的各种Bean。 - 配置AOP:用于实现面向切面编程,如事务管理、日志记录等。 - 配置属性文件:用于加载外部的属性文件,如数据库连接信息、邮件服务器信息等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值