1.导入核心jar包
注意:一个不能少
5.配置db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=12345678
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
3.编写Spring配置文件applicationContext.xml
<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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.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">
<!--读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1.配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 1.1.数据库驱动 -->
<property name="driverClassName"
value="${jdbc.driver}"></property>
<!-- 1.2.连接数据库的url -->
<property name="url"
value="${jdbc.url}"></property>
<!-- 1.3.连接数据库的用户名 -->
<property name="username" value="${jdbc.username}"></property>
<!-- 1.4.连接数据库的密码 -->
<property name="password" value="${jdbc.password}"></property>
<!-- 最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}"></property>
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 事务管理器,依赖于数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解,注册事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- mybatis与spring整合 -->
<!-- 配置MyBatis工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定核心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
</beans>
按图配置
4.编写MyBatis配置文件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>
<!-- 配置扫描包,别名就是类名首字母小写,别名不区分大小写 -->
<typeAliases>
<package name="com.lin.po"/>
</typeAliases>
<!--2.配置Mapper的位置 -->
<mappers>
<!-- ... -->
</mappers>
</configuration>
5.引入log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.lin=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n