Spring 和 Mybatis 整合

Spring 和 Mybatis 整合

Spring本身的Config文件:

在IDEA下面配置好文件后, 在WEB-INF下面有三个配置文件分别是web.xml, applicationContext.xml, dispatcher-servlet.xml.

其中, web.xml是总体的配置, 具体内容为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

可见web.xml中配置了其他两个配置项, 注册了一个sevlet作为dispacher, appcontext对应applicationContext.xml, 是注册其他bean的. 我们整合也是配置的这个文件. 原始的applicationContext.xml只有一个beans的定义.

修改过的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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:property-placeholder location="/WEB-INF/jdbc.properties"/>

    <!--&lt;!&ndash; 1. 数据源 : DriverManagerDataSource &ndash;&gt;-->
    <!--<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" />-->
        <!--<property name="username" value="root" />-->
        <!--<property name="password" value="123456" />-->
    <!--</bean>-->

    <!--&lt;!&ndash;-->
        <!--2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源-->

        <!--MyBatis定义数据源,同意加载配置-->
    <!--&ndash;&gt;-->
    <!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">-->
        <!--<property name="dataSource" ref="dataSource"></property>-->
        <!--<property name="configLocation" value="classpath:config/mybatis-config.xml" />-->
    <!--</bean>-->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <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"/>
        <!-- 加载Mybatis全局配置文件 -->
        <property name="configLocation" value="/WEB-INF/SqlMapConfig.xml"/>
    </bean>

    <!-- 配置mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
        <property name="basePackage" value="lyb.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--
        4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
    -->
    <bean id="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5. 使用声明式事务
         transaction-manager:引用上面定义的事务管理器
     -->
    <tx:annotation-driven transaction-manager="txManager" />
</beans>

主要是增加了几个bean, dataSource的bean是配置数据库信息的, 根据jdbc的properties来加载数据库; sqlSessionFactory是配置Mybatis的配置文件的;
还有一个bean是配置mapper的文件夹的; 最后一个是配置事务的.

其中配置Mybatis的文件的内容为:

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

    <!--&lt;!&ndash; 实体类,简称 -设置别名 &ndash;&gt;-->
    <!--<typeAliases>-->
        <!--<typeAlias alias="User" type="com.tgb.model.User" />-->
    <!--</typeAliases>-->
    <!-- 实体接口映射资源 -->
    <!--
        说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
    -->
    <mappers>
        <mapper resource="lyb/mapper/StudentMapper.xml" />
    </mappers>

</configuration>

就是Mybatis的mapper注册的配置项.

其他的就是在实体类的同一个文件夹下面写好实体类对应的sql语句的xml就好了.

原文地址:https://www.cnblogs.com/putuotingchan/p/8630999.html