mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--設置ssm項目的注解配置-->
<context:annotation-config/>
<!--包掃描-->
<context:component-scan base-package="com.qfedu"/>
<!--注解驅動窒朋,以使得訪問路徑與方法的匹配可以通過注解配置-->
<mvc:annotation-driven/>
<!--使用默認的Servlet來響應靜態(tài)文件-->
<mvc:default-servlet-handler/>
<!--引入spring和mybatis的整合文件-->
<import resource="classpath:spring-mybatis.xml"/>
<!--視圖解析-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring-mybatis.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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入數(shù)據(jù)庫配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--
druid數(shù)據(jù)源
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<!--<property name="password" value="${password}"/>-->
<property name="password" value="${pass}"/>
</bean>
<!--
SqlSessionFactoryBean,將mybatis交給spring來統(tǒng)一管理
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.qfedu.pojo"/>
<property name="mapperLocations" value="classpath:com/qfedu/dao/*Mapper.xml"/>
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--
配置映射掃描配置,分別設置dao包掃描和SqlSessionFactory的指定
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qfedu.dao"/>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
</bean>
<!--
配置事務管理器
-->
<bean id="dtx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
聲明事務的實現(xiàn)方式
以這些關鍵字開頭的方法分別設置事務的隔離級別以及出錯后的操作
-->
<tx:advice transaction-manager="dtx" id="tx">
<tx:attributes>
<tx:method name="save" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="insert" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="update" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
<tx:method name="delete" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- 定義切面 -->
<aop:config>
<aop:pointcut id="mpt" expression="execution(* com.qfedu.service.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="mpt"/>
</aop:config>
</beans>
- MyBatis SQL語句構建器