spring+springMVC+mybatis整合

7 篇文章 0 订阅
2 篇文章 0 订阅

- web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <!-- 注册springmvc框架核心控制器 -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value> 
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!-- 编码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>
    org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  • mybatis配置文件,mybatis.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.som.entity"/>
    </typeAliases>
</configuration>
  • spring配置文件,spring.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="

      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-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

      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

      ">




    <!-- 配置C3P0连接池,目的:管理数据库连接 -->
      <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/stu_origin"/>
            <property name="user" value="root"/>
            <property name="password" value="1994220"/>
      </bean>


    <!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <property name="dataSource" ref="comboPooledDataSource"/>
        <!--配置扫描式加载SQL映射文件,记得去掉mybatis-config配置-->
        <property name="mapperLocations" value="classpath:com/som/dao/*.xml"/>
    </bean>

     <!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->
      <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="comboPooledDataSource"/>
      </bean>

    <!-- 配置事务通知,即让哪些方法需要事务支持 -->
      <tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
      </tx:advice>

       <!-- 配置事务切面,即让哪些包下的类需要事务 -->
      <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* com.som.dao.*.*(..))"/>
            <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
      </aop:config>


      <!-- 自动扫描 -->
      <context:component-scan base-package="com.som"></context:component-scan>

      <!-- 配置 转换器,对于在basePackage设置的包(包括子包)下的接口类,如果接口类的全类名在Mapper.xml文件中和定义过命名空间一致,将被转换成spring的BEAN,在调用 的地方通过@Autowired方式将可以注入接口实例 
      即,dao不需要再写实现类,持久层只需要接口和sql就可以了-->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- <property name="sqlSessionFactoryBean" ref="sqlSessionFactoryBean" /> -->
        <property name="basePackage" value="com.som.dao" />
    </bean>
</beans>

接下来就是业务层和控制层的方法了,配置到这里就可以用逆向工程自动生成代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值