SSM框架整合

1.1 原始方式整合

1.准备工作

07_SSM整合 - 图1

2.创建Maven工程

07_SSM整合 - 图2

3.导入Maven坐标

参考:素材/配置文件/pom.xml文件

4.编写实体类

  1. public class Account {
  2. private int id;
  3. private String name;
  4. private double money;
  5. //省略getter和setter方法
  6. }

5.编写Mapper接口

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

6.编写Service接口

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

7.编写Service接口实现

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

8.编写Controller

  1. @Controller
  2. public class AccountController {
  3. @Autowired
  4. private AccountService accountService;
  5. @RequestMapping("/save")
  6. @ResponseBody
  7. public String save(Account account){
  8. accountService.save(account);
  9. return "save success";
  10. }
  11. @RequestMapping("/findAll")
  12. public ModelAndView findAll(){
  13. ModelAndView modelAndView = new ModelAndView();
  14. modelAndView.setViewName("accountList");
  15. modelAndView.addObject("accountList",accountService.findAll());
  16. return modelAndView;
  17. }
  18. }

9.编写添加页面

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

10.编写列表页面

  1. <table border="1">
  2. <tr>
  3. <th>账户id</th>
  4. <th>账户名称</th>
  5. <th>账户金额</th>
  6. </tr>
  7. <c:forEach items="${accountList}" var="account">
  8. <tr>
  9. <td>${account.id}</td>
  10. <td>${account.name}</td>
  11. <td>${account.money}</td>
  12. </tr>
  13. </c:forEach>
  14. </table>

11.编写相应配置文件(文件参考目录:素材/配置文件)

•Spring配置文件:applicationContext.xml

•SprngMVC配置文件:spring-mvc.xml

•MyBatis映射文件:AccountMapper.xml

•MyBatis核心文件:sqlMapConfig.xml

•数据库连接信息文件:jdbc.properties

•Web.xml文件:web.xml

•日志文件:[log4j.xml](

12.测试添加账户

07_SSM整合 - 图3

13.测试账户列表

07_SSM整合 - 图4

1.2 Spring整合MyBatis

1.整合思路

07_SSM整合 - 图5

2.将SqlSessionFactory配置到Spring容器中

  1. <!--加载jdbc.properties-->
  2. <context:property-placeholder location="classpath:jdbc.properties"/>
  3. <!--配置数据源-->
  4. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  5. <property name="driverClass" value="${jdbc.driver}"/>
  6. <property name="jdbcUrl" value="${jdbc.url}"/>
  7. <property name="user" value="${jdbc.username}"/>
  8. <property name="password" value="${jdbc.password}"/>
  9. </bean>
  10. <!--配置MyBatis的SqlSessionFactory-->
  11. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  12. <property name="dataSource" ref="dataSource"/>
  13. <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
  14. </bean>

3.扫描Mapper,让Spring容器产生Mapper实现类

  1. <!--配置Mapper扫描-->
  2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  3. <property name="basePackage" value="com.itheima.mapper"/>
  4. </bean>

4.配置声明式事务控制

  1. <!--配置声明式事务控制-->
  2. <bean id="transacionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"/>
  4. </bean>
  5. <tx:advice id="txAdvice" transaction-manager="transacionManager">
  6. <tx:attributes>
  7. <tx:method name="*"/>
  8. </tx:attributes>
  9. </tx:advice>
  10. <aop:config>
  11. <aop:pointcut id="txPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
  12. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  13. </aop:config>

5.修改Service实现类代码

  1. @Service("accountService")
  2. public class AccountServiceImpl implements AccountService {
  3. @Autowired
  4. private AccountMapper accountMapper;
  5. public void save(Account account) {
  6. accountMapper.save(account);
  7. }
  8. public List<Account> findAll() {
  9. return accountMapper.findAll();
  10. }
  11. }