Spring集成MyBatis框架(环境详细配置)
Posted 韶光不负
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成MyBatis框架(环境详细配置)相关的知识,希望对你有一定的参考价值。
目录
配置pom.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring_mybatis</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--junit 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<!-- spring测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring 事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- aspect 切面编程的jar -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!-- c3p0 连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 添加mybatis与Spring整合的核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- 日志打印相关jar -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
<build>
<!-- 设置资源目录与插件
Maven项目:如果源代码(src/main/java)存在xml、 properties. tld 等文件
Maven 默认不会自动编译该文件到输出目录,
如果要编译源代码中xml properties tld 等文件需要工配resources标签
-->
<resources>
<resource>
<directory>src/main/properties</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
配置spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/aop/spring-tx.xsd
http://www.springframework.org/schema/context ">
<!--
使用注解方式进行创建对象
1.开启注解扫描
含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
放到spring容器中
-->
<context:component-scan base-package="com"/>
<!-- 加载properties 配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!--aop相关配置-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置C3p0 数据源 -->
<bean id = "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="$()"></property>
<property name="jdbcUrl" value="$()"></property>
<property name="user" value="$()"></property>
<property name="password" value="$()"></property>
</bean>
<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 设置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="insert" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop 切面配置 -->
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* com.lsf.sercice..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"></aop:advisor>
</aop:config>
<!-- 配置 sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybat.xml"></property>
<property name="mapperLocations" value="classpath:com/lsf/mapper/*.xml"></property>
</bean>
<!-- 配置扫描器 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lsf.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
配置Mybatis.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>
<!--定义类别名-->
<typeAliases>
<package name=" com.lsf.po' />
</typeAliases>
< / configuration>
配置db.properties
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc :mysql://localhost:3306/spring._mybatis?useUnicode = true&characterEncoding=utf&serverTimezone=GMT%2B88useSSL=false
jdbc.username = root
jdbc.password = root
#localhost:3306/spring._mybatis
#localhost 用户名
#3306 MySQL接口
#spring._mybatis 数据库名称 不同需要更改
配置log4j.properties
1og4j.rootlogger =DEBUG, Console
# Console 日志
log4j.appender.Console = org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout = org.apache.1og4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern = xd [%t] %-5p [%c] - %m%n
log41.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log41.logger.java.sql.Statement=DEBUG
log4j.logger.java.sq1.PreparedStatement=DEBUG
以上是关于Spring集成MyBatis框架(环境详细配置)的主要内容,如果未能解决你的问题,请参考以下文章