复习
主键唯一 从键不一定唯一
感觉还是有欠缺
到公司或者项目 问物理模型图 或者 数据库字典
项目发布&项目打包
clean 清除jar包或者war包
install
本地仓库
导包出来
先install
再在对应的下图 找最新的jar包
总结
疑问 本地仓库相关
如上图 被别人调用 作为依赖 (在target不能被调用)
项目发布
和控制台运行的信息一样
项目发布成功
报错
自我实现
新建一个
install 导包
准备发布
cmd控制台
或者
工具里面点运行一样
yml配置问题
疑问不应该两个//?但是一个或者两个斜杠都可以
试试去掉RequestMapping注解url上的/
首要问题 端口号占用
如上图 和sts不同 还是能直接运行 提问 问江哥
去掉斜杠也可 idea中 一个斜杠和两个都可
自己该配置 去掉yml里面的斜杠好了 免得出错
很奇怪的问题
总结 注释掉yml的context-path 只留@RequestMapping("/getMsg1")的斜杠
继续
中午查上午 10.03
idea热部署
先加依赖
尝试热部署
不行
配上设置 和热部署依赖相对应
ctrl+shift+alt+/
热部署完成 好慢…大概15s+了
修改后保存即可(或者自动保存) 完成
继续 快捷键补全
环境切换
业务场景
分成上下两部分
yml配置文件
# 测试环境 /jt 端口号 8095 生产环境: / 端口号8090 配置必须完整.
#设定默认的环境
spring:
profiles:
active: test #活动的&默认的环境 第一时间显示的
---
# yml 1.key-value结构
# 2.key-value之间使用": "方法链接
# 3.YML配置文件有缩进的效果.
# 4.YML配置文件默认采用utf-8编码.
#定义环境名称
spring:
profiles: prod
server:
servlet:
context-path: / #设定项目发布路径
port: 8090
#配置图片上传路径
#image:
#localDir: D:/JT-SOFT/images
#设定分割线
---
# 设定环境名称
spring:
profiles: test
server:
servlet:
context-path: /jt #设定项目发布路径
port: 8095
自我实现
注解url加斜杠
对应上图的yml 生产和发布分开开来了 两个不同的对应
配置lombok
第一步 引入jar包
还有无参构造 有参构造
继续(还没写完)
晚上对应上午 11.25 提了一口端口号
spring整合mybatis
新建一个batis
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.32</version>
</dependency>
<!--springBoot数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis 暂时 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
重写启动类
pojo
动态生成get set 方法和构造方法 alt+insert
自我实现
继续新增
整合后测试
江哥
步骤
1.创建项目
2.导入项目包
<?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>org.example</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--parent标签作用: 定义了SpringBoot中所有关联项目的版本号信息.-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<!--项目打包时,跳过测试类打包-->
<skipTests>true</skipTests>
</properties>
<!--开箱即用:SpringBoot项目只需要引入少量的jar包及配置,即可拥有其功能.
spring-boot-starter 拥有开箱即用的能力.
maven项目中依赖具有传递性.
A 依赖 B 依赖 C项目 导入A bc会自动依赖
-->
<dependencies>
<!--直接依赖web springMVC配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<!--springBoot-start SpringBoot启动项 -->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot重构了测试方式 可以在测试类中 直接引入依赖对象-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--支持热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--引入插件lombok 自动的set/get/构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis 暂时 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<!--在项目打包部署时生效,如果不添加build,则程序发布时不然会报
项目中没有main方法.
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3 .编辑项目结构代码
4 .编辑YML配置文件
server:
port: 8090
servlet:
context-path: /
spring:
datasource:
#driver-class-name: com.mysql.jdbc.Driver 驱动注释,采用默认的方式
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#Mybatis整合
mybatis:
#定义别名包
type-aliases-package: com.jt.pojo
#添加xml文件的依赖
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
测试类
package com.jt.test;
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 java.util.List;
@SpringBootTest
public class TestMybatis {
@Autowired
private UserMapper userMapper;
@Test
public void test01(){
List<User> userList = userMapper.findAll();
System.out.println(userList);
}
}
报错
修正
代替了dao层的@Mapper注解
注解如上图的 反而报错
改回去 如下图
疑问
下午
关于MyBatis配置类的问题
resultType写法 太啰嗦了
自己加了包 但是可以自己指定路径
别名包定义
<!--当查询结果返回值会自己拼接包路径 com.jt.pojo.User
虽然使用了别名包,但是也可以自己指定包路径
结论:自己不写执行别名包
自己写了就执行自己的.
-->
<!--<select id="findAll" resultType="com.jd.pojo.User">
select * from user
</select>-->
**
驼峰命名规则 (yml中)
**
ORM的概念
对象关系映射(Object Relational Mapping,简称ORM)是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据库中。本质上就是将数据从一种形式转换到另外一种形式。 这也同时暗示着额外的执行开销;然而,如果ORM作为一种中间件实现,则会有很多机会做优化,而这些在手写的持久层并不存在。 更重要的是用于控制转换的元数据需要提供和管理;但是同样,这些花费要比维护手写的方案要少;而且就算是遵守ODMG规范的对象数据库依然需要类级别的元数据。
概括: 利用对象的方式操作数据库.
mybatis核心思想
补上orm和xml的驼峰规则代码
问题 resultType才开启驼峰规则 resultMap不需要开 自己去总结
<?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.jt.mapper.UserMapper">
<!--当查询结果返回值会自己拼接包路径 com.jt.pojo.User
虽然使用了别名包,但是也可以自己指定包路径
结论:自己不写 执行别名包
自己写了就执行自己的.
-->
<!--<select id="findAll" resultType="com.jd.pojo.User">
select * from user
</select>-->
<!--需求
字段信息: user_id,user_name,user_age
对象属性: userId,userName,userAge
需求:查询数据库,实现数据的映射.
补充知识: ORM:以对象的形式操作数据库.
resultType:将数据库中的结果动态的封装为对象.
原则: 字段的名称必须与对象属性的名称一致.
驼峰规则映射:
目的: 主要简化mybatis 映射的过程
规则: user_id(字段) ~~~ 去除下划线 ~~~之后首字母大写
userid userId
~~~之后与属性的名称一致,则可以正确的映射.
注意事项:一旦使用驼峰规则,必须满足条件
面试点: 如果开启了驼峰规则映射,
问: 字段user_id 与对象属性user_id
能否映射???
-->
<!--<select id="xxx" resultType="User">
<!–执行的是sql,返回的是List<User>–>
select * from user
</select>-->
</mapper>
MyBatisPlus
业务需求
mybatisplus介绍
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis工作原理说明(基于orm 以对象的方式操作数据库)
curd 它代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)操作。 CURD 定义了用于处理数据的基本原子操作。
Mapper表 .xml文件
属性与字段 resultMap resultType
公共的Mapper接口 定义公共的curd操作 利用泛型区分对象
接上上图
表名和对象对应 字段和属性对应
sql:insert into 表名(字段…) values(属性值…);由mp动态拼接之后交由mybatis执行
由mp(mybatis plus)动态拼接之后交由mybatis执行
## Springboot整合Mp
导入mp 注释掉m
pojo
如果如果名字一直(忽略大小写) 可以忽略表名
对象和表的映射
对象和表的映射
test测试
自我实现
接入mybatisplus
注释掉mybatis依赖 加入mybatisplus依赖
pojo 1.对象与数据库表如何映射?
利用注解实现对象与表绑定 属性与字段绑定.
Mapper接口
BaseMapper泛型没完全懂 晚上(基本over 回看)
2.如何解决众多接口编辑雷同的CRUD操作?
定义一个公共的Mapper接口,定义公共的CRUD操作 利用泛型区分对象.
mapper.xml没东西
yml配置文件
3.数据库只对Sql感兴趣,对对象没兴趣?(如何将对象转化为sql)
思想:按照特定的语法,将对象转化为sql语句.
例子: 将user对象插入数据库中.
userMapper.insert(user对象); //程序员完成的最后的代码
sql: insert into 表名(字段.....) values (属性值......); 由mp动态拼接之后交由mybatis执行.
实现结果
扩展点 泛型基础
泛型起约束作用
为什么要加泛型 标明是user类型的
如上 为什么要加泛型
解释 逆向去考虑
不能确定用户将来传什么 就用泛型 (用object行吗? 行 需要向上造型)
因为不知道转什么的时候 就用泛型 传用户就转用户 传数据就转数据(具体的实现 框架帮忙分析转换)
**BaseMapper**被定义后的泛型
补充知识 关于数据库的补充地址说明
MP API的介绍
insert操作
如下图 直接免去mapper xml页面的一对一操作
开放sql日志
用yml配置文件
自我实现
先配置yml文件
BaseMapper泛型 里面包含了CURD的操作
基本告别单表sql
结果
查询id为20
id代表主键 byId查的就是那个主键
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
注意: entity 生成的 where 条件与 使用各个 api 生成的 where 条件没有任何关联行为
自我实现
需求
需求 order by
需求
深入了解集合和数组的区别 晚上
数组和集合的区别
https://blog.csdn.net/hellojoy/article/details/82382671
自我试下:
条件构造器 where语句
AbstractWrapper
说明:
QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
用于生成 sql 的 where 条件, entity 属性也用于生成 sql 的 where 条件
注意: entity 生成的 where 条件与 使用各个 api 生成的 where 条件没有任何关联行为
单词分析
Query 查询
Update 更新
Wrapper 封装器
我们今天用的是QueryWrapper 主要用于查询 今天用的BaseMapper调用的是selectList,selectCount
自我实现:
模糊查询代替% like likeRight likeLeft
//模糊查询
/**
* 需求: 1.查询名称中包含'精'字的男性 "%精%"
* 2.查询以精结尾的 %精
* 3.查询以精开头 精%
* * */
@Test
public void select04(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//Left或者Right指的是%的放置位置
queryWrapper.like("name","精")//查询名称中包含'精'字的男性 "%精%"
// queryWrapper.likeLeft("name","精")//查询以精结尾的 %精
// queryWrapper.likeRight("name","精")//查询以精开头 精%
.eq("sex","男");
List<User> userList = userMapper.selectList(queryWrapper);//集合对集合
System.out.println(userList);
}
顺序排列
按age降序顺序排列 orderByDesc age 20-----18
/**
* 需求: 查询sex=女,之后按照age倒叙排列.如果年龄相同按照id降序排列.
* */
@Test
public void select05(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex","女")
.orderByDesc("age","id");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
按age升序顺序排列 orderByAsc age 16—17–18
/**
* 需求: 查询sex=女,之后按照age倒叙排列.如果年龄相同按照id降序排列.
* */
@Test
public void select05(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sex","女")
.orderByAsc("age","id");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
批量查询
方法一 基础的封装成Interger类型集合查询
/**
* 需求: 查询id为 1,3,5,7,8,9的数据???
* 关键字 in or
* 在关联查询中慎用
* */
//第一种方式 批量操作 不需要强转
@Test
public void select06(){
List<Integer> idList = new ArrayList<>();//数组封装成Interger类型集合
idList.add(1);
idList.add(3);
idList.add(5);
idList.add(7);
idList.add(8);
idList.add(9);
List<User> userList = userMapper.selectBatchIds(idList);//查询(根据ID 批量查询)
System.out.println(userList);
}
第二种方法 数组查询
/**
* 需求: 查询id为 1,3,5,7,8,9的数据???
* 关键字 in or
* 在关联查询中慎用
* */
//第二种方式 数组形式去封装 需要强转
@Test
public void select07(){
Integer[] ids ={1,3,5,7,8,9};
List<Integer> idList = Arrays.asList(ids);//ids强转成idList 数组转集合 下面才能集合转集合相对应
List<User> userList = userMapper.selectBatchIds(idList);//查询(根据ID 批量查询)
System.out.println(userList);
}
回归晚上的