diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230109 \346\200\273\345\244\215\344\271\240.md" "b/14 \346\235\216\344\277\212\345\205\264/20230109 \346\200\273\345\244\215\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..ddd670f35a27fcc6125b00f515ffc9a066d10496 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230109 \346\200\273\345\244\215\344\271\240.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文件 \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/20240104 SpringMVC.md" "b/14 \346\235\216\344\277\212\345\205\264/20240104 SpringMVC.md" new file mode 100644 index 0000000000000000000000000000000000000000..14720363f75b17e99dfdeef0a74f1ab86a087413 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20240104 SpringMVC.md" @@ -0,0 +1,585 @@ +pom.xml + +```xml + + 4.0.0 + com.mdd + springmvc_04 + 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.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/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; + +@Configuration +@ComponentScan("com.mdd.controller") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +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() { +// SpringConfig.class + return new Class[]{SpringConfig.class}; // SpringConfig.class + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +src/main/java/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; + + + // 提供getter/setter方法,有参,无参构造器,toString,也就是标准的JavaBean ,快捷方式ALT+insert + + 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 + + '}'; + } +} + +``` + +src/main/java/com/mdd/domain/R.java + +```java +package com.mdd.domain; + +public class R { + private Integer code; + private String msg; + private Object date; + + public R() { + } + + public static R success(Integer code,Object data){ + return new R(code,data); + } + + public static R success(Integer code,String msg){ + return new R(code,msg); + } + + + + public R(Integer code, Object date) { + this.code = code; + this.date = date; + } + + public R(Integer code, String msg, Object date) { + this.code = code; + this.msg = msg; + this.date = date; + + } + + public R(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + 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 getDate() { + return date; + } + + public void setDate(Object date) { + this.date = date; + } + + @Override + public String toString() { + return "R{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", date=" + date + + '}'; + } +} + +``` + +src/main/java/com/mdd/controller/BrandController.java + +```java +package com.mdd.controller; + +import com.mdd.domain.Brand; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +//@Controller +//@ResponseBody +@RestController +@RequestMapping( "/brands") +public class BrandController { + // 表现层要调用业务层 + @Autowired + private BrandService brandService; + // 添加 + @PostMapping + public String addBrand(@RequestBody Brand brand){ + System.out.println("接收到一个Brand==》"+brand); + brandService.addBrand(brand); + return "add ok!"; + } + + + // 修改 + @PutMapping + public String editBrand(@RequestBody Brand brand){ + brandService.editBrand(brand); + return "add ok!"; + } + + // 查所有 + + @GetMapping + public List getBrands(){ + List brands = brandService.selectAll(); + return brands; + } + + // 查单个 + @GetMapping("/{id}") + public Brand getBrand(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库从查询到了。id="+id+"的品牌"); + + return new Brand(id,"华为10086","大华为",100,"科技公司",1); + } + + // 删除 + @DeleteMapping("/{id}") + public String delBrand(@PathVariable Integer id){ + System.out.println("接收到一个id==》"+id); + System.out.println("从数据库从删除了。id="+id+"的品牌"); + return "delete ok!"; + } +} +``` + +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.service", "com.mdd.mapper"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} + +``` + +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"); //123456 + return ds; + } +} +``` + +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 ssfb = new SqlSessionFactoryBean(); + // 设置实体类的包 + ssfb.setTypeAliasesPackage("com.mdd.domain"); + // 设置数据源dataSource + ssfb.setDataSource(dataSource); + return ssfb; + } + + // 设置Mapper + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + // 设置代理的包 + msc.setBasePackage("com.mdd.mapper"); + return msc; + } +} + +``` + +src/main/java/com/mdd/mapper/BrandMapper.java + +```java +package com.mdd.service.impl; + +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要调用数据层的对象,所以先自动装配BrandMapper + @Autowired + private BrandMapper brandMapper; + + + @Override + public List selectAll() { + System.out.println("业务层开始:查询所有"); + return brandMapper.selectAll(); + } + + @Override + public Brand selectById(int id) { + System.out.println("业务层开始:查某个"); + + return brandMapper.selectById(id); + } + + @Override + public void deleteById(int id) { + brandMapper.deleteById(id); + } + + @Override + public void addBrand(Brand brand) { + brandMapper.addBrand(brand); + } + + @Override + public void editBrand(Brand brand) { + brandMapper.editBrand(brand); + } +} + +``` + +src/main/java/com/mdd/service/impl/BrandServiceImpl.java + +```java +package com.mdd.service.impl; + +import com.mdd.domain.Brand; +import com.mdd.mapper.BrandMapper; +import com.mdd.service.BrandService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BrandServiceImpl implements BrandService { + // 业务层要调用数据层的对象,所以先自动装配BrandMapper + @Autowired + private BrandMapper brandMapper; + + + @Override + public List selectAll() { + System.out.println("业务层开始:查询所有"); + return brandMapper.selectAll(); + } + + @Override + public Brand selectById(int id) { + System.out.println("业务层开始:查某个"); + + return brandMapper.selectById(id); + } + + @Override + public void deleteById(int id) { + brandMapper.deleteById(id); + } + + @Override + public void addBrand(Brand brand) { + brandMapper.addBrand(brand); + } + + @Override + public void editBrand(Brand brand) { + brandMapper.editBrand(brand); + } +} + +``` + +src/main/java/com/mdd/service/BrandService.java(接口) + +```java +package com.mdd.service; + +import com.mdd.domain.Brand; + +import java.util.List; + +public interface BrandService { + // 查所有 + + List selectAll(); + + // id查某个 + + Brand selectById(int id); + + // id 删除某个 + void deleteById(int id); + + // 添加 + + void addBrand(Brand brand); + + + // 修改 + + void editBrand(Brand brand); +} + + +``` \ No newline at end of file