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来响应静态文件-->
<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">
<!--引入数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--
druid数据源
-->
<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来统一管理
-->
<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>
<!--
声明事务的实现方式
以这些关键字开头的方法分别设置事务的隔离级别以及出错后的操作
-->
<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语句构建器