From e5a1877712ca8f731fb2e75091dd39fb3f4913e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Wed, 10 Jan 2024 05:10:10 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- ...05\346\200\273\344\275\234\344\270\232.md" | 568 ++++++++++++++++++ 1 file changed, 568 insertions(+) create mode 100644 "49 \346\235\216\350\210\222\346\261\266/20240105\346\200\273\344\275\234\344\270\232.md" diff --git "a/49 \346\235\216\350\210\222\346\261\266/20240105\346\200\273\344\275\234\344\270\232.md" "b/49 \346\235\216\350\210\222\346\261\266/20240105\346\200\273\344\275\234\344\270\232.md" new file mode 100644 index 0000000..c0cc422 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/20240105\346\200\273\344\275\234\344\270\232.md" @@ -0,0 +1,568 @@ +pom.xml + +```xm + + 4.0.0 + com.mdd + ssm + 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.9.1 + + + + + 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 + + + + + + + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 80 + / + + utf-8 + + + + + + + +``` + +src/main/java/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 +@ComponentScan({"com.mdd.mapper","com.mdd.service"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} + +``` + +src/main/java/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 { + + //实体类,数据库连接 + @Bean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + + SqlSessionFactoryBean ssfn = new SqlSessionFactoryBean(); + ssfn.setDataSource(dataSource); + ssfn.setTypeAliasesPackage("com.mdd.domain"); + + return ssfn; + + } + + //mapper + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + + } + + + +} +``` + +src/main/java/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 { + + @Bean + public DataSource dataSource(){ + + DruidDataSource ds = new DruidDataSource(); + + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///mybatis"); + ds.setUsername("root"); + ds.setPassword("root"); + + return ds; + + } +} + +``` + +src/main/java/com/mdd/config/SpringMvcConfig.java + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan("com.mdd") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + + //放行 + registry.addResourceHandler("/css/**").addResourceLocations("/css/"); + registry.addResourceHandler("/js/**").addResourceLocations("/js/"); + registry.addResourceHandler("/lib/**").addResourceLocations("/lib/"); + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico"); + + } + + //ctrl+o(字母o) 重写方法 + // add+H 快捷方式 + +} + +``` + +src/main/java/com/mdd/config/WebConfig.java + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + @Override + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +src/main/java/com/mdd/controller/BookController.java + +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +import com.mdd.domain.R; +import com.mdd.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/books") +public class BookController { + + @Autowired + private BookService bookService; + + //查看数据库数据,带提示词 + @GetMapping + public R getAll(){ + List books = bookService.getAll(); + return R.success(200,books); + } + + //查看数据库数据 +// @GetMapping +// public List getAll(){ +// return bookService.getAll(); +// } + + + //添加数据 + @PostMapping + //添加数据 + public Book addBook(@RequestBody Book book){ + bookService.addBook(book); + return book; + + } + + //修改数据 + @PutMapping + public Book updateBook(@RequestBody Book book){ + bookService.updateBook(book); + return book; + } + + //删除 + @DeleteMapping("/{id}") + public R deleteBookById(@PathVariable Integer id){ + Integer i = bookService.deleteBook(id); + if (i>0) { + return R.success(200,"删除成功!"); + }else{ + return R.failure(404,"删除失败"); + } + + } + + //根据id查询 + @GetMapping("/{id}") + public Book getBook(@PathVariable Integer id){ + return bookService.getBook(id); + } + + + +} +``` + +src/main/java/com/mdd/domain/Book.java + +```java +package com.mdd.domain; + +public class Book { + + private Integer id; + private String bookName; + private String author; + private String publisher; + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", bookName='" + bookName + '\'' + + ", author='" + author + '\'' + + ", publisher='" + publisher + '\'' + + '}'; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBookName() { + return bookName; + } + + public void setBookName(String bookName) { + this.bookName = bookName; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.publisher = publisher; + } + + public Book() { + } +} + +``` + +src/main/java/com/mdd/domain/R.java + +```java +package com.mdd.domain; + +public class R { + + private Integer code;//错误代码 + private String msg;//提示信息 + private Object data;//返回的数据,Object通用的 + + @Override + public String toString() { + return "R{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R() { + } + + public static R success(Integer code, Object data) { + R r = new R(); + r.setData(data); + r.setCode(code); + return r; + } + + public static R success(Integer code,String msg){ + R r = new R(); + r.setCode(code); + r.setMsg(msg); + return r ; + } + + public static R failure(Integer code,String msg){ + R r = new R(); + r.setMsg(msg); + r.setCode(code); + return r; + } + +} + +``` + +src/main/java/com/mdd/mapper/BookMapper.java + +```java +package com.mdd.mapper; + +import com.mdd.domain.Book; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +public interface BookMapper { + + + @Select("select id, book_name as bookName, author, publisher from tb_book order by id desc") + List getAll(); + + @Insert("insert into tb_book (book_name, author, publisher) values (#{bookName},#{author},#{publisher})") + void addBook(Book book); + + @Update("update tb_book set book_name = #{bookName},author = #{author},publisher = #{publisher} where id = #{id}") + void updateBook(Book book); + @Delete("delete from tb_book where id = #{id};") + Integer deleteBook(Integer id); + @Select("select id, book_name as bookName, author, publisher from tb_book where id = #{id}") + Book getBook(Integer id); +} + +``` + +src/main/java/com/mdd/service/BookService.java + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + void addBook(Book book); + + void updateBook(Book book); + + Integer deleteBook(Integer id); + + Book getBook(Integer id); +} + +``` + +src/main/java/com/mdd/service/Impl/BookServiceImpl.java + +```java +package com.mdd.service.Impl; + +import com.mdd.domain.Book; +import com.mdd.mapper.BookMapper; +import com.mdd.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +import java.util.List; + +@Service +public class BookServiceImpl implements BookService { + + @Autowired + private BookMapper bookMapper; + + public List getAll() { + return bookMapper.getAll(); + } + + public void addBook(Book book) { + bookMapper.addBook(book); + } + + + public void updateBook(Book book) { + bookMapper.updateBook(book); + } + + + public Integer deleteBook(Integer id) { + return bookMapper.deleteBook(id); + } + + + public Book getBook(Integer id) { + return bookMapper.getBook(id); + } +} + +``` + +记得在webapp文件加入html文件 -- Gitee From 3adb6eacb8f30861ca618df32c5adab5f0d7c9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Wed, 10 Jan 2024 05:13:08 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- ...8Mybatis\346\225\264\345\220\210spring.md" | 375 ++++++++++++++++++ 1 file changed, 375 insertions(+) create mode 100644 "49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" diff --git "a/49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" "b/49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" new file mode 100644 index 0000000..ff2fa98 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" @@ -0,0 +1,375 @@ +#### 1.打开IDEA,新建一个项目(模块),开启MySQL + +#### 2.编写Pom.mxl文件,导入相关依赖的坐标 + +#### 3.创建数据库的相关表,列,并导入数据 + +#### 4.在src下的 main下的 java里域名包(com.mdd)创建domain包,并在 domain下创建实体类 Brand + +```java +package com.nct.domain; + +public class Brand { + private Integer id; + private String brandName; + private String companyName; + private Integer ordered; + private String description; + private Integer status; + + @Override + public String toString() { + return "Brand{" + + "id=" + id + + ", brandName='" + brandName + '\'' + + ", companyName='" + companyName + '\'' + + ", ordered=" + ordered + + ", description='" + description + '\'' + + ", 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; + } + + 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; + } +} +``` + +#### 5.编写代理接口 在com.nct.包下创建mapper包创建BrandMapper代理接口,用来从数据库从获取数据 + +```java +package com.nct.mapper; + +import com.nct.domain.Brand; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +// 代理接口,用来获取数据库数据 +public interface BrandMapper { + // 写增删改查的各种方法 + // 1 查所有Brand + // 使用别名是因为字段名和实体类的变量不一致,会导致查询结果为空 + @Select("select id,brand_name as brandName,company_name as companyName,ordered,description,status from tb_brand") // 简单的语句可以直接使用注解开发 + List findAll(); // 因为查询结果是集合所以用集合 +} +``` + +#### 6.在 com.nct包下创建config 包,编写Spring核心配置类文件SpringConfig.java,写上@Configuration注解,将这个类变成配置类 + +```java +@Configuration +// 引入 JdbcConfig,MybatisConfig 配置文件 +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +``` + +7.在 com.mdd.config 目录下,编写 JdbcConfig.java也就是mysql驱动配置类文件 + +```java +package com.nct.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + // jdbc负责连接mysql,配置驱动,url,用户名,密码 + // 通过写一个方法,返回这个bean的类型,并上这个方法写一个注解@Bean,让这个spring加载第三方的bean + @Bean + public DataSource dataSource(){ + // 这里要用DruidDataSource + DruidDataSource ds = new DruidDataSource(); + // 配置连接四要素:驱动,url,用户名,密码 + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///db_2023?useSSL=false&allowPublicKeyRetrieval=true"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} +``` + +#### 8.再次编写Spring核心配置类文件SpringConfig.java + +```java +package com.nct.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +// @Configuration声明此类是一个配置类 +@Configuration +// 导入JdbcConfig +@Import(JdbcConfig.class) +public class SpringConfig { +} +``` + +#### 9.在com.nct目录下创建App测试类 + +```java +package com.nct; + +import com.nct.config.SpringConfig; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import javax.sql.DataSource; + +public class App { + public static void main(String[] args) { + // 1 通过Spring核心配置文件,获取IOC容器 + ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); + // 2 通过IOC容器对象的getBean获取对应的bean(对象) + DataSource dataSource = ctx.getBean(DataSource.class); + System.out.println(dataSource); + + } +} +``` + +#### 10.在config包创建MybatisConfig类 + +```java +package com.nct.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 + @Bean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + // 1 设置实体类的别名 + ssfb.setTypeAliasesPackage("com.nct.domain"); // 实体类所在的包的路径 + // 2 设置数据语言 + ssfb.setDataSource(dataSource); + return ssfb; + } + // 2 获取Mapper代理的包 + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(DataSource dataSource){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.nct.mapper"); // 代理类的包的路径 + return msc; + } +} +``` + +#### 11.再一次编写SpringConfig类 + +```java +package com.nct.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +// @Configuration声明此类是一个配置类 +@Configuration +@Import({JdbcConfig.class, MybatisConfig.class}) +@ComponentScan("com.nct") +public class SpringConfig { +} +``` + +#### 12.在Test包下java包下创建域名(com.nct)包下创建test包创建BrandTest测试类 + +```java +package com.nct.test; + +import com.nct.domain.Brand; +import com.nct.mapper.BrandMapper; +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为junit专门提供的运行类 +@ContextConfiguration(classes = SpringConfig.class) // 加载spring核心配置,可以理解为加载IOC容器 +public class BrandTest { + // 通过自动装配的形式,直接将要bean注入进来 + @Autowired + private BrandMapper brandMapper; + @Test + public void test01(){ +// List all = brandMapper.findAll(); +// System.out.println(all); + // 1 模拟浏览器向服务器发送查询数量 + String brandName = "得啵椅子"; + String companyName = "IST"; + // 1.5 因为要模糊查询,所以要对原始数据,做加%处理 + brandName = "%"+brandName+"%"; + companyName = "%"+companyName+"%"; + // 2 将查询数据封装成一个brand对象 + Brand brand = new Brand(); + brand.setBrandName(brandName); + brand.setCompanyName(companyName); + // 3 调用代理对象,数据区这个brand + List brands = brandMapper.selectByCondition(brand); + System.out.println(brands); + } +} +``` + +#### 13.模拟业务层,先有一个接口,再写一个实现类 + +##### 在com.nct包下创建service包创建BrandService接口 + +```java +package com.nct.service; + +import com.nct.domain.Brand; + +import java.util.List; + +public interface BrandService { + List selectAll(); +} +``` + +##### 在service包下创建impl包创建BrandServiceImpl类 + +```java +package com.nct.service.impl; + +import com.nct.domain.Brand; +import com.nct.mapper.BrandMapper; +import com.nct.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要实现数据的调用,就要借助数据层的对象 + // 将数据层的对象,自动装配进来 + @Autowired + private BrandMapper brandMapper; + @Override + public List selectAll(){ + System.out.println("开始业务"); + return brandMapper.findAll(); + } +} +``` + +#### 14.再试试BrandTest测试类 + +```java +package com.nct.test; + +import com.nct.config.SpringConfig; +import com.nct.domain.Brand; +import com.nct.mapper.BrandMapper; +import com.nct.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为junit专门提供的运行类 +@ContextConfiguration(classes = SpringConfig.class) // 加载spring核心配置,可以理解为加载IOC容器 +public class BrandTest { + // 通过自动装配的形式,直接将要bean注入进来 + @Autowired + private BrandMapper brandMapper; + @Autowired + private BrandService brandService; // 要什么就自动装配什么 + @Test + public void test01(){ +// List all = brandMapper.findAll(); +// System.out.println(all); + // 1 模拟浏览器向服务器发送查询数量 + String brandName = "得"; + String companyName = "I"; + // 1.5 因为要模糊查询,所以要对原始数据,做加%处理 + brandName = "%"+brandName+"%"; + companyName = "%"+companyName+"%"; + // 2 将查询数据封装成一个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); + } +} +``` -- Gitee From ebf8e25e044b13803d14d60a5c77ca06f3c88121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Wed, 10 Jan 2024 05:14:10 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2049=20?= =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6/20231228Mybatis=E6=95=B4=E5=90=88sp?= =?UTF-8?q?ring.md=20=E4=B8=BA=2049=20=E6=9D=8E=E8=88=92=E6=B1=B6/202312.2?= =?UTF-8?q?8Mybatis=E6=95=B4=E5=90=88spring.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../202312.28Mybatis\346\225\264\345\220\210spring.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" => "49 \346\235\216\350\210\222\346\261\266/202312.28Mybatis\346\225\264\345\220\210spring.md" (100%) diff --git "a/49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" "b/49 \346\235\216\350\210\222\346\261\266/202312.28Mybatis\346\225\264\345\220\210spring.md" similarity index 100% rename from "49 \346\235\216\350\210\222\346\261\266/20231228Mybatis\346\225\264\345\220\210spring.md" rename to "49 \346\235\216\350\210\222\346\261\266/202312.28Mybatis\346\225\264\345\220\210spring.md" -- Gitee From 46a171b6536cfdea8d7abf8f309aad25681a6a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Wed, 10 Jan 2024 05:17:11 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- .../202312.19Maven.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "49 \346\235\216\350\210\222\346\261\266/202312.19Maven.md" diff --git "a/49 \346\235\216\350\210\222\346\261\266/202312.19Maven.md" "b/49 \346\235\216\350\210\222\346\261\266/202312.19Maven.md" new file mode 100644 index 0000000..8efd4a6 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/202312.19Maven.md" @@ -0,0 +1,34 @@ +## Maven + +Maven是专门用于管理和构建Java项目的工具,它的主要功能有: + +提供了一套标准化的项目结构 + +提供了一套标准化的构建流程(编译,测试,打包,发布……) + +提供了一套依赖管理机制 + +仓库分类: 本地仓库:自己计算机上的一个目录 + +中央仓库:由Maven团队维护的全球唯一的仓库 + +地址: [https://repo1.maven.org/maven2/](https://gitee.com/link?target=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2F) + +远程仓库(私服):一般由公司团队搭建的私有仓库,国内镜像也是远程仓库的一种 当项目中使用坐标引入对应依赖jar包后,首先会查找本地仓库中是否有对应的jar 包: 如果有,则在项目直接引用; + +如果没有,则去中央仓库中下载对应的jar包到本地仓库。 + +```java +import org.junit.Test; + +public class APP { + @Test + public void test01(){ + System.out.println(999); + } + @Test + public void test02(){ + System.out.println("吃饭了吗"); + } +} +``` -- Gitee From 4f4dd49c69b6db7c60ec37063667e77a35faac28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com> Date: Wed, 10 Jan 2024 05:20:00 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李舒汶 <3228916049@qq.com> --- ...VC\350\277\233\345\214\226\347\211\210.md" | 448 ++++++++++++++++++ 1 file changed, 448 insertions(+) create mode 100644 "49 \346\235\216\350\210\222\346\261\266/20240103SpringMVC\350\277\233\345\214\226\347\211\210.md" diff --git "a/49 \346\235\216\350\210\222\346\261\266/20240103SpringMVC\350\277\233\345\214\226\347\211\210.md" "b/49 \346\235\216\350\210\222\346\261\266/20240103SpringMVC\350\277\233\345\214\226\347\211\210.md" new file mode 100644 index 0000000..2094864 --- /dev/null +++ "b/49 \346\235\216\350\210\222\346\261\266/20240103SpringMVC\350\277\233\345\214\226\347\211\210.md" @@ -0,0 +1,448 @@ +## SpringMvc进阶一点版 + +### com.mdd.config + +#### 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 druidDataSource = new DruidDataSource(); + druidDataSource.setDriverClassName("com.sql.cj.jdbc.Driver"); + druidDataSource.setUrl("jdbc:mysql:///db_2023?useSSL=false"); + druidDataSource.setUsername("root"); + druidDataSource.setPassword("root"); + return druidDataSource; + } +} +``` + +#### MybatisConfig + +```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 { + @Bean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + ssfb.setTypeAliasesPackage("com.mdd.domain"); + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(DataSource dataSource){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + } +} +``` + +#### 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") +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +``` + +#### SpringMvcConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.mdd.controller") +@EnableWebMvc +public class SpringMvcConfig { +} +``` + +#### WebConfig + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + @Override + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} +``` + +### com.mdd.domain + +#### Address + +```java +package com.mdd.domain; + +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() { + } + + public Address(String city) { + this.city = city; + } +} +``` + +#### Book + +```java +package com.mdd.domain; + +public class Book { + private Integer id; + private String bookName; + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", bookName='" + bookName + '\'' + + '}'; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBookName() { + return bookName; + } + + public void setBookName(String bookName) { + this.bookName = bookName; + } + + public Book() { + } + + public Book(Integer id, String bookName) { + this.id = id; + this.bookName = bookName; + } +} +``` + +#### User + +```JAVA +package com.mdd.domain; + +import java.util.Date; + +public class User { + private Integer id; + private String username; + private String password; + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } + + 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 User() { + } + + public User(Integer id, String username, String password) { + this.id = id; + this.username = username; + this.password = password; + } +} +``` + +### com.mdd.controller + +#### BookController + +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +import com.mdd.service.UserService; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/books") +public class BookController { + private UserService userService; + @GetMapping + public List getBooks(){ + ArrayList books = new ArrayList(); + books.add(new Book(1,"融心")); + return books; + } + + @GetMapping("/{id}") + public Book getBookById(@PathVariable Integer id){ + return new Book(id,"融心"); + } + + @PostMapping("/{id}") + public Book addBook(@RequestBody Book book){ + return book; + } + +// @RequestParam 普通参数 +// @PathVariable 路径参数,1个参数 +// @RequestBody json形式的参数,封装多个参数 +} +``` + +#### UserController + +```java +package com.mdd.controller; + +import com.mdd.domain.User; +import com.mdd.service.UserService; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + + +@RestController +@RequestMapping("/users") +public class UserController { +// // 接收用户名和密码,登录 +// @RequestMapping("/login") +// public String login(User user){ +// String result = "login error"; +// if("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())){ +// result = "login success"; +// } +// return result; +// } +// +// @RequestMapping(value = "/reg",produces = "text/html;charset=utf-8") +// public String reg(User user){ +// System.out.println(user); +// return "注册成功"; +// } +// +//// 接收时间 +// @RequestMapping(value = "/date") +// public String getDate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){ +// System.out.println(date); +// return "ok"; +// } +// +// @RequestMapping("/jsonArray") +// public String jsonData(@RequestBody List names){ +// System.out.println(names.toString()); +// return "ok"; +// } +// +//// @RequestMapping("/userJson") +//// public String jsonUser(@RequestBody List user){ +//// System.out.println(user); +//// return user.toString(); +//// } +// +// // 这个写法可以解决乱码的事情 +// @RequestMapping("/userJson") +// public List jsonUser(@RequestBody List user){ +// System.out.println(user); +// return user; +// } + + // 查询所有用户 get + @GetMapping + public List getAll(){ + List list = new ArrayList(); + list.add(new User(1,"牙牙","root")); + list.add(new User(2,"牙牙1","root")); + list.add(new User(3,"牙牙2","root")); + list.add(new User(4,"牙牙3","root")); + return list; + } + + // 添加用户 post + @PostMapping + public User addUser(@RequestBody User user){ + System.out.println("添加一个新用户"+user); + return user; + } + + // 修改用户 put + @PutMapping + public User updateUser(@RequestBody User user){ + System.out.println("更新一个旧用户"+user); + return user; + } + + // 按id查询用户 get + @GetMapping("/{id}") + public User getUser(@PathVariable Integer id){ + System.out.println("根据id"+id+"查询用户"); + return new User(id,"xxx","xxx"); + } + + // 按id删除用户 delete + @DeleteMapping("/{id}") + public User deleteUser(@PathVariable Integer id){ + System.out.println("根据id"+id+"删除用户"); + return new User(id,"xxx","xxx"); + } +} +``` + +### com.mdd.mapper + +#### UserMapper接口 + +```java +package com.mdd.mapper; + +import com.mdd.domain.User; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +public interface UserMapper { + @Select("select * from tb_user") + List findAll(); +} +``` + +### com.mdd.service + +#### UserService接口 + +```java +package com.mdd.service; + +import com.mdd.domain.User; + +import java.util.List; + +public interface UserService { + List findAll(); +} +``` + +### com.mdd.service.impl + +#### UserServiceImpl + +```java +package com.mdd.service.impl; + +import com.mdd.domain.User; +import com.mdd.mapper.UserMapper; +import com.mdd.service.UserService; +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; + @Override + public List findAll() { + return userMapper.findAll(); + } +} +``` -- Gitee