SSM框架整合(6)—— Spring整合MyBatis

本文详细介绍了如何将Spring与MyBatis进行整合,包括配置连接池、SqlSessionFactoryBean、持久层接口扫描,以及Spring的声明式事务管理。通过示例展示了业务层的实现、jsp页面的使用,并给出了运行后的页面展示和控制台输出。
摘要由CSDN通过智能技术生成

Spring 整合 MyBatis

结构目录

  • java
    • controller
      • AccountController.java(表现层)
    • dao
      • AccountDao.java(持久层)
    • domain
      • Account.java(JavaBean对象)
    • service
      • AccountService.java(业务层)
      • AccountServiceImp.java(业务层)
  • resources
    • applicationContext.xml(Spring配置文件)
    • springmvc.xml(SpringMVC配置文件)
  • webapp
    • WEB-INF
      • pages
        • list.jsp(jsp页面文件)
      • web.xml(web配置文件)
    • index.jsp(jsp页面文件)

Spring配置文件

  • 开启注解扫描
    • 忽略表现层注解
  • Spring 整合 MyBatis
    • 配置 连接池(ComboPooledDataSource)
    • 配置 SqlSessionFactoryBean
    • 配置 持久层接口的包
  • Spring 声明式事务管理
    • 配置 事务管理(DataSourceTransactionManager)
    • 配置 事务通知(tx:advice)
    • 配置 AOP增强(aop:config)
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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- 开启注解扫描 -->
            <!-- 使用Spring框架处理业务层(service)和持久层(dao),不处理表现层(Controller) -->
        <context:component-scan base-package="cn.water">
            <!-- 配置不扫描的注解类 -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

        <!-- Spring 整合 MyBatis -->
        <!-- 配置 连接池 -->
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql:///ssm"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
        <!-- 配置 SqlSessionFactoryBean -->
        <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="comboPooledDataSource"></property>
        </bean>
        <!-- 配置 持久层接口的包 -->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.water.dao"></property>
        </bean>

        <!-- Spring 声明式事务管理 -->
        <!-- 配置 事务管理器 -->
        <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="comboPooledDataSource"></property>
        </bean>
        <!-- 配置 事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
            <tx:attributes>
                <!-- 查询方法:只读 -->
                <tx:method name="find*" read-only="true"/>
                <!--  -->
                <tx:method name="*" isolation="DEFAULT"/>
            </tx:attributes>
        </tx:advice>
        <!-- 配置 AOP增强 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.water.service.*SErviceImp.*(..))"/>
        </aop:config>

  </beans>

业务层

  • 业务层调用持久层
AccountServiceImp.java
package cn.water.service;

import cn.water.dao.AccountDao;
import cn.water.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")
public class AccountServiceImp implements AccountService{

    @Autowired
    private AccountDao dao;

    /** 查询所有用户 */
    @Override
    public List<Account> findAll() {
        /* 业务层 */
        System.out.println("业务层:查询所有用户");
        /* 持久层 */
        return dao.findAll();
    }

    /** 添加用户 */
    @Override
    public void addAccount(Account account) {
        /* 业务层 */
        System.out.println("业务层:添加用户");
        /* 持久层 */
        dao.addAccount(account);
    }
}

jsp页面

index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3><a href="accountController/findAll">findAll</a></h3>

    <form action="accountController/addAccount" method="post">
        姓名:<input type="text" name="name">
        金额:<input type="text" name="money">
        <input type="submit" value="保存">
    </form>

</body>
</html>

jsp页面

list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h1>用户信息列表</h1>
    <h2>${accounts}</h2>
    <c:forEach items="${accounts}" var="account">
        <h3>${account.id}</h3>
        <h3>${account.name}</h3>
        <h3>${account.money}</h3>    <br>
    </c:forEach>
</body>
</html>

运行结果

页面展示
用户信息列表
[Account{id=1, name='cat', money=9.16}, Account{id=2, name='dog', money=11.27}, Account{id=3, name='rat', money=14.87}, Account{id=4, name='water', money=11.11}]
1
cat
9.16

2
dog
11.27

3
rat
14.87

4
water
11.11
控制台
表现层:查询所有用户信息
业务层:添加用户
表现层:查询所有用户信息
业务层:查询所有用户
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值