效果
视屏
实现增删查改,多表关联,适合新手拿来,熟悉SSM框架。
想要源码和数据库SQL,三连私聊我,我发给你。
整体架构流程
后端框架:SSM框架,Spring+Spring MVC+Mybatis。
前端:Element,Axios等。
一、准备
工具:IDEA2021
本地仓库:maven3.6.1
二、项目搭建
1、软件页面选择maven项目。
2、配置好的maven后,再点击IDEA的file,选择setting,在搜索maven,选择你的maven,进行设置。
我的参考图
3、导入依赖pom.xml。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>itcast-health</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.7.3</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--SpringBoot单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--web起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<!-- fastjson 手动把对象转成JSON -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--字符串处理工具类包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- apache poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- 七牛云 -->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.0</version>
</dependency>
<!-- 阿里云短信包 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.4</version>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 定时任务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- itext 生成pdf -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<!-- jasperreports 跟强大的生成pdf -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
<exclusions>
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
4、建立相应文件夹,以及启动类Application。
4.1、Application类
package com.itheima.health;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j//日志注解
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
log.info("项目启动成功!");
}
}
4.2、applicatyion.yml放在resource根目录下,这个目录这样放静态资源。
spring:
# 数据库连接池的配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/health?useSSL=false
username: root
password: 123456
# MyBatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml #映像设置
type-aliases-package: com.itheima.health.pojo #包扫描
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #Mybatis 日志打印
map-underscore-to-camel-case: true #配置驼峰标识 user_id --->userId
# Redis配置
redis:
port: 6379
host: localhost
server:
port: 808
听我解释:mapper-locations: classpath:mapper/*.xml #映像设置
为什么要配置这个呢?当然是映像在持久层DAO用配置文件与数据库联系,形成映射。我举一个例子吧。
这里就是使用.XML的例子,并没有使用注解。
然后就mapper-locations: classpath:mapper/*.xml就映射mapper包下的所有 .xml文件
映射完了就到下面了,type-aliases-package: com.itheima.health.pojo #包扫描下的所有实体类,找到对应类。这就是关于基于.xml操作数据库的全部。要是只用注解完全可以不配这两项。还有一个插件交TK-mabatis-***,这个使得曾删查改就更简单了,它的jar包里都增删查改封装好了,我都不想写增删查改了。
4.3、目录结构
三、开干?
一、处理流程图。
二、实现检查组增增删查改,实现思路。
1、Controller包在创建一个类CheckItemController,在其类上面加上@RestController,标识为,标识为控制层。
上图说了,Controller层是接受前段的请求的,那么前段的请求怎么知道要向后端请求哪里呢?当然请求路劲了,这时候后端就用到了一个注解@RequestMapping("/checkitem")//Url,路径请求。
前段请求图
后端图
这样他们就建立联系,前端请求就会更根据路劲来查请求后端。
3、那么问题又来了?控制层怎么调用业务层呢?业务层又怎么调用持久层呢?当然是通过对象了。这时候我们又要用到一个注释@Autowired,依赖注入,就是注入一个对象的意思,这样就可以实现层层调用。
控制层与业务层联系图
业务层与持久层联系图
4、记得给业务层的类加上注解@Service,也是标识的意思。持久层类上也要加注@Mapper //表示当接口层是持久层,被Spring扫描,管理
5、 结束
嗷嗷嗷嗷,就没了。
上代码,检查项的增删查改。
1、控制层。
package com.itheima.health.controller;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckltemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@Controller //标识为控制层,
//@ResponseBody //返回值都会转化为JSON数据
@RestController
@RequestMapping("/checkitem")//Url,路径请求
public class CheckItemController {
@Autowired
private CheckltemService checkltemService;
/**
* 添加数据项
* @param checkItem 前端数据
* @return //响应结果 404后端与前端一致,405请求方式错误,800SQL语句错误,200成功
*/
@PostMapping("/add.do")
//@RequestBody 接受前端的数据项,并把前端数据JSON转为Java对象
public Result add(@RequestBody CheckItem checkItem){
//1.调用service接口,业务层
//Java调用 对象(.)方法名。调用方法
boolean flag=checkltemService.add(checkItem);
if(flag){
return new Result(true,"添加检查项成功");
}
return new Result(false,"添加检查项失败");
}
/**
* 查询检查项数据
* @param queryPageBean
* @return
*/
@PostMapping("/findPage.do")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
//查询que 提交的分页条件 里面有当前页 显示条数 过滤条件
PageResult pageResult=checkltemService.findPage(queryPageBean);
//2.响应分页数据的结果
return pageResult;
}
/**
* id查询
* @param id
* @return
*/
@GetMapping("/findById.do")
public Result findById(Integer id){
//1。ID查询
CheckItem checkItem=checkltemService.findById(id);
//2.
if(checkItem!=null){
return new Result(true,"回现数据查询成功",checkItem);
}
return new Result(false,"查询数据不存在");
}
/**
*修改
* @param checkItem 修改前端的数据
* @return 响应结果
*/
@PostMapping("/edit.do")
//@RequestBody 接受前端的数据项,并把前端数据JSON转为Java对象
public Result edit(@RequestBody CheckItem checkItem){
//1.调用service接口,业务层
//Java调用 对象(.)方法名。调用方法
boolean flag=checkltemService.edit(checkItem);
if(flag){
return new Result(true,"修改成功");
}
return new Result(false,"修改失败");
}
/**
* id删除
* @param id
* @return
*/
@GetMapping("/delete.do")
public Result delete(Integer id){
//1。ID查询
int i=checkltemService.Count(id);
//2.
if(i==0){
int d=checkltemService.D(id);
return new Result(true,"删除成功");
}
return new Result(false,"删除失败");
}
@GetMapping("/delete2.do")
public Result delete2(Integer id){
//1。ID查询
boolean flag=checkltemService.delete2(id);
//2.
if(flag){
return new Result(true,"删除成功");
}
return new Result(false,"删除失败");
}
/**
* 查询所有检查项数据
* @return
*/
@GetMapping("/findAll.do")
public Result findAll(){
List<CheckItem> list=checkltemService.findAll();
//返回list
return new Result(true,"查询所有检查项数据",list);
}
}
2、业务层
service层接口类。
package com.itheima.health.service;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import java.util.List;
public interface CheckltemService {
/**
*
* @param checkItem
* @booolean 成功与失败
*/
boolean add(CheckItem checkItem);
//分页查询
PageResult findPage(QueryPageBean queryPageBean);
/**
*
* @param id
* @return
*/
CheckItem findById(Integer id);
boolean edit(CheckItem checkItem);
/**
* 删除
* @param id
* @return
*/
int Count(Integer id);
/**
*
* @param id
* @return
*/
int D(Integer id);
/**
* 删除2
* @param id
* @return
*/
boolean delete2(Integer id);
/**
* 查询所有检查项
* @return
*/
List<CheckItem> findAll();
}
service层实类现
package com.itheima.health.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckltemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service//标识当前类是业务类
public class CheckltemServicelmpl implements CheckltemService {
@Autowired//从Spring获取对象,根据接口依赖注入
private CheckItemDao checkItemDao;
@Override
public boolean add(CheckItem checkItem) {
int i=checkItemDao.add(checkItem);
if (i>0){
return true;
}
return false;
}
/**
* 查询
* @param queryPageBean
* @return
*/
@Override
public PageResult findPage(QueryPageBean queryPageBean) {
//1.设置分页,分页插件参数
PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//2.调用DAO,查询语句,回传给分页插件,进行分页(查询条件)
Page<CheckItem> page=checkItemDao.findPage(queryPageBean.getQueryString());
//返回分页对象(总数,分页结果)'
return new PageResult(page.getTotal(), page.getResult());
}
/**
* ID查询
* @param id
* @return
*/
@Override
public CheckItem findById(Integer id) {
//1.调用DAO
return checkItemDao.findById(id);
}
/**
* ID 修改
* @param checkItem
* @return
*/
@Override
public boolean edit(CheckItem checkItem) {
int i=checkItemDao.edit(checkItem);
if (i>0){
return true;
}
return false;
}
/**
* 查询关联
* @param id
* @return
*/
@Override
public int Count(Integer id) {
return checkItemDao.Count(id);
}
@Override
public int D(Integer id) {
return checkItemDao.D(id);
}
/**
*
* @param id
* @return
*/
@Override
public boolean delete2(Integer id) {
//查询count
long count=checkItemDao.findCount(id);
if(count>0){
//
return false;
}
//删除
int i=checkItemDao.deleteById(id);
if(i>0){
return true;
}
return false;
}
/**
* 检查所有检查项
* @return
*/
@Override
public List<CheckItem> findAll() {
//
return checkItemDao.findAll();
}
}
Dao持久层。
package com.itheima.health.dao;
import com.github.pagehelper.Page;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper //表示当接口层是持久层,被Spring扫描,管理
//Mybatis动态创建其对象放到Spring
public interface CheckItemDao {
/**
*
* @param checkItem 业务过来到检查项
* @return 相应几条数据受影响
*
*
* private Integer id; // 主键
* private String code; // 项目编码
* private String name; // 项目名称
* private String sex; // 适用性别
* private String age; // 适用年龄(范围),例如:20-50
* private Float price; // 价格
* private String type; // 检查项类型,分为检查和检验两种类型
* private String remark; // 项目说明
* private String attention; // 注意事项
*/
@Insert("insert into t_checkitem values(null,#{code},#{name},#{sex},#{age},#{price},#{type},#{remark},#{attention})")
int add(CheckItem checkItem);
/**
*
* @param queryString
* @return
*/
Page<CheckItem> findPage(String queryString);
/**
*
* @param id
* @return
*/
@Select("select * from t_checkitem where id=#{id}")
CheckItem findById(Integer id);
/**
* 修改
* @param checkItem
* @return
*/
int edit(CheckItem checkItem);
/**
* 统计
* @param id
* @return
*/
@Select("select count(*) from t_checkgroup_checkitem where checkitem_id=#{id}")
int Count(Integer id);
/**
* 删除
* @param id
* @return
*/
@Delete("delete from t_checkitem where id=#{id}")
int D(Integer id);
/
@Select("SELECT COUNT(*) FROM t_checkgroup_checkitem WHERE checkitem_id=#{id} ")
long findCount(Integer id);
//删除
@Delete("delete from t_checkitem where id=#{id}")
int deleteById(Integer id);
/**
*查询所有检查项,404资源访问不到,
* (返不回数据,路径错误,访问数据库)
* @return
*/
@Select("select * from t_checkitem")
List<CheckItem> findAll();
}
小结
1、Controller层:Controller层负责具体的业务模块流程的控制。
2、View层:View层与控制层结合⽐较紧密,需要⼆者结合起来协同⼯发。View层主要负责前台jsp页⾯的表⽰。
3、Service层主要负责业务模块的逻辑应⽤设计。
4、DAO层:DAO层主要是做数据持久层的⼯作,负责与数据库进⾏联络的⼀些任务都封装在此。
注意:1、后端和前端的请求(参数)命名一定要相同。
2、从请求从控制层开始写起,IDEA会有提示,这样我们才不会更容易放错。
没学之前,是真的不懂,好高深啊,学了之后.............*********..........