From 0d3be4b26f604bac366c073dc5b839296a428051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=9B=AA=E8=8E=B2=E7=9A=84=E4=BD=9C=E4=B8=9A?= <1653483984@qq.com> Date: Sun, 14 Jan 2024 23:20:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AB=E9=9B=AA=E8=8E=B2=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20240108 \345\244\215\344\271\240.md" | 390 +++++++++++++++++ .../20240109 \345\244\215\344\271\240.md" | 390 +++++++++++++++++ .../20240111 \345\244\215\344\271\240.md" | 390 +++++++++++++++++ ...40\347\224\250\346\210\267\350\241\250.md" | 400 ++++++++++++++++++ 4 files changed, 1570 insertions(+) create mode 100644 "37 \351\227\253\351\233\252\350\216\262/20240108 \345\244\215\344\271\240.md" create mode 100644 "37 \351\227\253\351\233\252\350\216\262/20240109 \345\244\215\344\271\240.md" create mode 100644 "37 \351\227\253\351\233\252\350\216\262/20240111 \345\244\215\344\271\240.md" create mode 100644 "37 \351\227\253\351\233\252\350\216\262/20240112 \345\244\215\344\271\240\347\224\250\346\210\267\350\241\250.md" diff --git "a/37 \351\227\253\351\233\252\350\216\262/20240108 \345\244\215\344\271\240.md" "b/37 \351\227\253\351\233\252\350\216\262/20240108 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..db52ba2 --- /dev/null +++ "b/37 \351\227\253\351\233\252\350\216\262/20240108 \345\244\215\344\271\240.md" @@ -0,0 +1,390 @@ +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig { +} + +``` + +```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.controller") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +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 bookList=bookService.getAll(); + Integer code=bookList !=null? 2001:2000; + Object data=bookList !=null? bookList:"查询数据失败,可能没有数据!"; + return new R(code, data); + } + //添加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flat= bookService.addBook(book); + Integer code=flat ? 3001:3000; + Object data=flat ? "添加成功":"添加失败!"; + return new R(code, data); + } + //修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flat= bookService.updateBook(book); + Integer code=flat? 4001:4000; + Object data=flat ? "修改成功":"修改失败!"; + return new R(code,data); + } + ////根据id查询 + @GetMapping("/{id}") + public R getById(@PathVariable Integer id){ + Book book=bookService.getById(id); + Integer code=book !=null? 2001:2000; + Object data=book !=null? book:"查询数据失败,可能没有数据!"; + return new R(code,data); + } + //根据id删除 + @DeleteMapping("/{id}") + public R delById(@PathVariable Integer id){ + Boolean flat=bookService.delById(id); + Integer code=flat? 5001:5000; + Object data=flat ? "删除成功":"删除失败!"; + return new R(code,data); + } + + + +} + +``` + +```java +package com.mdd.controller; + +public class Code { + public static final Integer GET_OK =2001; + public static final Integer POST_OK =3001; + public static final Integer PUT_OK =4001; + public static final Integer DELETE_OK =5001; + + + public static final Integer GET_ERR =2000; + public static final Integer POST_ERR =3000; + public static final Integer PUT_ERR =4000; + public static final Integer DELETE_ERR =5000; + + +} + +``` + +```java +package com.mdd.controller; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R(Integer code, Object data) { + this.code = code; + this.data = data; + } + + 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 getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} + +``` + +```java +package com.mdd.domain; + +public class Book { + private Integer id; + private String bookName; + private String author; + private String publisher; + + public Book() { + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.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; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", bookName='" + bookName + '\'' + + ", author='" + author + '\'' + + ", publisher='" + publisher + '\'' + + '}'; + } +} + +``` + +```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 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})") + Integer addBook(Book book); + + @Update("update tb_book set book_name=#{bookName}, author=#{author}, publisher=#{publisher} where id=#{id}") + Integer updateBook(Book book); + + @Select("select id, book_name bookName, author, publisher from tb_book where id=#{id}") + Book getById(Integer id); + + @Delete("delete from tb_book where id=#{id}") + Integer delById(Integer id); +} + +``` + +```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 boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + + public Boolean delById(Integer id) { + return bookMapper.delById(id)>0; + } +} + +``` + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + boolean addBook(Book book); + + Boolean updateBook(Book book); + + Book getById(Integer id); + + Boolean delById(Integer id); +} + +``` + diff --git "a/37 \351\227\253\351\233\252\350\216\262/20240109 \345\244\215\344\271\240.md" "b/37 \351\227\253\351\233\252\350\216\262/20240109 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..db52ba2 --- /dev/null +++ "b/37 \351\227\253\351\233\252\350\216\262/20240109 \345\244\215\344\271\240.md" @@ -0,0 +1,390 @@ +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig { +} + +``` + +```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.controller") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +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 bookList=bookService.getAll(); + Integer code=bookList !=null? 2001:2000; + Object data=bookList !=null? bookList:"查询数据失败,可能没有数据!"; + return new R(code, data); + } + //添加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flat= bookService.addBook(book); + Integer code=flat ? 3001:3000; + Object data=flat ? "添加成功":"添加失败!"; + return new R(code, data); + } + //修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flat= bookService.updateBook(book); + Integer code=flat? 4001:4000; + Object data=flat ? "修改成功":"修改失败!"; + return new R(code,data); + } + ////根据id查询 + @GetMapping("/{id}") + public R getById(@PathVariable Integer id){ + Book book=bookService.getById(id); + Integer code=book !=null? 2001:2000; + Object data=book !=null? book:"查询数据失败,可能没有数据!"; + return new R(code,data); + } + //根据id删除 + @DeleteMapping("/{id}") + public R delById(@PathVariable Integer id){ + Boolean flat=bookService.delById(id); + Integer code=flat? 5001:5000; + Object data=flat ? "删除成功":"删除失败!"; + return new R(code,data); + } + + + +} + +``` + +```java +package com.mdd.controller; + +public class Code { + public static final Integer GET_OK =2001; + public static final Integer POST_OK =3001; + public static final Integer PUT_OK =4001; + public static final Integer DELETE_OK =5001; + + + public static final Integer GET_ERR =2000; + public static final Integer POST_ERR =3000; + public static final Integer PUT_ERR =4000; + public static final Integer DELETE_ERR =5000; + + +} + +``` + +```java +package com.mdd.controller; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R(Integer code, Object data) { + this.code = code; + this.data = data; + } + + 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 getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} + +``` + +```java +package com.mdd.domain; + +public class Book { + private Integer id; + private String bookName; + private String author; + private String publisher; + + public Book() { + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.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; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", bookName='" + bookName + '\'' + + ", author='" + author + '\'' + + ", publisher='" + publisher + '\'' + + '}'; + } +} + +``` + +```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 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})") + Integer addBook(Book book); + + @Update("update tb_book set book_name=#{bookName}, author=#{author}, publisher=#{publisher} where id=#{id}") + Integer updateBook(Book book); + + @Select("select id, book_name bookName, author, publisher from tb_book where id=#{id}") + Book getById(Integer id); + + @Delete("delete from tb_book where id=#{id}") + Integer delById(Integer id); +} + +``` + +```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 boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + + public Boolean delById(Integer id) { + return bookMapper.delById(id)>0; + } +} + +``` + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + boolean addBook(Book book); + + Boolean updateBook(Book book); + + Book getById(Integer id); + + Boolean delById(Integer id); +} + +``` + diff --git "a/37 \351\227\253\351\233\252\350\216\262/20240111 \345\244\215\344\271\240.md" "b/37 \351\227\253\351\233\252\350\216\262/20240111 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..db52ba2 --- /dev/null +++ "b/37 \351\227\253\351\233\252\350\216\262/20240111 \345\244\215\344\271\240.md" @@ -0,0 +1,390 @@ +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig { +} + +``` + +```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.controller") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +```java +package com.mdd.controller; + +import com.mdd.domain.Book; +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 bookList=bookService.getAll(); + Integer code=bookList !=null? 2001:2000; + Object data=bookList !=null? bookList:"查询数据失败,可能没有数据!"; + return new R(code, data); + } + //添加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flat= bookService.addBook(book); + Integer code=flat ? 3001:3000; + Object data=flat ? "添加成功":"添加失败!"; + return new R(code, data); + } + //修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flat= bookService.updateBook(book); + Integer code=flat? 4001:4000; + Object data=flat ? "修改成功":"修改失败!"; + return new R(code,data); + } + ////根据id查询 + @GetMapping("/{id}") + public R getById(@PathVariable Integer id){ + Book book=bookService.getById(id); + Integer code=book !=null? 2001:2000; + Object data=book !=null? book:"查询数据失败,可能没有数据!"; + return new R(code,data); + } + //根据id删除 + @DeleteMapping("/{id}") + public R delById(@PathVariable Integer id){ + Boolean flat=bookService.delById(id); + Integer code=flat? 5001:5000; + Object data=flat ? "删除成功":"删除失败!"; + return new R(code,data); + } + + + +} + +``` + +```java +package com.mdd.controller; + +public class Code { + public static final Integer GET_OK =2001; + public static final Integer POST_OK =3001; + public static final Integer PUT_OK =4001; + public static final Integer DELETE_OK =5001; + + + public static final Integer GET_ERR =2000; + public static final Integer POST_ERR =3000; + public static final Integer PUT_ERR =4000; + public static final Integer DELETE_ERR =5000; + + +} + +``` + +```java +package com.mdd.controller; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R(Integer code, Object data) { + this.code = code; + this.data = data; + } + + 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 getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} + +``` + +```java +package com.mdd.domain; + +public class Book { + private Integer id; + private String bookName; + private String author; + private String publisher; + + public Book() { + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.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; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", bookName='" + bookName + '\'' + + ", author='" + author + '\'' + + ", publisher='" + publisher + '\'' + + '}'; + } +} + +``` + +```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 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})") + Integer addBook(Book book); + + @Update("update tb_book set book_name=#{bookName}, author=#{author}, publisher=#{publisher} where id=#{id}") + Integer updateBook(Book book); + + @Select("select id, book_name bookName, author, publisher from tb_book where id=#{id}") + Book getById(Integer id); + + @Delete("delete from tb_book where id=#{id}") + Integer delById(Integer id); +} + +``` + +```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 boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + + public Boolean delById(Integer id) { + return bookMapper.delById(id)>0; + } +} + +``` + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + boolean addBook(Book book); + + Boolean updateBook(Book book); + + Book getById(Integer id); + + Boolean delById(Integer id); +} + +``` + diff --git "a/37 \351\227\253\351\233\252\350\216\262/20240112 \345\244\215\344\271\240\347\224\250\346\210\267\350\241\250.md" "b/37 \351\227\253\351\233\252\350\216\262/20240112 \345\244\215\344\271\240\347\224\250\346\210\267\350\241\250.md" new file mode 100644 index 0000000..950efea --- /dev/null +++ "b/37 \351\227\253\351\233\252\350\216\262/20240112 \345\244\215\344\271\240\347\224\250\346\210\267\350\241\250.md" @@ -0,0 +1,400 @@ +```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:///ssm"); + ds.setUsername("root"); + ds.setPassword("1234"); + return ds; + } +} + +``` + +```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.setDataSource(dataSource); + ssfb.setTypeAliasesPackage("com.mdd.domain"); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + } +} + +``` + +```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({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig { +} + +``` + +```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.controller") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.mdd.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { + protected Class[] getRootConfigClasses() { + return new Class[]{SpringConfig.class}; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{SpringMvcConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} + +``` + +```java +package com.mdd.controller; + +public class Code { + public static final Integer GET_OK=1000; + public static final Integer POST_OK=2000; + public static final Integer PUT_OK=3000; + public static final Integer DEL_OK=4000; + + public static final Integer GET_ERR=1001; + public static final Integer POST_ERR=2001; + public static final Integer PUT_ERR=3001; + public static final Integer DEL_ERR=4001; + + + +} + +``` + +```java +package com.mdd.controller; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + public R(Integer code, Object data) { + this.code = code; + this.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; + } +} + +``` + +```java +package com.mdd.controller; + +import com.mdd.domain.User; +import com.mdd.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/users") +public class UserController { + + @Autowired + private UserService userService; + + //查所有 + @GetMapping + public R getAll(){ + List userList= userService.getAll(); + Integer code=userList!=null? 1000:1001; + Object data=userList!=null? userList:"查询失败,可能没有数据"; + return new R(code,data); + } + + @PostMapping + public R addUser(@RequestBody User user){ + Boolean flag= userService.addUser(user); + Integer code=flag? 2000:2001; + Object data=flag? "添加成功":"添加失败"; + return new R(code,data); + } + + @PutMapping + public R updateUser(@RequestBody User user){ + Boolean flag= userService.updateUser(user); + Integer code=flag? 3000:3001; + Object data=flag? "修改成功":"修改失败"; + return new R(code,data); + } + + @GetMapping("/{id}") + public R getById(@PathVariable Integer id){ + User user= userService.getById(id); + Integer code=user!=null? 1000:1001; + Object data=user!=null? user:"查询失败,可能没有数据"; + return new R(code,data); + } + + @DeleteMapping("/{id}") + public R delById(@PathVariable Integer id){ + Boolean flag= userService.delById(id); + Integer code=flag? 4000:4001; + Object data=flag? "删除成功":"删除失败"; + return new R(code,data); + } + + + +} + +``` + +```java +package com.mdd.domain; + +public class User { + private Integer id; + private String username; + private Integer age; + private String gender; + + public User(Integer id, String username, Integer age, String gender) { + this.id = id; + this.username = username; + this.age = age; + this.gender = gender; + } + + public User() { + } + + 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 Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", age=" + age + + ", gender='" + gender + '\'' + + '}'; + } +} + +``` + +```java +package com.mdd.mapper; + +import com.mdd.domain.User; +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 UserMapper { + @Select("select id, username, age, gender from ssm.tb_user order by id desc;") + List getAll(); + + @Insert("insert into ssm.tb_user(username, age, gender) value (#{username}, #{age}, #{gender})") + Integer addUser(User user); + + @Update("update ssm.tb_user set username=#{username}, age=#{age}, gender=#{gender} where id=#{id}") + Integer updateUser(User user); + + @Select("select id, username, age, gender from ssm.tb_user where id=#{id};") + User getById(Integer id); + + @Delete("delete from ssm.tb_user where id=#{id}") + Integer delById(Integer id); +} + +``` + +```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; + public List getAll() { + return userMapper.getAll(); + } + + public Boolean addUser(User user) { + return userMapper.addUser(user)>0; + } + + public Boolean updateUser(User user) { + return userMapper.updateUser(user)>0; + } + + public User getById(Integer id) { + return userMapper.getById(id); + } + + public Boolean delById(Integer id) { + return userMapper.delById(id)>0; + } + + +} + +``` + +```java +package com.mdd.service; + +import com.mdd.domain.User; + +import java.util.List; + +public interface UserService { + List getAll(); + + Boolean addUser(User user); + + Boolean updateUser(User user); + + User getById(Integer id); + + Boolean delById(Integer id); +} + +``` + -- Gitee