From a51803f3d58008edf3b176c1a299a70c462b3075 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com>
Date: Thu, 4 Jan 2024 13:25:55 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../20240102 \347\254\224\350\256\260.md" | 0
.../20240104 \347\254\224\350\256\260.md" | 325 ++++++++++++++++++
2 files changed, 325 insertions(+)
rename "03 \350\265\226\345\277\203\345\246\215/2024012 \347\254\224\350\256\260.md" => "03 \350\265\226\345\277\203\345\246\215/20240102 \347\254\224\350\256\260.md" (100%)
create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md"
diff --git "a/03 \350\265\226\345\277\203\345\246\215/2024012 \347\254\224\350\256\260.md" "b/03 \350\265\226\345\277\203\345\246\215/20240102 \347\254\224\350\256\260.md"
similarity index 100%
rename from "03 \350\265\226\345\277\203\345\246\215/2024012 \347\254\224\350\256\260.md"
rename to "03 \350\265\226\345\277\203\345\246\215/20240102 \347\254\224\350\256\260.md"
diff --git "a/03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md" "b/03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md"
new file mode 100644
index 0000000..7c4d767
--- /dev/null
+++ "b/03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md"
@@ -0,0 +1,325 @@
+## 笔记
+
+REST简介
+
+REST(Representational State Transfer)
+
+优点:
+
+隐藏资源的访问行为,无法通过地址得知对资源是何种操作
+
+书写简化
+
+名称:@RequestMapping
+
+类型:方法注解
+
+位置:SpringMVC控制器方法定义上方
+
+作用:设置当前控制器方法请求访问路径
+
+属性
+
+value(默认):请求访问路径
+
+method:http请求动作,标准动作(GET/POST/PUT/DELETE)
+
+名称:@PathVariable
+
+类型:形参注解
+
+位置:SpringMVC控制器方法形参定义前面
+
+作用:绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应
+
+@RequestBody @RequestParam @PathVariable
+
+区别
+
+@RequestParam用于接收url地址传参或表单传参
+
+@RequestBody用于接收json数据
+
+@PathVariable用于接收路径参数,使用{参数名称}描述路径参数
+
+应用
+
+后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
+
+如果发送非json格式数据,选用@RequestParam接收请求参数采用RESTful进行开发,当参数数量较少时,例如1个,
+
+可以采用@PathVariable接收请求路径变量,通常用于传递id值
+
+## 作业
+
+pom.xml
+
+```xml
+
+ 4.0.0
+ com.xlu
+ ssmvc03
+ war
+ 1.0-SNAPSHOT
+
+
+
+ org.springframework
+ spring-webmvc
+ 5.2.25.RELEASE
+
+
+ javax.servlet
+ javax.servlet-api
+ 3.1.0
+
+ provided
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.11.3
+
+
+
+
+
+
+
+ org.apache.tomcat.maven
+ tomcat7-maven-plugin
+ 2.2
+
+ 80
+ /
+
+ utf-8
+
+
+
+
+
+
+
+```
+
+src/main/java/com/xlu/controller/UserController.java
+
+```java
+package com.xlu.controller;
+
+import com.xlu.admin.User;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.Arrays;
+import java.util.List;
+
+@Controller
+public class UserController {
+
+ //接受用户名和慢慢,登录
+ @RequestMapping("/login")
+ @ResponseBody
+ public String login(User user){
+
+ String result = "login error";
+ if ("admin".equals(user.getUsername())&&"123456".equals(user.getPassword())) {
+ result = "login success";
+ }
+ return result;//输入用户名admin,密码123456,显示结果巍峨login success
+ }
+
+ //模拟注册
+ @RequestMapping(value = "/reg",produces = "text/html;charset=utf-8")//解决返回的中文乱码问题
+ @ResponseBody
+ public String reg(User user){
+
+ System.out.println(user);
+
+ return "注册成功!";
+ }
+
+ //json集合
+ @RequestMapping("/jsonArray")
+ @ResponseBody
+ public String jsonData(@RequestBody List names){
+
+ System.out.println(names);
+
+ return "ok";
+ }
+
+// JSON数组
+ @RequestMapping("/jsonArray2")
+ @ResponseBody
+ public String jsonData2(@RequestBody String[] names){
+
+ System.out.println(Arrays.toString(names));
+
+ return "ok";
+ }
+
+// json对象
+ @RequestMapping("/userJson")
+ @ResponseBody
+ public String jsonUser(@RequestBody User user){
+ System.out.println(user);
+ return user.toString();
+ }
+
+ @RequestMapping("/userJson2")
+ @ResponseBody
+ public String jsonUser2(@RequestBody List user){
+ System.out.println(user);
+ return user.toString();
+ }
+
+
+}
+
+```
+
+src/main/java/com/xlu/config/WebConfig.java
+
+```java
+package com.xlu.config;
+
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
+ protected Class>[] getRootConfigClasses() {
+ return new Class[0];
+ }
+
+ protected Class>[] getServletConfigClasses() {
+ return new Class[]{SpringMvcConfig.class};
+ }
+
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+}
+
+```
+
+src/main/java/com/xlu/config/SpringMvcConfig.java
+
+```java
+package com.xlu.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+
+@Configuration
+@ComponentScan("com.xlu.controller")
+@EnableWebMvc
+public class SpringMvcConfig {
+}
+
+```
+
+src/main/java/com/xlu/admin/User.java
+
+```java
+package com.xlu.admin;
+
+public class User {
+
+ private Integer id;
+ private String username;
+ private String password;
+ private Address address;
+
+ @Override
+ public String toString() {
+ return "User{" +
+ "id=" + id +
+ ", username='" + username + '\'' +
+ ", password='" + password + '\'' +
+ ", address=" + address +
+ '}';
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public Address getAddress() {
+ return address;
+ }
+
+ public void setAddress(Address address) {
+ this.address = address;
+ }
+
+ public User(Integer id, String username, String password, Address address) {
+ this.id = id;
+ this.username = username;
+ this.password = password;
+ this.address = address;
+ }
+
+ public User() {
+ }
+}
+
+```
+
+src/main/java/com/xlu/admin/Address.java
+
+```java
+package com.xlu.admin;
+
+public class Address {
+ private String city;
+
+ @Override
+ public String toString() {
+ return "Address{" +
+ "city='" + city + '\'' +
+ '}';
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public Address(String city) {
+ this.city = city;
+ }
+
+ public Address() {
+ }
+}
+```
\ No newline at end of file
--
Gitee
From 8422573806dd5439eaf75e3cd2ea30f715be6d50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com>
Date: Thu, 4 Jan 2024 22:37:27 +0800
Subject: [PATCH 2/2] 1.4
---
.../20231226 Ioc\345\222\214DI.md" | 275 ++++++++++--------
.../20240103 \347\254\224\350\256\260.md" | 0
.../20240104 REST.md" | 153 ++++++++++
3 files changed, 306 insertions(+), 122 deletions(-)
rename "03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md" => "03 \350\265\226\345\277\203\345\246\215/20240103 \347\254\224\350\256\260.md" (100%)
create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20240104 REST.md"
diff --git "a/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md" "b/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md"
index becc9ad..d717e5a 100644
--- "a/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md"
+++ "b/03 \350\265\226\345\277\203\345\246\215/20231226 Ioc\345\222\214DI.md"
@@ -1,162 +1,193 @@
-## Ioc和DI
-
-```html
-IoC(Inversion of Control)控制反转
-使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转
-Spring技术对IoC思想进行了实现
-Spring提供了一个容器,称为IoC容器,用来充当IoC思想中的“外部”
-IoC容器负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean
-DI(Dependency Injection)依赖注入
-在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入
-```
-
-bean生命周期:
-
-```java
-初始化容器
-创建对象(内存分配)
-执行构造方法
-执行属性注入(set操作)
-执行bean初始化方法
-使用bean
-执行业务操作
-关闭/销毁容器
-执行bean销毁方法
-```
-
-bean销毁时机
-
-```java
-容器关闭前触发bean的销毁
-关闭容器方式:
-手工关闭容器
-ConfigurableApplicationContext接口close()操作
-注册关闭钩子,在虚拟机退出前先关闭容器再退出虚拟机
- ConfigurableApplicationContext接口registerShutdownHook()操作
-
-public class AppForLifeCycle { public static void main( String[] args ) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ctx.close(); }}
-
-```
-
-pom.xml
+### 1 pom.xml文件
```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
-
-
-
+
+ 4.0.0
+
+ com.yina
+ untitled06
+ 1.0-SNAPSHOT
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+ com.mysql
+ mysql-connector-j
+ 8.1.0
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+ org.mybatis
+ mybatis
+ 3.5.13
+
+
+ org.springframework
+ spring-context
+ 5.2.25.RELEASE
+
+
-
```
-```xml
-src/main/resources/ApplicationContext.xml
-ApplicationContext.xml
-
-
-
-
-
-
-
-```
+### 2 域名包(com.yina)下创建service包创建BookService接口
```java
-文件名:BookDaoImpl.java
-
-package com.mdd.dao.impl;
+package com.yina.service;
-import com.mdd.dao.BookDao;
-
-public class BookDaoImpl implements BookDao {
- public void save() {
- System.out.println("book dao save ...");
- }
+public interface BookService {
+ public void save();
}
```
+### 3 域名包(com.yina)下service包下创建impl包创建BookServiceImpl类
+
```java
-文件名: BookServiceImpl.java
+package com.yina.service.impl;
-package com.mdd.service.impl;
-import com.mdd.dao.BookDao;
-import com.mdd.dao.impl.BookDaoImpl;
-import com.mdd.service.BookService;
+import com.yina.dao.BookDao;
+import com.yina.dao.impl.BookDaoImpl;
+import com.yina.service.BookService;
-public class BookServiceImpl implements BookService {
+import java.util.Arrays;
+import java.util.Properties;
+// 这里就是业务层
+public class BookServiceImpl implements BookService {
+ // 业务层调用了数据层
private BookDao bookDao;
+// private int num;
+// private String bookName;
+ private int[] nums;
+ private Properties jdbc;
+ public void setJdbc(Properties jdbc) {
+ this.jdbc = jdbc;
+ }
+ public void setNums(int[] nums) {
+ this.nums = nums;
+ }
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
- public void save() {
- System.out.println("book service save ...");
+ // 构造函数的方式,这种写法很少大多是用setter写法
+// public BookServiceImpl(BookDao bookDao, int num, String bookName) {
+// this.bookDao = bookDao;
+// this.num = num;
+// this.bookName = bookName;
+// }
+
+ // setter方法
+ // public void setBookName(String bookName) {
+// this.bookName = bookName;
+// }
+//
+// public void setNum(int num) {
+// this.num = num;
+// }
+//
+// // 给bookDao赋值,方法二
+//// public BookServiceImpl(BookDao bookDao) {
+//// this.bookDao = bookDao;
+//// }
+// // 给bookDao赋值,方法一
+// public void setBookDao(BookDao bookDao) {
+// this.bookDao = bookDao;
+// }
+//
+// // = new BookDaoImpl();
+ public void save(){
+ System.out.println("book service save ..."+ Arrays.toString(nums)+jdbc.getProperty("driver"));
bookDao.save();
}
-
}
```
+### 4 在resources包下创建ApplicationContext.xml
+
+```xml
+
+
+
+
+
+
+
+
+
+
+
+ 11
+ 22
+ 33
+
+
+
+
+
+ com.mysql.cj.jdbc.Driver
+ root
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+```
+
+### 5 域名包(com.yina)下创建dao包创建BookDao接口
+
```java
-文件名:BookDao.java
-package com.mdd.dao;
+package com.yina.dao;
+
public interface BookDao {
public void save();
}
+
```
+### 6 域名包(com.yina)下创建dao包下创建impl包创建BookDaoImpl类
+
```java
-文件名: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();
+package com.yina.dao.impl;
+
+import com.yina.dao.BookDao;
+// 这里是数据层
+public class BookDaoImpl implements BookDao {
+ public void save(){
+ System.out.println("book dao save ...");
}
}
```
-```java
-文件名:BookService.java
-package com.mdd.service;
-
-public interface BookService {
- public void save();
-}
-```
\ No newline at end of file
diff --git "a/03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md" "b/03 \350\265\226\345\277\203\345\246\215/20240103 \347\254\224\350\256\260.md"
similarity index 100%
rename from "03 \350\265\226\345\277\203\345\246\215/20240104 \347\254\224\350\256\260.md"
rename to "03 \350\265\226\345\277\203\345\246\215/20240103 \347\254\224\350\256\260.md"
diff --git "a/03 \350\265\226\345\277\203\345\246\215/20240104 REST.md" "b/03 \350\265\226\345\277\203\345\246\215/20240104 REST.md"
new file mode 100644
index 0000000..6870671
--- /dev/null
+++ "b/03 \350\265\226\345\277\203\345\246\215/20240104 REST.md"
@@ -0,0 +1,153 @@
+## REST风格
+
+REST(Representational State Transfer),表现形式状态转换
+
+- 传统风格资源描述形式
+
+ http://localhost/user/getById?id=1
+
+ http://localhost/user/saveUser
+
+- REST风格描述形式
+
+ http://localhost/user/1
+
+ http://localhost/user
+
+优点:隐藏资源的访问行为,无法通过地址得知对资源是何种操作书写简化
+
+### 按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
+
+http://localhost/users 查询全部用户信息 GET(查询)
+
+http://localhost/users/1 查询指定用户信息 GET(查询)
+
+http://localhost/users 添加用户信息 POST(新增/保存
+
+http://localhost/users 修改用户信息 PUT(修改/更新)
+
+http://localhost/users/1 删除用户信息 DELETE(删除)
+
+# 案例
+
+SpringConfig
+
+```java
+package com.mdd.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+@Configuration
+@ComponentScan({"com.mdd.mapper","com.mdd.service"})
+@Import({JdbcConfig.class, MybatisConfig.class})
+public class SpringConfig {
+}
+
+```
+
+MybatisConfig
+
+```java
+package com.mdd.config;
+
+import com.sun.deploy.security.MSCredentialManager;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.mapper.MapperScannerConfigurer;
+
+import javax.sql.DataSource;
+
+public class MybatisConfig {
+ public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
+ SqlSessionFactoryBean sql = new SqlSessionFactoryBean();
+ sql.setTypeHandlersPackage("com.mdd.domain");
+ sql.setDataSource(dataSource);
+ return sql;
+ }
+
+ public MapperScannerConfigurer mc(){
+ MapperScannerConfigurer mapp = new MapperScannerConfigurer();
+ mapp.setBasePackage("com.mdd.mapper");
+ return mapp;
+ }
+}
+
+```
+
+JdbcConfig
+
+```java
+package com.mdd.config;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.context.annotation.Bean;
+
+import javax.sql.DataSource;
+
+public class JdbcConfig {
+ @Bean
+ public DataSource dataSource(){
+ DruidDataSource dr = new DruidDataSource();
+ dr.setDriverClassName("com.mysql.cj.jdbc.Driver");
+ dr.setUrl("jdbc:mysql:///user01");
+ dr.setUsername("root");
+ dr.setPassword("123456");
+ return dr;
+ }
+}
+
+```
+
+BrandController
+
+```java
+package com.mdd.controller;
+
+import com.mdd.domian.Brand;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping("/brands")
+public class BrandController {
+ @PostMapping
+ public String addBrand(@RequestBody Brand brand){
+ System.out.println("添加了一条数据==》"+brand);
+ return "add ok!";
+ }
+
+ @DeleteMapping("/{id}")
+ public String deleteBrand(@PathVariable Integer id){
+ System.out.println("删除了一条数据==》"+id);
+ return "delete ok!";
+ }
+
+ @PutMapping
+ public String updateBrand(@RequestBody Brand brand){
+ System.out.println("修改了一条数据==》"+brand);
+ brand.setName("老赵");
+ System.out.println("修改成功!");
+ return "update ok!";
+ }
+
+ @GetMapping
+ public List getBrand(Brand brand){
+ ArrayList brands = new ArrayList();
+ brands.add(new Brand(1,"你好","男性",18));
+ brands.add(new Brand(2,"你好2","女性",38));
+ brands.add(new Brand(3,"你好3","男性",28));
+ brands.add(new Brand(4,"你好3","女性",48));
+ return brands;
+ }
+
+ @GetMapping("/{id}")
+ public Brand gettBrand(@PathVariable Integer id){
+ System.out.println("查询到一条数据==》"+id);
+ return new Brand(id,"你好369","女性",48);
+ }
+}
+
+```
\ No newline at end of file
--
Gitee