springboot整合ssm詳細講解
SSM是企業中廣泛應用的框架。大家再熟練地使用SSM進行業務邏輯開發的時候,也被它大量的xml配置困擾。今
天快速優雅的使用SpringBoot實現簡易的SSM工程。廢話不多說,come on
開發工具idea
1.建立一個web工程,pom.xml中加入如下配置:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--使用jsp頁面--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build>
2.工程結構
3.資料庫和實體類
資料庫使用mysql,建立部門表dept,表結構很簡單,id主鍵和name部門名稱。
4.dao層
dao的程式碼我們演示兩種方式,一種使用傳統的Mapper對映檔案,一種使用註解編寫sql:
package com.test.springboot.ssm.dao;
import com.test.springboot.ssm.pojo.Dept;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import java.util.List;
public interface DeptDAO {
//查詢列表,演示使用傳統的mapper對映檔案
List<Dept> getDeltList();
//插入,演示使用註解編寫sql,省略xml配置
@Insert("insert into DEPT(NAME) values(#{name})")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "ID")
void addDept(String name);
}
mapper對映檔案如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-
mapper.dtd" >
<mapper namespace="com.test.springboot.ssm.dao.DeptDAO">
<resultMap id="deptMap" type="Dept">
<id property="id" column="ID"/>
<result property="name" column="NAME"/>
</resultMap>
<select id="getDeltList" resultMap="deptMap">
select ID,NAME from DEPT
</select>
</mapper>
注意mapper的namespace是DAO介面的全路徑,mapper中語句的id與DAO介面中的方法名是一致的
5.service層
介面:
package com.test.springboot.ssm.service;
import com.test.springboot.ssm.pojo.Dept;
import java.util.List;
public interface DeptService {
List<Dept> getDeltList();
void addDept(String name);
}
實現類;
package com.test.springboot.ssm.service.impl;
import com.test.springboot.ssm.dao.DeptDAO;
import com.test.springboot.ssm.pojo.Dept;
import com.test.springboot.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDAO deptDAO;
@Override
public List<Dept> getDeltList() {
return deptDAO.getDeltList();
}
@Override
public void addDept(String name) {
deptDAO.addDept(name);
}
}
6.controller層
package com.test.springboot.ssm.controller;
import com.test.springboot.ssm.pojo.Dept;
import com.test.springboot.ssm.service.DeptService;
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
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("list.html")
public ModelAndView list() {
List<Dept> deptList = deptService.getDeltList();
return new ModelAndView("list", "deptList", deptList);
}
@RequestMapping("add.html")
public String add(String name) {
deptService.addDept(name);
//新增成功後重定向到列表頁
return "redirect:list.html";
}
}
jsp頁面
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="/add.html" method="post">
部門名:<input type="text" name="name"/><br/>
<input type="submit" value="add"/>
</form>
</body>
</html>
list.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<c:forEach items="${deptList}" var="dept">
${dept.id}-${dept.name}<br/>
</c:forEach>
</body>
</html>
貌似到目前為止,編碼的形式和我們開發普通的SSM沒什麼區別,不要著急,重點來了。傳統的ssm中我們需需要
在spring.xml中配置資料來源、宣告式事務、mybatis配置資訊、註解掃描等。使用SpringBoot,我們可以省略掉所
有的xml。沒錯,就是一個配置檔案都沒有,廢話少說,上程式碼:
SpringBoot有個全域性的屬性配置檔案application.properties
#資料來源的基本資訊
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#mybatis中mapper檔案的路徑
mybatis.mapper-locations=classpath*:com/test/springboot/ssm/dao/mappers/*.xml
#起別名。可省略寫mybatis的xml中的resultType的全路徑
mybatis.type-aliases-package=com.test.springboot.ssm.pojo
#springMVC中的檢視資訊,響應字首
spring.mvc.view.prefix=/
# 響應頁面預設字尾
spring.mvc.view.suffix=.jsp
#DispatcherServlet中響應的url-pattern
server.sevlet-path=*.html
7.入口類
package com.test.springboot.ssm;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement//開啟事務管理
@ComponentScan("com.test.springboot.ssm")//掃描註解元素
@MapperScan("com.test.springboot.ssm.dao")//Mybatis的DAO所在包
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
對比發現,SpringBoot可以省略大量的xml配置檔案,幾個註解輕鬆代替繁瑣的配置。為了方便大家接受
SpringBoot,本文中的示例使用的頁面模板引擎還是jsp。Spring官網推薦使用Thymeleaf 作為頁面模板引擎,後
續的學習中