diff --git "a/56 \350\265\265\346\225\217/20231219 SSM\346\241\206\346\236\266.md" "b/56 \350\265\265\346\225\217/20231219 SSM\346\241\206\346\236\266.md" new file mode 100644 index 0000000000000000000000000000000000000000..4935d81fd09d62ed2d7e628f22189d12c42e9075 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20231219 SSM\346\241\206\346\236\266.md" @@ -0,0 +1,99 @@ +## 笔记 + +### Maven + +~~~ md +Maven是专门用于管理和构建Java项目的工具,它的主要功能有: + +提供了一套标准化的项目结构 + +提供了一套标准化的构建流程(编译,测试,打包,发布……) + +提供了一套依赖管理机制 + + +仓库分类: +本地仓库:自己计算机上的一个目录 + +中央仓库:由Maven团队维护的全球唯一的仓库 + +地址: https://repo1.maven.org/maven2/ + +远程仓库(私服):一般由公司团队搭建的私有仓库,国内镜像也是远程仓库的一种 +当项目中使用坐标引入对应依赖jar包后,首先会查找本地仓库中是否有对应的jar +包: +如果有,则在项目直接引用; + +如果没有,则去中央仓库中下载对应的jar包到本地仓库。 + + +Maven 坐标详解 + +什么是坐标? +Maven 中的坐标是==资源的唯一标识== +使用坐标来定义项目或引入项目中需要的依赖 + +Maven 坐标主要组成 +groupId:定义当前Maven项目隶属组织名称(通常是域名反写,例如: +com.mdd) +artifactId:定义当前Maven项目名称(通常是模块名称,例如 order-service、 +goods-service) +version:定义当前项目版本号 + + +使用坐标引入jar包 +使用坐标引入jar包的步骤: + +在项目的 pom.xml 中编写 标签 +在 标签中 使用 引入坐标 +定义坐标的 groupId,artifactId,version +点击刷新按钮,使坐标生效 + + +依赖范围 +通过设置坐标的依赖范围(scope),可以设置 对应jar包的作用范围:编译环境、测试 +环境、运行环境。 +如下图所示给 junit 依赖通过 scope 标签指定依赖的作用范围。 那么这个依赖就 +只能作用在测试环境,其他环境下不能使用。 + +compile :作用于编译环境、测试环境、运行环境。 +test : 作用于测试环境。典型的就是Junit坐标,以后使用Junit时,都会将scope +指定为该值 +provided :作用于编译环境、测试环境。我们后面会学习 servlet-api ,在使 +用它时,必须将 scope 设置为该值,不然运行时就会报错 +runtime : 作用于测试环境、运行环境。jdbc驱动一般将 scope 设置为该值, +当然不设置也没有任何问题 +注意: +如果引入坐标不指定 scope 标签时,默认就是 compile 值。以后大部分 +jar包都是使用默认值。 +~~~ + +## 作业 + +~~~ java + + + junit + junit + 4.13.2 + test + + +~~~ + +~~~ java +import org.junit.Test; + +public class APP { + @Test + public void test01(){ + System.out.println(999); + } + @Test + public void test02(){ + System.out.println("吃饭了吗"); + } +} + +~~~ + diff --git "a/56 \350\265\265\346\225\217/20231225 mybatis\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/56 \350\265\265\346\225\217/20231225 mybatis\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..075c8538edde7ff2c8a0ae5b467803493949e107 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20231225 mybatis\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,342 @@ +## 作业 + +~~~ java +package com.dsg; + +import com.dsg.mapper.BrandMapper; +import com.dsg.pojo.Brand; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MybatistTest { + // 查询所有 + @Test + public void test01() throws IOException { +// 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + + SqlSession sqlSession = sqlSessionFactory.openSession(); + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + List brands = mapper.selectAll(); + System.out.println(brands); + sqlSession.close(); + } + // 通过id进行查询 + @Test + public void findById() throws IOException { + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(); + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + Brand brand = mapper.selectById(1); + System.out.println(brand); + sqlSession.close(); + } + // 多条件查询,散装参数 + @Test + public void findByCondition() throws IOException { + // 定义参数 + int status = 1; + String companyName = "华为"; + String brandName = "华为"; + + companyName = "%" + companyName + "%"; + brandName = "%" + brandName + "%"; + + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + List brands = mapper.selectByCondition(status, companyName, brandName); + System.out.println(brands); + + sqlSession.close(); + } + // 多条件查询,用类做参数 + @Test + public void findByCondition01() throws IOException { + // 定义参数 + int status = 1; + String companyName = "华为"; + String brandName = "华为"; + + companyName = "%" + companyName + "%"; + brandName = "%" + brandName + "%"; + + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + brand.setStatus(status); + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + List brands = mapper.selectByCondition(brand); + System.out.println(brands); + + sqlSession.close(); + } + // 多条件查询,用map方法做参数 + @Test + public void findByCondition02() throws IOException { + // 定义参数 + int status = 1; + String companyName = "华为"; + String brandName = "华为"; + + companyName = "%" + companyName + "%"; + brandName = "%" + brandName + "%"; + +// Brand brand = new Brand(); +// brand.setBrandName(brandName); +// brand.setCompanyName(companyName); +// brand.setStatus(status); + + Map map = new HashMap(); + map.put("status",status); + map.put("companyName",companyName); + map.put("brandName",brandName); + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + List brands = mapper.selectByCondition(map); + System.out.println(brands); + + sqlSession.close(); + } + // 添加 + @Test + public void insert() throws IOException { + // 定义参数 + int status = 1; + String companyName = "抖营"; + String brandName = "抖营"; + String description = "多看大美女,有益身心健康"; + int ordered = 100; + + + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + brand.setStatus(status); + brand.setDescription(description); + brand.setOrdered(ordered); + + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(true); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + mapper.insert(brand); + // 手动提交事务 +// sqlSession.commit(); + System.out.println(brand.getId()); + // 释放资源 + sqlSession.close(); + } + // 修改 + @Test + public void upData() throws IOException { + // 定义参数 + int status = 1; + String companyName = "火山短视频"; + String brandName = "火山短视频"; + String description = "多看大帅哥,有益身心健康"; + int ordered = 50; + int id = 6; + + + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + brand.setStatus(status); + brand.setDescription(description); + brand.setOrdered(ordered); + brand.setId(id); + + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(true); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + mapper.upData(brand); + // 手动提交事务 +// sqlSession.commit(); + System.out.println(brand.getId()); + // 释放资源 + sqlSession.close(); + } + // 删除 + @Test + public void delete() throws IOException { + int[] ids = {6}; + + // 1 加载mybatis的核心配置文件 + String resource = "mybatis-confing.xml"; + InputStream inputStream = Resources.getResourceAsStream(resource); + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + System.out.println(111); + + SqlSession sqlSession = sqlSessionFactory.openSession(true); + // 获取代理 + BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); + // 执行sql + mapper.delete(ids); + // 手动提交事务 +// sqlSession.commit(); + System.out.println(ids); + // 释放资源 + sqlSession.close(); + } +} + +~~~ + +~~~ java +package com.dsg.mapper; + +import com.dsg.pojo.Brand; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +public interface BrandMapper { + List selectAll(); + + Brand selectById(int id); + + // 散装参数 + List selectByCondition( + @Param("status") int status, + @Param("companyName") String companyName, + @Param("brandName") String brandName + ); + + // 用brand对象做参数 + List selectByCondition(Brand brand); + + // 使用map方法做参数 + List selectByCondition(Map map); + + // 添加 + void insert(Brand brand); + + // 修改 + void upData(Brand brand); + + // 删除 + void delete(int[] ids); +} + +~~~ + +~~~ java + + + + + + + + + + + + + + + + + insert into tb_brand (brand_name,company_name,ordered,description,status) + values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status}); + + + + update tb_brand + set + status = #{status}, + brand_name = #{brandName}, + company_name = #{companyName}, + ordered = #{ordered}, + description = #{description} + where id = #{id}; + + + + delete + from tb_brand + where id in ( + + #{id} + + + ); + + +~~~ +