From 8adbdd8276f40661b67f24a20af5aaac08186954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Thu, 28 Dec 2023 00:02:00 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20231226\347\254\224\350\256\260.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20231226\347\254\224\350\256\260.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20231226\347\254\224\350\256\260.md" "b/18 \345\276\220\346\260\270\346\267\263/20231226\347\254\224\350\256\260.md" new file mode 100644 index 0000000..36f0c7c --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20231226\347\254\224\350\256\260.md" @@ -0,0 +1,33 @@ +## 笔记 + +### Mybatis 概念 + +- MyBatis 是一款优秀的==持久层框架==,用于简化 JDBC 开发 + +**持久层:** + +- 负责将数据到保存到数据库的那一层代码。以后开发我们会将操作数据库的Java代码作为持久层。而Mybatis就是对jdbc代码进行了封装。 + +- JavaEE三层架构:表现层、业务层、持久层 + +**框架:** + +- 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型 + +- 在框架的基础之上构建软件编写更加高效、规范、通用、可扩展 + +### **JDBC** 缺点 + +- 硬编码,耦合度高(注册驱动、获取连接、SQL语句,不方便后期维护) + +- 操作繁琐(手动设置参数,手动封装结果集,没有什么技术含量,而且特别耗费时间的) + +### Mybatis 优化 + +- 硬编码可以配置到==配置文件== + +- 操作繁琐的地方mybatis都==自动完成== + +### tips: + +1. 核心配置文件:mybatis-config.xml \ No newline at end of file -- Gitee From a0ff08f3cbc2d7a452c81bc1142431a0275ce4f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Fri, 29 Dec 2023 00:19:33 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20231227\347\254\224\350\256\260.md" | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20231227\347\254\224\350\256\260.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20231227\347\254\224\350\256\260.md" "b/18 \345\276\220\346\260\270\346\267\263/20231227\347\254\224\350\256\260.md" new file mode 100644 index 0000000..9ca7fce --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20231227\347\254\224\350\256\260.md" @@ -0,0 +1,131 @@ + +Ioc和DI +IoC(Inversion of Control)控制反转 +使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转 +Spring技术对IoC思想进行了实现 +Spring提供了一个容器,称为IoC容器,用来充当IoC思想中的“外部” +IoC容器负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean +DI(Dependency Injection)依赖注入 +在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入 +bean生命周期: + +初始化容器 +创建对象(内存分配) +执行构造方法 +执行属性注入(set操作) +执行bean初始化方法 +使用bean +执行业务操作 +关闭/销毁容器 +执行bean销毁方法 +bean销毁时机 + +容器关闭前触发bean的销毁 +关闭容器方式: +手工关闭容器 +ConfigurableApplicationContext接口close()操作 +注册关闭钩子,在虚拟机退出前先关闭容器再退出虚拟机 + ConfigurableApplicationContext接口registerShutdownHook()操作 + +public class AppForLifeCycle { public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ctx.close(); }} +pom.xml + + + + + 4.0.0 + + com.mdd + spring_01_quickstart + 1.0-SNAPSHOT + + + + junit + junit + 4.13.2 + test + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + + +src/main/resources/ApplicationContext.xml +ApplicationContext.xml + + + + + + + +文件名:BookDaoImpl.java + +package com.mdd.dao.impl; + +import com.mdd.dao.BookDao; + +public class BookDaoImpl implements BookDao { + public void save() { + System.out.println("book dao save ..."); + } +} +文件名: BookServiceImpl.java + +package com.mdd.service.impl; +import com.mdd.dao.BookDao; +import com.mdd.dao.impl.BookDaoImpl; +import com.mdd.service.BookService; + +public class BookServiceImpl implements BookService { + + private BookDao bookDao; + + public void setBookDao(BookDao bookDao) { + this.bookDao = bookDao; + } + + public void save() { + System.out.println("book service save ..."); + bookDao.save(); + } + +} +文件名:BookDao.java +package com.mdd.dao; +public interface BookDao { + public void save(); +} +文件名:App.java +package com.mdd; +import com.mdd.service.BookService; +import com.mdd.service.impl.BookServiceImpl; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(String[] args) { +// BookService bookService = new BookServiceImpl(); +// bookService.save(); + //1.通过配置文件获取IOC容器 + ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml"); + //2.通过IOC容器的getBean 方法,获取对象 + BookServiceImpl bookService = (BookServiceImpl) ctx.getBean("bookService"); + bookService.save(); + + } +} +文件名:BookService.java +package com.mdd.service; + +public interface BookService { + public void save(); +} \ No newline at end of file -- Gitee From 4d6bdf2bbbab9b5b1b8805e6b904c5207fa6df90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Fri, 29 Dec 2023 00:21:17 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20231228\347\254\224\350\256\260.md" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20231228\347\254\224\350\256\260.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20231228\347\254\224\350\256\260.md" "b/18 \345\276\220\346\260\270\346\267\263/20231228\347\254\224\350\256\260.md" new file mode 100644 index 0000000..b6caf78 --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20231228\347\254\224\350\256\260.md" @@ -0,0 +1,53 @@ +创建容器 + +加载多个配置文件 + +```java +ApplicationContext ctx = new ClassPathXmlApplicationContext("bean1.xml", "bean2.xml"); +``` + + + +Spring提供@Component注解的三个衍生注解 + +@Controller:用于表现层bean定义 + +@Service:用于业务层bean定义 + +@Repository:用于数据层bean定义 + +```java +@Repository("bookDao")public class BookDaoImpl implements BookDao {} +@Servicepublic class BookServiceImpl implements BookService {} + +``` + +Spring3.0开启了纯注解开发模式,使用Java类替代配置文件,开启了Spring快速开发赛道Java类代替Spring核心配置文件, + +@Configuration注解用于设定当前类为配置类@ComponentScan注解用于设定扫描路径,此注解只能添加一次,多个数据请用数组格式 + +```java +@ComponentScan({com.mdd.service","com.mdd.dao"}) + +``` + +```java +//加载配置文件初始化容器 +ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); +//加载配置类初始化容器 +ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + +``` + +依赖注入 + +使用@Autowired注解开启自动装配模式(按类型) + +```java +@Servicepublic class BookServiceImpl implements BookService { @Autowired private BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } public void save() { System.out.println("book service save ..."); bookDao.save(); }} + +``` + +注意:自动装配基于反射设计创建对象并暴力反射对应属性为私有属性初始化数据,因此无需提供setter方法 + +注意:自动装配建议使用无参构造方法创建对象(默认),如果不提供对应构造方法,请提供唯一的构造方法 \ No newline at end of file -- Gitee From 3615406a00c6657eb4ea15ac5f34358fb453dad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Fri, 29 Dec 2023 00:26:23 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20231228\344\275\234\344\270\232.md" | 413 ++++++++++++++++++ 1 file changed, 413 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20231228\344\275\234\344\270\232.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20231228\344\275\234\344\270\232.md" "b/18 \345\276\220\346\260\270\346\267\263/20231228\344\275\234\344\270\232.md" new file mode 100644 index 0000000..47af3bd --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20231228\344\275\234\344\270\232.md" @@ -0,0 +1,413 @@ +完整版 + +除了以下这些还需要自己配置,mysql也要弄一下数据库 + +pom.xml + +```xml + + + 4.0.0 + + com.mdd + ssm06 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + org.springframework + spring-jdbc + 5.2.25.RELEASE + + + org.springframework + spring-test + 5.2.25.RELEASE + + + + org.mybatis + mybatis + 3.5.13 + + + + org.mybatis + mybatis-spring + 2.0.5 + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + junit + junit + 4.13.2 + test + + + + com.alibaba + druid + 1.1.23 + + + + + +``` + + + +com/mdd/domain/Brand.java + +```java +package com.mdd.domain; + +public class Brand { + private Integer id; + private String brandName; + private String companyName; + private Integer ordered; + private String description; + private Integer status; + + public Brand() { + } + + public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) { + this.id = id; + this.brandName = brandName; + this.companyName = companyName; + this.ordered = ordered; + this.description = description; + this.status = status; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBrandName() { + return brandName; + } + + public void setBrandName(String brandName) { + this.brandName = brandName; + } + + public String getCompanyName() { + return companyName; + } + + public void setCompanyName(String companyName) { + this.companyName = companyName; + } + + public Integer getOrdered() { + return ordered; + } + + public void setOrdered(Integer ordered) { + this.ordered = ordered; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + @Override + public String toString() { + return "Brand{" + + "id=" + id + + ", brandName='" + brandName + '\'' + + ", companyName='" + companyName + '\'' + + ", ordered=" + ordered + + ", description='" + description + '\'' + + ", status=" + status + + '}'; + } +} +``` + +com/mdd/config/SpringConfig.java + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +//这个文件的三个格式必须写 +@Configuration +@Import({JdbcConfig.class,MybatisConfig.class}) +@ComponentScan("com.mdd") +public class SpringConfig { + +} +``` + +com/mdd/config/JdbcConfig.java + +```java +package com.mdd.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + //jdbc负责连接mysql,配置驱动,URL,用户名,密码 + //spring如何加载第三方的Bean,通过写一个方法,返回这个bean的类名,并在这个方法上面写一个注解@Bean + @Bean + public DataSource dataSource(){ + //此时,这里要用DruidDataSource实现类 + DruidDataSource ds = new DruidDataSource(); + //配置连接四要素:驱动,url,用户名,密码 + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///mybatis?useSSL=false&allowPublicKeyRetrieval=true"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} + +``` + +com/mdd/config/MybatisConfig.java + +```java +package com.mdd.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.mapper.MapperScannerConfigurer; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class MybatisConfig { + //这里做两件事: +// 1.获取SqlSessionFactoryBean +// 2.获取Mapper代理的包 + @Bean + // 1.获取SqlSessionFactoryBean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + +// ssfb也做两件事: +// 1.设置实体类的别名 +// 2.设置数据源 + + ssfb.setTypeAliasesPackage("com.mdd.domain");//实体类所在的包的路径 + + ssfb.setDataSource(dataSource); + + return ssfb; + + } + + //获取Mapper代理的包 + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + + msc.setBasePackage("com.mdd.mapper");//代理类的包的路径 + + return msc; + + } + + + + + + +} + +``` + +com/mdd/mapper/BrandMapper.java(接口) + +```java +package com.mdd.mapper; + +import com.mdd.domain.Brand; +import org.apache.ibatis.annotations.Select; +import java.util.List; + +public interface BrandMapper { + //用来写增加删除修改的各种方法 + //1.查询所有的Brand + //当遇到null的情况时,可以将不符合java命名方式的列名取一个别名 + @Select("select id, brand_name as brandName, company_name as companyName, ordered, description, status from tb_brand") + List findAll(); + + //遇到复杂的sql怎么办,也就是如何处理动态sql,要结合xml映射文件来处理 + List selectByCondition(Brand brand); +// 简单的注解 +// @Select() 查询 +// @Delete() 删除 +// @Update() 修改 +// @Insert() 增加 + +} + +``` + +src/main/java/App.java + +```java +import com.mdd.config.SpringConfig; +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import java.util.List; + +public class App { + public static void main(String[] args) { + //1.通过Spring核心配置文件,获取IOC容器 + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + //2.获取代理接口的对象 + BrandMapper brandMapper = ctx.getBean(BrandMapper.class); + //3.调用代理对象,对应的sql方法 + List all = brandMapper.findAll(); + System.out.println(all); + } +} + +``` + +com/mdd/mapper/BrandMapper.xml + +```xml + + + + + + + + + +``` + +src/test/java/com/mdd/test/BrandTest.java + +```java +package com.mdd.test; + +import com.mdd.config.SpringConfig; +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import com.mdd.service.BrandService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class)//加载spring为菌体专门提供的运行类 +@ContextConfiguration(classes = SpringConfig.class)//加载Spring核心配置,你可以理解成就是IDC容器 +public class BrandTest { + + //通过自动装配的形式,直接将要bean注入进来 + @Autowired + private BrandMapper brandMapper;//这里报错很正常,不用管 + + @Autowired + private BrandService brandService;//要什么就自动装配什么 + + @Test + public void testo1(){ + + //作用和APP的一样, +// List all = brandMapper.findAll(); +// System.out.println(all); + +// 1.模拟浏览器向服务器发送了查询的数据 + String brandName = "华为"; + String companyName = "华为"; + +// 1.5因为要模糊查询,所以要对原始数据做加%处理 + brandName = "%"+brandName+"%"; + companyName = "%"+companyName+"%"; + + //2.将查询数据,封装成一个brand对象 + Brand brand = new Brand(); +// Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + + //3.调用代理对象,数据区这个brand + List brands = brandMapper.selectByCondition(brand); + System.out.println(brands); + + } + + //模拟表现层调用业务层 + @Test + public void testService(){ + List brands = brandService.selectAll(); + System.out.println(brands); + + } + +} +``` \ No newline at end of file -- Gitee