Springの2種類はmybatisの配置ファイルの方法を導入します.
Spring 2種類のMyBatisプロファイルの導入方法
1、(推奨)
spring容器にmybatis-configを導入して、mybatis-configにMapperのマッピングファイルの住所があります.直接にmybatis-config.xmlを取得しました.
Mapperマッピングファイルのアドレスを直接取得すると、mybatis-config.xmlファイルを書く必要がありません.
spring.xml:
1、(推奨)
mybatis-config.xml
ファイルを単独で新たに作成し、Spring
容器に注入することに依存する.spring容器にmybatis-configを導入して、mybatis-configにMapperのマッピングファイルの住所があります.直接にmybatis-config.xmlを取得しました.
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
settings>
<typeAliases>
<package name="cn.yznu.pojo" />
typeAliases>
<mappers>
<mapper resource="mappers/UserMapper.xml" />
mappers>
configuration>
spring.xml:
は、mybatisプロファイルを導入する.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
<context:component-scan base-package="cn.yznu"/>
beans>
2、Spring
容器に直接
で注入する.Mapperマッピングファイルのアドレスを直接取得すると、mybatis-config.xmlファイルを書く必要がありません.
は、Mapperマッピングファイルアドレスを導入する.spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
<context:component-scan base-package="cn.yznu"/>
beans>