此處的整合是一次比較簡單的整合,並沒有用到工程的拆分和聚合思想。
1.搭建簡單的整合環境
整合思路
1.先搭建整合環境,此處直接通過骨架創建web工程
2.完成Spring的搭建配置
3.使用Spring整合SpringMVC
4.使用Spring整合Mybatis
示例代碼
創建數據庫和表結構
create database ssm;
create table account(
id int primary key auto_increment,
name varchar(20),
money double(7,2)
);
編寫實體類
package com.liang.domain;
import java.io.Serializable;
/**
* 賬戶實體類
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
編寫業務層接口
package com.liang.service;
import com.liang.domain.Account;
import java.util.List;
/**
* 賬戶的業務層接口
*/
public interface AccountService {
/**
* 查詢所有賬戶
* @return
*/
public List<Account> findAll();
/**
* 保存賬戶
* @param account
*/
public void saveAccount(Account account);
}
編寫持久層接口
package com.liang.dao;
import com.liang.domain.Account;
import java.util.List;
/**
* 賬戶的持久層接口
*/
public interface AccountDao {
/**
* 查詢所有賬戶
* @return
*/
List<Account> findAll();
/**
* 保存賬戶
*/
void saveAccount(Account account);
}
編寫Spring代碼,保證其能夠獨立運行
編寫spring配置文件(spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置spring創建容器時要掃描的包-->
<context:component-scan base-package="com.liang"></context:component-scan>
</beans>
使用註解配置業務層和持久層
package com.liang.service.impl;
import com.liang.dao.AccountDao;
import com.liang.domain.Account;
import com.liang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 賬戶的業務層實現類
*/
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
public AccountDao accountDao;
public List<Account> findAll(){
System.out.println("AccountServiceImpl ——> findAll方法執行了...");
return accountDao.findAll();
}
@Override
public void saveAccount(Account account) {
System.out.println("AccountServiceImpl ——> saveAccount方法執行了...");
accountDao.saveAccount(account);
}
}
package com.liang.dao.impl;
import com.liang.dao.AccountDao;
import com.liang.domain.Account;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 賬戶的持久層實現類
*/
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {
@Override
public List<Account> findAll() {
System.out.println("查詢所有用戶");
return null;
}
@Override
public void saveAccount(Account account) {
System.out.println("保存賬戶");
}
}
測試spring獨立運行
package com.liang.test;
import com.liang.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 測試spring環境
*/
public class SpringTest {
@Test
public void springTest(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = (AccountService) applicationContext.getBean("accountService");
accountService.findAll();
System.out.println("測試完畢");
}
}
所需jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
編寫SpringMVC代碼,保證其能夠獨立運行
在web.xml中配置核心控制器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置SpringMVC的核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化參數:用於讀取SpringMVC的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置SpringMVC編碼過濾器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--設置過濾器中的屬性值-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--過濾所有請求-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
編寫SpringMVC的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置需要掃描的包-->
<context:component-scan base-package="com.liang.controller">
<!--表示只掃描@Controller註解的java類-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
編寫jsp和Controller
index.jsp頁面代碼
<%@page contentType="text/html; charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<body>
<h2>賬戶操作</h2>
<a href="SSMtest/findAllAccount">查詢所有賬戶</a><br/><br/><br/>
<form action="SSMtest/saveAccount" method="post">
姓名:<input type="text" name="name"><br/>
金額:<input type="text" name="money"><br/>
<input type="submit" value="保存"><br/>
</form>
</body>
</html>
WEB-INF/pages/success.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>
執行成功!!!<br/><br/>
<c:forEach items="${accounts}" var="account">
<c:out value="${account.id}"></c:out>
<c:out value="${account.name}"></c:out>
<c:out value="${account.money}"></c:out><br/>
</c:forEach>
</body>
</html>
控制器代碼
package com.liang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(path = "/SSMtest")
public class AccountController {
@RequestMapping(path = "/findAllAccount")
public ModelAndView findAllAccount(){
System.out.println("controller findAll方法執行了");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(path = "/saveAccount")
public String saveAccount(Account account){
System.out.println("controller findAll方法執行了");
return "redirect:/SSMtest/findAllAccount";
}
}
所需jar包
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
使用Spring整合SpringMVC
目的:在controller中能夠成功的調用service對象中的方法
思路:在項目啓動的時候,就加載spring的配置文件,則需要在web.xml中配置ContextLoaderListener監聽器(該監聽器只能加載WEB-INF目錄下的名爲applicationContext.xml配置文件)
修改web.xml文件(添加監聽器代碼)
<!--配置spring的監聽器:用於啓動服務時加載容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
修改spring配置文件
<!--配置spring創建容器時要掃描的包-->
<context:component-scan base-package="com.liang">
<!--表示不掃描@Controller註解的java類-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
修改控制器代碼
package com.liang.controller;
import com.liang.domain.Account;
import com.liang.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping(path = "/SSMtest")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(path = "/findAllAccount")
public ModelAndView findAllAccount(){
System.out.println("controller findAll方法執行了");
List<Account> accounts = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accounts",accounts);
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(path = "/saveAccount")
public String saveAccount(Account account){
System.out.println("controller findAll方法執行了");
accountService.saveAccount(account);
return "redirect:/SSMtest/findAllAccount";
}
}