spring+springmvc+mybatis整合

本文详细介绍了在JavaEE开发中如何构建三层结构,并整合MyBatis框架,包括配置数据源、使用Spring管理DAO层、配置事务管理及SpringMVC控制器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先建立javaee开发的三层结构
导入配置文件 jdbc.properties log4j.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456


log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %d [%-5p] %c - %m%n
#show sql
log4j.logger.java.sql.ResultSet=INFO 
log4j.logger.org.apache=INFO 
log4j.logger.java.sql.Connection=DEBUG 
log4j.logger.java.sql.Statement=DEBUG 
log4j.logger.java.sql.PreparedStatement=DEBUG

导入spring 配置文件

applicationContext-dao.xml:配置数据源,加载mybatis配置文件 mybatis-config.xml,mapper接口映射

<?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"
     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">
             
              <context:property-placeholder location= "classpath:jdbc.properties"/>
             
              <bean id ="dataSource" class=" org.apache.commons.dbcp.BasicDataSource">
                       <property name ="driverClassName" value= "${jdbc.driver}"/>
                       <property name ="url" value="${jdbc.url}"/>
                       <property name ="username" value= "${jdbc.username}"/>
                       <property name ="password" value= "${jdbc.password}"/>
                      
                       <property name ="maxActive" value="10"/>
                       <property name ="maxIdle" value="5"/>
              </bean >
             
              <bean id ="sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean" >
                       <property name ="dataSource" ref= "dataSource"/>
                      
                       <property name ="configLocation" value= "classpath:mybatis-config.xml"/>
                      
                       <!-- <property name="typeAliasesPackage" value="com.hsj.bean"></property> -->
              </bean >
             
              <bean class= "org.mybatis.spring.mapper.MapperScannerConfigurer" >
                       <property name ="basePackage" value= "com.hsj.mapper"/>
              </bean >
</beans>

applicationContext-service.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"
     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">
             
              <context:component-scan base-package= "com.hsj.service"/>
</beans>



applicationContext-trans.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"
     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
              " >
             
            <bean id ="transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
                 <property name ="dataSource" ref="dataSource"/>
            </bean >
           
     <tx:advice id ="txAdvice" transaction-manager="transactionManager" >
            <tx:attributes >
                 <!-- 传播行为 -->
                 <tx:method name ="save*" propagation="REQUIRED" />
                 <tx:method name ="insert*" propagation="REQUIRED" />
                 <tx:method name ="delete*" propagation="REQUIRED" />
                 <tx:method name ="update*" propagation="REQUIRED" />
                 <tx:method name ="find*" propagation="SUPPORTS" read-only= "true" />
                 <tx:method name ="get*" propagation="SUPPORTS" read-only= "true" />
                 <tx:method name ="query*" propagation="SUPPORTS" read-only= "true" />
            </tx:attributes >
     </tx:advice >
     
     <aop:config >
            <aop:advisor advice-ref ="txAdvice" pointcut= "execution(* com.hsj.service.*.*(..))" />
     </aop:config >
</beans>

springmvc.xml

<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns= "http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
     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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
           
           
            <context:component-scan base-package= "com.hsj.controller"/>
            <mvc:annotation-driven />
            <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
                 <property name ="prefix" value= "/WEB-INF/jsp/"></property >
                 <property name ="suffix" value=".jsp"></ property>
            </bean >
</beans>

配置 web.xml

<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "WebApp_ID" version ="3.0">
  < display-name>springmvc- mybatis</ display-name>
  < welcome-file-list>
    <welcome-file >index.html </welcome-file >
    <welcome-file >index.htm </welcome-file >
    <welcome-file >index.jsp </welcome-file >
    <welcome-file >default.html </welcome-file >
    <welcome-file >default.htm </welcome-file >
    <welcome-file >default.jsp </welcome-file >
  </ welcome-file-list>
 
  <!-- 配置spring -->
   <context-param >
            <param-name >contextConfigLocation </param-name >
           <param-value >classpath:applicationContext*. xml</ param-value>
  </ context-param>
  <!-- 配置监听器加载Spring配置文件 -->
  < listener>
           <listener-class >org.springframework.web.context.ContextLoaderListener </listener-class >
  </ listener>
  < servlet>
            <servlet-name >springMvc </servlet-name >
           <servlet-class >org.springframework.web.servlet.DispatcherServlet </servlet-class >
            <init-param >
                 <param-name >contextConfigLocation </param-name >
                 <param-value >classpath:springmvc.xml </param-value >
            </init-param >
  </ servlet>
  < servlet-mapping>
            <servlet-name >springMvc </servlet-name >
            <url-pattern >/ </url-pattern >
  </ servlet-mapping>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值