SSM08 —— SSM整合

本文记录了SSM框架的原始整合方式,mybatis整合spring,声明式事务控制实现

原始方式整合

示例account表:

1
2
3
4
5
6
create database ssm;
create table account(
id int primary key auto_increment,
name varchar(100),
money double(7,2)
);
  • 创建Maven工程

  • 导入Maven坐标

  • 编写实体类

    1
    2
    3
    4
    5
    6
    public class Account {

    private Integer id;
    private String name;
    private Double money;
    }
  • 编写Mapper接口

    1
    2
    3
    4
    5
    6
    7
    public interface AccountMapper {
    //保存账户数据
    public void save(Account account);
    //查询账户数据
    public List<Account> findAll();

    }
  • 编写Service接口

    1
    2
    3
    4
    5
    6
    7
    public interface AccountService {
    //保存账户数据
    public void save(Account account);
    //查询账户数据
    public List<Account> findAll();

    }
  • 编写Service接口实现

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @Service("accountService")
    public class AccountServiceImpl implements AccountService {
    public void save(Account account) {
    SqlSession sqlSession = MyBatisUtils.openSession();
    AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    accountMapper.save(account);
    sqlSession.commit();
    sqlSession.close();
    }
    public List<Account> findAll() {
    SqlSession sqlSession = MyBatisUtils.openSession();
    AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
    return accountMapper.findAll();
    }
    }
  • 编写Controller

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @Controller
    @RequestMapping("/account")
    public class AccountController {

    @Autowired
    private AccountService accountService;

    //保存
    @RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String save(Account account){
    accountService.save(account);
    return "保存成功";
    }

    //查询
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
    List<Account> accountList = accountService.findAll();
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("accountList",accountList);
    modelAndView.setViewName("accountList");
    return modelAndView;
    }

    }
  • 编写添加页面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <h1>添加账户信息表单</h1>
    <form name="accountForm" action="${pageContext.request.contextPath}/account/save" method="post">
    账户名称:<input type="text" name="name"><br>
    账户金额:<input type="text" name="money"><br>
    <input type="submit" value="保存"><br>
    </form>
    </body>
    </html>
  • 编写列表页面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <h1>展示账户数据列表</h1>
    <table border="1">
    <tr>
    <th>账户id</th>
    <th>账户名称</th>
    <th>账户金额</th>
    </tr>

    <c:forEach items="${accountList}" var="account">
    <tr>
    <td>${account.id}</td>
    <td>${account.name}</td>
    <td>${account.money}</td>
    </tr>
    </c:forEach>

    </table>
    </body>
    </html>
  • 编写相应配置文件

    • Spring配置文件:applicationContext.xml
    • SprngMVC配置文件:spring-mvc.xml
    • MyBatis映射文件:AccountMapper.xml
    • MyBatis核心文件:sqlMapConfig.xml
    • 数据库连接信息文件:jdbc.properties
    • Web.xml文件:web.xml
    • 日志文件:log4j.xml
  • 测试添加账户

  • 测试账户列表

Spring整合MyBatis

  • 整合思路
  • 将SqlSessionFactory配置到Spring容器中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <!--加载properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--配置数据源信息-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--配置sessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <!--加载mybatis核心文件-->
    <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
    </bean>
  • 扫描Mapper,让Spring容器产生Mapper实现类

    1
    2
    3
    4
    <!--扫描mapper所在的包 为mapper创建实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.itheima.mapper"></property>
    </bean>
  • 配置声明式事务控制

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <!--声明式事务控制-->
    <!--平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务增强-->
    <tx:advice id="txAdvice">
    <tx:attributes>
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>

    <!--事务的aop织入-->
    <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
    </aop:config>
  • 修改Service实现类代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Service("accountService")
    public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void save(Account account) {
    accountMapper.save(account);
    }

    @Override
    public List<Account> findAll() {
    return accountMapper.findAll();
    }
    }
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022 ZHU
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信