1、配置
1.1导入依赖关系
<!--集中定义版本h号-->
<properties>
<!--集中定义项目中依赖的版本号-->
<druid.version>1.1.10</druid.version>
<junit.version>4.12</junit.version>
<spring.version>4.3.7.RELEASE</spring.version>
<mybatis.version>3.4.5</mybatis.version>
<!--MyBatis和Spring整合的专用包-->
<mybatis.spring.version>1.3.0</mybatis.spring.version>
<mysql.version>5.1.38</mysql.version>
<slf4j.version>1.6.4</slf4j.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<commons-io.version>1.3.2</commons-io.version>
</properties>
<!--导入依赖-->
<dependencies>
<!--SSM的依赖-->
<!--数据库连接池工具包 阿里出产!-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Mybatis -->
<!--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
-->
<!--Spring和MyBatis整合包-->
<!--
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
-->
<!--MyBatis Plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.2.0</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<build>
<!--XML编译问题-->
<!--编译的时候同时也把包下面的xml同时编译进去-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
1.2创建resources文件
(1)applicationContext.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">
<!--1.扫描包-->
<context:component-scan base-package="com.zxsilent.*"/>
<!--2.加载数据库信息文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--3.配置德鲁伊数据库连接池-->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<!--4.配置MyBatis的sqlSession工厂 Spring框架管理了session
如果需要使用MyBatisPlus那么就把 SqlSessionFactoryBean 替换为 MybatisSqlSessionFactoryBean
-->
<bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<!--指定Mapper的映射文件位置-->
<property name="mapperLocations" value="classpath:com/zxsilent/mapper/*.xml"/>
<!--指定实体类的别名-->
<property name="typeAliasesPackage" value="com.bruceliu.bean"/>
<!--配置MyBatisPlus的分页插件-->
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
</array>
</property>
</bean>
<!--5.Spring框架扫描Mapper层接口,自动给Mapper层接口生成代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zxsilent.mapper"/>
</bean>
<!--6.配置事务管理 事务:要么全部成功,要么全部失败! 原子性、一致性、隔离性、持久性-->
<!--配置MyBatis的事务平台管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="ds"/>
</bean>
<!-- <!–-->
<!-- Spring的声明式事务目前有2种配置方式:-->
<!-- 1.XML配置声明式事务 (过时 了解)-->
<!-- 2.注解配置声明式事务 (推荐 掌握)-->
<!-- –>-->
<!-- <!–配置事务增强–>-->
<!-- <tx:advice id="adviser" transaction-manager="transactionManager">-->
<!-- <!–配置哪些方法需要配代理植入事务–>-->
<!-- <tx:attributes>-->
<!-- <tx:method name="transferMioney"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!-- <!–配置AOP切入点–>-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.zxsilent.service.impl.*.*(..))"/>-->
<!-- <aop:advisor advice-ref="adviser" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
<!--
Spring的声明式事务目前有2种配置方式:
1.XML配置声明式事务 (过时 了解)
2.注解配置声明式事务 (推荐 掌握)
-->
<!--开启注解管理事务 注解配置声明式事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
(2)jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
(3)log4j.properties
# log4J日志框架的配置文件 文件名字不能改
# DEBUG 表示日志的级别 调试
# Console 日志打印在控制台
log4j.rootLogger=INFO, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
# 哪些日志需要打印
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
转载:https://blog.csdn.net/qq_40744354/article/details/101287925
查看评论