1.引入jar包
2.編寫db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
3.編寫spring配置文件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:aop="http://www.springframework.org/schema/aop"
? ? xmlns:tx="http://www.springframework.org/schema/tx"
? ? xmlns:context="http://www.springframework.org/schema/context"
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans
? ? http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
? ? http://www.springframework.org/schema/tx
? ? http://www.springframework.org/schema/tx/spring-tx-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">
? ? <!--讀取db.properties -->
? ? <context:property-placeholder location="classpath:db.properties"/>
? ? <!-- 配置數(shù)據(jù)源 -->
<bean id="dataSource"
? ? ? ? ? ? class="org.apache.commons.dbcp2.BasicDataSource">
? ? ? ? <!--數(shù)據(jù)庫驅(qū)動(dòng) -->
? ? ? ? <property name="driverClassName" value="${jdbc.driver}" />
? ? ? ? <!--連接數(shù)據(jù)庫的url -->
? ? ? ? <property name="url" value="${jdbc.url}" />
? ? ? ? <!--連接數(shù)據(jù)庫的用戶名 -->
? ? ? ? <property name="username" value="${jdbc.username}" />
? ? ? ? <!--連接數(shù)據(jù)庫的密碼 -->
? ? ? ? <property name="password" value="${jdbc.password}" />
? ? ? ? <!--最大連接數(shù) -->
? ? ? ? <property name="maxTotal" value="${jdbc.maxTotal}" />
? ? ? ? <!--最大空閑連接? -->
? ? ? ? <property name="maxIdle" value="${jdbc.maxIdle}" />
? ? ? ? <!--初始化連接數(shù)? -->
? ? ? ? <property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 事務(wù)管理器怔昨,依賴于數(shù)據(jù)源 -->
<bean id="transactionManager" class=
? ? "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
? ? <!--開啟事務(wù)注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
? ? <!--配置MyBatis工廠 -->
? ? <bean id="sqlSessionFactory"
? ? ? ? ? ? class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!--注入數(shù)據(jù)源 -->
? ? ? ? <property name="dataSource" ref="dataSource" />
? ? ? ? <!--指定核心配置文件位置 -->
? <property name="configLocation" value="classpath:mybatis-config.xml"/>
? </bean>
? <!--實(shí)例化Dao -->
<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">
<!-- 注入SqlSessionFactory對象實(shí)例-->
? ? <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- Mapper代理開發(fā)(基于MapperFactoryBean) -->
<!-- <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
? ? <property name="mapperInterface" value="com.itheima.mapper.CustomerMapper" />
? ? <property name="sqlSessionFactory" ref="sqlSessionFactory" />?
</bean> -->
<!-- Mapper代理開發(fā)(基于MapperScannerConfigurer) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? <property name="basePackage" value="com.itheima.mapper" />
</bean>
<!-- 開啟掃描 -->
<context:component-scan base-package="com.itheima.service" />
</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.itheima.po" />
? ? </typeAliases>
? ? <!--配置Mapper的位置 -->
<mappers>
? ? ? <mapper resource="com/itheima/po/CustomerMapper.xml" />
? ? ? <!-- Mapper接口開發(fā)方式 -->
? <mapper resource="com/itheima/mapper/CustomerMapper.xml" /
</mappers>
</configuration>