spring整合mybatis步驟

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ū)動 -->

? ? ? ? <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>


? <!--實例化Dao -->

<bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl">

<!-- 注入SqlSessionFactory對象實例-->

? ? <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>

5.log4j.properties

# Global logging configuration log4j.rootLogger=ERROR, stdout

# MyBatis logging configuration... log4j.logger.com.itheima=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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容