1. 程式人生 > 其它 >Spring-MyBatis的配置檔案

Spring-MyBatis的配置檔案

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--DataSource:使用Spring的資料來源替換Mybatis的配置 
--> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?userSSL=true&amp;userUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"
/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource"
/> <!--繫結Mybatis配置檔案--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:cn/lkx/mapper/UserMapper.xml"/> </bean> <!--SqlSessionTemplate:我們使用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能使用構造器注入sqlSessionFactory ,原始碼中沒有set--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userMapper" class="cn.lkx.mapper.impl.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean> </beans>