一.脚手架安装
1.1安装
1.2导入项目
说明: 前端的完整项目在码云资源文件中. 用户将资源下载到本地 通过vue-cli客户端导入即可.
路径说明
挑选路径 点击导入即可
运行项目效果如图
二. MybatisPlus 学习
2.1 MP入门案例
package com.jt;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
public class TestMP {
@Autowired
private UserMapper userMapper;
/**
* 完成数据的入库操作
* 新增user数据(name="afh",age=40,sex="厉害")
* Sql: insert into demo_user value(xx,xx,xx,xx)
* 思考: MP实现入库流程!!!
* 选择A: 讲mp入库流程 理论为主.
*/
@Test
public void test01(){
User user = new User();
user.setName("afh").setAge(40)
.setSex("厉害");
//以对象的方式操作数据!!!
userMapper.insert(user);
System.out.println("入库操作成功!!!!");
}
/**
* 以mybatis的方式实现数据库查询
* 1.实现user入库操作 insert into
* 2.update 将name="afh" 改为 name="tlb"
* 3.delete 将name="tlb"数据删除.
* 4.select 查询 name="小乔" 并且 性别 ="女"
* 5.select 查询age < 18岁 性别="女"
* 6.select 查询 name包含 '君'字的数据
* 7.select 查询 sex="女" 按照年龄倒序排列.
* 8.根据 name/sex 不为null的数据查询. 动态Sql!!
* name="xxx" sex=null
* name="xxx" sex="xx"
* name=null sex=null
*/
//1.查询ID查询数据库 id=231 主键查询
@Test
public void selectById(){
int id = 231; //模拟用户参数.
User user = userMapper.selectById(id);
System.out.println(user);
}
/**
* 2.查询 name="小乔" 并且 性别 ="女"
* 思路: 如果将来有多个结果 则使用List进行接收.
* Sql: select * from demo_user where name="小乔" and sex="女"
* 注意事项: 默认的连接符 and
*/
@Test
public void select01(){
//1.通过对象封装数据
User user = new User();
user.setName("小乔").setSex("女");
//2.构建条件构造器 根据对象中不为null的属性充当where条件!
QueryWrapper<User> queryWrapper = new QueryWrapper(user);
//3.根据条件构造器 实现数据查询
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 3.查询 name="小乔" 并且 性别 ="女"
* 逻辑运算符: = eq, > gt, < lt
* >= ge, <= le
* != ne
*/
@Test
public void select02(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name","小乔")
.eq("sex","女");
List<User> userList =
userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 4.案例: select 查询age < 18岁 性别="女"
* 方式1: 利用mybatis方式实现
* 方式2: 利用MP方式实现
*/
@Test
public void select03(){
//1.
List<User> userList = userMapper.findList(18,"女");
System.out.println(userList);
//2.mp写法:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lt("age",18)
.eq("sex","女");
List<User> userList2 = userMapper.selectList(queryWrapper);
System.out.println(userList2);
}
/**
* 5.select 查询 name包含 '君'字的数据
* 关键字: like "%xxx%"
* 以君开头: likeRight "君%"
* 以君结尾: likeLeft "%君"
*/
@Test
public void select04(){
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.like("name","君");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 6.select 查询 sex="女" 按照年龄倒序排列.
*/
@Test
public void select05(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex", "女").orderByDesc("age");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 7.需求:动态sql查询,如果数据有值,则拼接where条件
* 如果数据为null,则不拼接where条件
* 语法:condition:true 则拼接where条件
* false 不拼接where条件
*
*/
@Test
public void select06(){
String name = "貂蝉";
int age = 18;
boolean nameFlag = name == null ? false :true;
boolean ageFlag = age == 0 ? false : true;
QueryWrapper<User> queryWrapper = new QueryWrapper();
queryWrapper.eq(nameFlag,"name",name)
.eq(ageFlag,"age",age);
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 8.批量查询 查询id = 1,4,5,6……的数据
*
*/
@Test
public void selectIn(){
//1.方式1:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id",1,4,5,6);
//queryWrapper.in("id","1,4,5,6");
List<User> userList1 = userMapper.selectList(queryWrapper);
System.out.println(userList1);
//2.方式2:
// List<Integer> ids = new ArrayList<>();
// ids.add(1);
// ids.add(4);
// ids.add(5);
// ids.add(6);
// List<User> userList2 = userMapper.selectBatchIds(ids);
// System.out.println(userList2);
//3.优化方式2
//数组在未来由用户负责传递
Integer[] array = new Integer[]{1,4,5,6};
//数组转化为List集合??
List<Integer> ids = Arrays.asList(array);
List<User> userList2 = userMapper.selectBatchIds(ids);
System.out.println(userList2);
}
/**
* 9.查询性别为男的用户,只查询ID字段
* selectObjs(),只查询第一个字段(主键)
* 实际用途:根据业务只需要主键的查询
*/
@Test
public void selectObjs(){
//构建条件构造器
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex", "男");
//根据条件构造器,实现对数据的查询
List<Object> ids = userMapper.selectObjs(queryWrapper);
System.out.println(ids);
}
}
2.2关于查询总结
- MP核心:以对象的方式操作数据库
- 条件构造器:new QueryWrapper<>();动态拼接where条件,
- 拼接规则:根据对象不为null的属性充当where条件
- 特殊转义字符: =eq, >gt , < lt, >= ge, <= le, != ne
- xml文件中的万能的转义字符<![CDATA[ SQL语句 ]]>
- 关键字:like、order By 、in、……
- 动态sql的语法:
condition:true 拼接where 条件
false 不拼接where条件
2.3 MP更新操作
package com.jt;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestUpdate {
@Autowired
private UserMapper userMapper;
/**
* 1.将ID=231的数据 name改为 "xx"
* Sql: update demo_user set name="xx" where id=231
*/
@Test
public void testUpdate(){
User user = new User();
user.setId(231).setName("中秋节快乐").setAge(10).setSex("男");
//byId 表示ID只当作where条件.
//其它不为null的属性 当作set条件
userMapper.updateById(user);
}
/**
* 2.将name="中秋节快乐" 改为 name="国庆快乐" age=40 sex="男"
* .update(arg1,arg2)
* arg1: 实体对象 set条件的数据
* arg2: updateWrapper 动态拼接where条件
*/
@Test
public void testUpdate2(){
User user = new User();
user.setName("国庆快乐").setAge(40).setSex("男");
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","中秋节快乐");
userMapper.update(user,updateWrapper);
System.out.println("更新操作成功!!!!");
}
}
三.前后端交互
3.1框架之间的关系
1.SpringBoot(工具/框架的框架)
2.Spring(整合第三方)
3.SpringMVC()
3.2 完成后端层级代码
说明:MVC思想主要的目的实现代码的解耦,根据MVC思想,演化成层级代码结构 Controller/Service
3.3查询案例
1.业务需求
用户浏览器输入地址: http://localhost:8090/findAll,
要求: 利用MP查询数据库,返回集合的JSON数据.
2 编辑UserController
package com.jt.controller;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
//@Controller
//@ResponseBody //将服务端数据转化为JSON串返回
public class UserController {
//编码规则: 面向接口编程 解耦
@Autowired
private UserService userService;
/**
* 查询所有的数据
* URL: http://localhost:8090/findAll
* 参数: 没有参数
* 返回值: List<User>
*/
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
3.编辑UserServiceImpl
package com.jt.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
//查询所有数据 没有where条件 所以参数为空
@Override
public List<User> findAll() {
QueryWrapper queryWrapper = new QueryWrapper();
return userMapper.selectList(null);
}
}
4. 页面效果展现
四.SpringMVC参数传递
4.1简单参数接受
1.需求分析
根据id查询用户信息:http://localhost:8090/findUserById?id=1
2.编辑UserController
/**
*
* 不同类型的请求注解说明:
* @PostMapping("")
* @PutMapping("")
* @DeleteMapping("")
* 参数说明:
* 1.参数名称必须与URL中的名称一致.
* 2.SpringMVC可以根据用户的需求,自动的实现类型的转化
* 底层实现: springmvc所有的参数默认都是String类型
* 根据用户参数的类型,自动实现转化.
*
* URL地址: http://localhost:8090/findUserById?id=1
* 请求类型: GET/DELETE /POST/PUT
* 参数: id=1
* 返回值结果: User对象的json串
*
*/
//@RequestMapping(value = "/findUserById",method = RequestMethod.GET)
@GetMapping("/findUserById") //只允许接收get类型
public User findUserById(Integer id){
return userService.findUserById(id);
}
3.编辑UserSerrviceImpl
@Override
public User findUserById(Integer id) {
return userMapper.selectById(id);
}
3.页面效果
4.2对象参数接受
案例: 根据name=“王昭君” sex=女 查询用户数据?
1.需求:根据name=“王昭君” sex = “女” 查询用户数据
url:http://localhost:8090/findUserByNS?name=王昭君&sex=女
2.编辑UserController
/**
* 规则: SpringMVC 可以利用对象的方式接收
* 底层实现: 参数name="xxx" 拼接set形成setName,之后检查对象中
* 是否有对应的setName(), 如果匹配该方法,则为对象赋值.
* 注意事项: 参数名称最好以属性名称一致
*
* URL地址: http://localhost:8090/findUserByNS?name=王昭君&sex=女
* 参数: name=xxx&sex=xx
* 返回值:List<User>
*/
@GetMapping("/findUserByNS")
public List<User> findUserByNS(User user){
return userService.findUserByNS(user);
}
3.编辑UserServiceImpl
//MP 可以根据对象中不为null的属性拼接where条件
@Override
public List<User> findUserByNS(User user) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
return userMapper.selectList(queryWrapper);
}
4.页面效果
4.3 restFul参数接收
1.resultFul介绍
ResultFul指的是一组架构约束条件和原则,满足这些约束条件和原则的应用程序或设计就是
ResultFul:ResultFul是一种请求的规则(语法/定义)
2.ResultFul说明:
Get请求:http://localhost:8090/findUserByNS?name=王昭君&sex=女
信息:1.查询请求
2.参数直观
3.请求的结果冗余,不合适多个参数的写法
请求优化:http://localhost:8090/user/王昭君/女
优势:
1. 用户不能了解请求的意图,规定:请求方法名称不能出现动词,只能写名词
2. 参数保密,只有后端服务器清楚参数的意义
3. 请求字节传输量少 简洁
注意事项:
1. URL地址中参数与参数之间使用/分隔
2. 请求的参数的位置一旦固定,不可轻易修改
3. 用户发请求时,就应该按照resultFul的结构执行
4. resultFul请求一般以get请求为主,
3.案例
1.需求
查询name=“貂蝉” age>10岁的用户
URL:http://localhost:8090/user/貂蝉/10
2.编辑UserController
/**
* 后端服务器接收规则:
* 1.参数与参数之后使用 /分隔
* 2.参数的位置一旦确定,一般不变.
* 3.接收的参数使用 {形参变量}
* 4.使用@PathVariable 接收
* 5.如果参数有多个建议使用对象接收 参数必须与属性一致,SpringMVC自动封装
* 注意事项: 如果名称不统一,则需要转化 具体如下:
* @PathVariable("name") String username
*
* url: http://localhost:8090/user/貂蝉/10
* 参数: name/age
* 返回值: List<User>
*/
@GetMapping("/user/{name}/{age}")
public List<User> findUserByNA(User user){
return userService.findUserByNA(user);
}
/*
说明: restFul写法1
@GetMapping("/user/{name}/{age}")
public List<User> findUserByNA(@PathVariable String name,
@PathVariable Integer age){
System.out.println(name);
System.out.println(age);
return null;
}*/
3.编辑UserServiceImpl
//规则: 字段与属性的逻辑运算符为 '=号'时使用实体(user)对象封装
//查询 name="貂蝉" age> 10 岁 的用户.
@Override
public List<User> findUserByNA(User user) {
QueryWrapper<User> queryWrapper = new QueryWrapper();
queryWrapper.eq("name",user.getName())
.gt("age",user.getAge());
return userMapper.selectList(queryWrapper);
}
4.4同名提交问题
1.需求
用户查询id=1,3,4,5的数据. 如果有同名参数一般采用 ',号 ’ 分隔
URL: http://localhost:8090/getUserByIds?ids=1,3,4,5
2.编辑UserController
/**
* 规则: 如果参数使用,号分隔,则SpringMVC可以自动的转化为数组.
* 查询多个用户
* URL: http://localhost:8090/getUserByIds?ids=1,3,4,5
* 参数: ids = 1,3,4,5
* 返回值: List<User>
*/
@GetMapping("/getUserByIds")
public List<User> getUserByIds(Integer[] ids){
return userService.getUserByIds(ids);
}
3 编辑UserServiceImpl
@Override
public List<User> getUserByIds(Integer[] ids) {
List<Integer> idList = Arrays.asList(ids);
return userMapper.selectBatchIds(idList);
}