ssm整合之Spring整合MyBatis框架
1.在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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="com.txw">
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--Spring整合MyBatis框架-->
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置AccountDao接口所在包-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.txw.dao"/>
</bean>
</beans>
2.修改账户持久层接口的代码如下:
package com.txw.dao;
import com.txw.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
*帐户dao接口
* @author Adair
*/
@Repository // 把Dao交给IOP进行管理
@SuppressWarnings("all") // 注解警告信息
public interface AccountDao {
/**
*查询所有账户
* @return
*/
@Select("select * from account")
public List<Account> findAll();
/**
*保存帐户信息
* @param account
*/
@Insert("insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
}
3.在账户业务层的实现类修改如下代码:
package com.txw.service.impl;
import com.txw.dao.AccountDao;
import com.txw.domain.Account;
import com.txw.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 账户业务的实现类
* @author Adair
*/
@Service("accountService")
@SuppressWarnings("all") // 注解警告信息
public class AccountServiceImpl implements AccountService {
@Autowired //类型自动注入
private AccountDao accountDao;
/**
* 查询所有账户
* @return
*/
@Override
public List<Account> findAll() {
System.out.println("业务层:查询所有账户...");
return accountDao.findAll();
}
/**
* 保存帐户信息
* @param account
*/
@Override
public void saveAccount(Account account) {
System.out.println("业务层:保存帐户信息...");
accountDao.saveAccount(account);
}
}
4.修改帐户的控制类的代码如下:
package com.txw.controller;
import com.txw.domain.Account;
import com.txw.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
*帐户的控制类
* @author Adair
*/
@Controller
@RequestMapping(path = "/account")
@SuppressWarnings("all") // 注解警告信息
public class AccountController {
@Autowired // 自动类型注入
private AccountService accountService;
@RequestMapping(value = "/findAll")
public String findAll(Model model){
System.out.println("表现层:查询所有账户...");
// 调用findAll()方法
List<Account> list = accountService.findAll();
// 进行存储
model.addAttribute("list",list);
return "list";
}
}
5.修改list.jsp的代码如下:
<%--
Created by IntelliJ IDEA.
User: Adair
Date: 2020/7/8 0008
Time: 14:35
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
<h3>查询所有的帐户</h3>
<<c:forEach items="${list}" var="account">
${
account.name}
${
account.money}
</c:forEach>
</body>
</html>
6.重新部署项目,运行结果如图所示:
7.通过浏览器访问http://localhost:8080/如图所示:8.点击测试会跳转到如图所示的界面:
9.控制台打印结果如图所示: