SpringBoot2整合SSM框架详解
发布时间:2019-01-15 21:33,
浏览次数:1218
, 标签:
SpringBoot
SSM
<>开发环境
* 开发工具:Eclipse + STS插件
* JDK版本:9.0.4
* MySQL版本:8.0.12
* Spring Boot版本:2.1.2
<>1、创建Spring Boot工程
<> (1)工程创建过程
<> (2)工程目录结构
注:工程创建完成后,缺少的文件夹需手动补全。
<> (3)添加额外依赖工程
所依赖的jar包和插件由工程创建过程中所选的组件自动生成。由于本工程需要用到JSP视图,需要在pom.xml中手动添加额外依赖:
org.apache.tomcat.embed
tomcat-embed-jasper
javax.servletjstl
dependency>
<> (4)配置application.yml文件
注:Spring Boot工程默认的配置文件为application.properties,可选中配置文件,单击鼠标右键,选中Convert
.properties to .yaml后,转为application.yml文件。
<>application.yml:
#服务器配置 server: port: 8090 #spring配置 spring: #数据源配置 datasource: #配置mysql数据库
driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:
//localhost/customer_manager?serverTimezone=CTT username: root password: 1314
#配置dbcp连接池 dbcp2: connection-init-sqls: characterEncoding=utf-8 initial-size: 5
max-idle: 100 max-wait-millis: 10000 min-idle: 5 test-on-borrow: true
test-while-idle: true time-between-eviction-runs-millis: 27800 #配置JSP视图 mvc:
view: prefix: /WEB-INF/jsp/ suffix: .jsp #配置mybatis框架 mybatis: #定义Mapper接口映射文件位置
mapper-locations: classpath:mapper/*.xml #定义实体类位置 type-aliases-package:
com.ming.ssm.pojo#控制台打印sql语句 logging: level: com.ming.ssm.mapper: debug
<>2、持久层实现
<>customer_manager数据库中customer表的构建:
<>Customer:
package com.ming.ssm.pojo; import java.io.Serializable; /** *
数据库(customer_manager)中表(customer)所对应的实体类(Customer) * @author Mr.F * */ public
class Customer implements Serializable{ private static final long
serialVersionUID= 1L; private Long c_id; private String c_name; private String
c_password; private String c_address; private String c_phone; private String
c_email; public Long getC_id() { return c_id; } public void setC_id(Long c_id) {
this.c_id = c_id; } public String getC_name() { return c_name; } public void
setC_name(String c_name) { this.c_name = c_name; } public String getC_password()
{ return c_password; } public void setC_password(String c_password) { this.
c_password= c_password; } public String getC_address() { return c_address; }
public void setC_address(String c_address) { this.c_address = c_address; }
public String getC_phone() { return c_phone; } public void setC_phone(String
c_phone) { this.c_phone = c_phone; } public String getC_email() { return c_email
; } public void setC_email(String c_email) { this.c_email = c_email; } }
<>CustomerMapper:
package com.ming.ssm.mapper; import java.util.List; import org.apache.ibatis.
annotations.Mapper; import com.ming.ssm.pojo.Customer; /** *
持久层实现Mybatis框架中的Mapper接口,声名对数据库的操作方法 * @author Mr.F * */ @Mapper public
interface CustomerMapper { List findAllCustomer(); //查询所有客户信息 }
<>CustomerMapper.xml:
<?xml version="1.0" encoding="UTF-8"?> /p>
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="com.ming.ssm.mapper.CustomerMapper">
SELECT * FROM `customer`
<>3、业务层实现
<>CustomerService:
package com.ming.ssm.service; import java.util.List; import com.ming.ssm.pojo.
Customer; /** * 定义业务层接口 * @author Mr.F * */ public interface CustomerService {
List findAllCustomer(); //查询所有客户信息 }
<>CustomerServiceImpl:
package com.ming.ssm.service.impl; import java.util.List; import org.
springframework.beans.factory.annotation.Autowired; import org.springframework.
stereotype.Service; import com.ming.ssm.mapper.CustomerMapper; import com.ming.
ssm.pojo.Customer; import com.ming.ssm.service.CustomerService; /** *
实现业务层的CustomerService接口 * @author Mr.F * */ @Service public class
CustomerServiceImpl implements CustomerService{ @Autowired private
CustomerMapper customerMapper; @Override public List findAllCustomer()
{ return customerMapper.findAllCustomer(); } }
<>4、控制层实现
<>CustomerController:
package com.ming.ssm.controller; import java.util.List; 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 com.ming.ssm.pojo.
Customer; import com.ming.ssm.service.CustomerService; /** * 定义控制层类 * @author
Mr.F * */ @Controller public class CustomerController { @Autowired private
CustomerService customerService; @RequestMapping("findAll") public String
findAll(Model model) { List list = customerService.findAllCustomer();
model.addAttribute("list", list); return "index"; } }
<>index.jsp:
pageEncoding="UTF-8"%>
prefix="c"%>/p>
"http://www.w3.org/TR/html4/loose.dtd">
客户管理页面客户管理系统
客户ID | 客户姓名 | 客户密码 | 客户地址 td> | 客户手机 | 客户邮箱 |
align="center">
${c.c_id} ${c.c_name} ${c.c_password}${c.c_address } ${c.c_phone } ${c.c_email }c:forEach>
<>5、工程测试
在该工程下找到Spring Boot入口类:SpringbootCustomerApplication.java,单击鼠标右键,选中Run As
-->Spring Boot App运行。在浏览器地址栏访问http://localhost:8090/findAll,其结果如下: