From ac3763c2de5477bb0ffb8d1e5269c70e447d72b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8F=91=E6=A7=90?= <11785178+wmm1004lsp@user.noreply.gitee.com> Date: Fri, 12 Jan 2024 09:24:07 +0000 Subject: [PATCH] =?UTF-8?q?=E7=8E=8B=E5=8F=91=E6=A7=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王发槐 <11785178+wmm1004lsp@user.noreply.gitee.com> --- .../2024.01.08.MD" | 392 ++++++++++++++++++ .../2024.01.09.md" | 392 ++++++++++++++++++ .../2024.01.11.md" | 392 ++++++++++++++++++ .../2024.01.12.md" | 392 ++++++++++++++++++ 4 files changed, 1568 insertions(+) create mode 100644 "\347\216\213\345\217\221\346\247\220/2024.01.08.MD" create mode 100644 "\347\216\213\345\217\221\346\247\220/2024.01.09.md" create mode 100644 "\347\216\213\345\217\221\346\247\220/2024.01.11.md" create mode 100644 "\347\216\213\345\217\221\346\247\220/2024.01.12.md" diff --git "a/\347\216\213\345\217\221\346\247\220/2024.01.08.MD" "b/\347\216\213\345\217\221\346\247\220/2024.01.08.MD" new file mode 100644 index 0000000..053f115 --- /dev/null +++ "b/\347\216\213\345\217\221\346\247\220/2024.01.08.MD" @@ -0,0 +1,392 @@ +```java +package com.pxx.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.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.pxx.controller") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +```java +package com.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan({"com.pxx.service","com.pxx.mapper"}) +@Import({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig implements WebMvcConfigurer { + + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.pxx.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.pxx.domain"); + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.pxx.mapper"); + return msc; + } +} +``` + +```java +package com.pxx.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("123456"); + return ds; + } +} +``` + +```java +package com.pxx.controller; + +import com.pxx.domain.Book; +import com.pxx.domain.R; +import com.pxx.service.BookService; +import org.apache.ibatis.annotations.Delete; +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); + } + // 查询单个 + @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); + } + // 增加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flag = bookService.addBook(book); + Integer code = flag != null ? 3001 : 3000; + Object data = flag != null ? "添加成功" : "添加失败"; + return new R(code,data); + } + + // 删除 + @DeleteMapping("/{id}") + public R deleById(@PathVariable Integer id){ + Boolean flag =bookService.deleById(id); + Integer code = flag !=null ?4001:4000; + Object data = flag != null ?"删除成功" : "删除失败"; + return new R(code,data); + } + // 更新 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag !=null ? 5001:5000; + Object data =flag!=null?"更新成功":"更新失败"; + return new R(code,data); + } + +} +``` + +```java +package com.pxx.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.pxx.domain; + + +public class Code { + + public static final Integer GET_OK = 2001; + public static final Integer POST_OK = 3001; + public static final Integer DELETE_OK = 4001; + public static final Integer PUT_OK = 5001; + + public static final Integer GET_ERR = 2000; + public static final Integer POST_ERR = 3000; + public static final Integer DELETE_ERR = 4000; + public static final Integer PUT_ERR = 5000; +} + +``` + +```java +package com.pxx.domain; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + 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 R (Integer code ,String msg,Object data){ + this.code=code; + this.msg=msg; + 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.pxx.mapper; + +import com.pxx.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 { + @Insert("insert into ssm.tb_book (book_name, author, publisher) VALUES (#{bookName},#{author},#{publisher})") + Integer addBook(Book book) ; + + @Delete("delete from ssm.tb_book where id =#{id}") + Integer deleById(Integer id) ; + + @Update("update ssm.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 ssm.tb_book order by id desc ") + List geAll(); + + + @Select("select id, book_name bookName, author, publisher from ssm.tb_book where id = #{id}") + Book getById(Integer id); +} +``` + +```java +package com.pxx.service; + +import com.pxx.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + Book getById(Integer id); + + Boolean addBook(Book book); + + Boolean deleById(Integer id); + + Boolean updateBook(Book book); +} + +``` + +```java +package com.pxx.service.impl; + +import com.pxx.domain.Book; +import com.pxx.mapper.BookMapper; +import com.pxx.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.geAll() ; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + public Boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean deleById(Integer id) { + return bookMapper.deleById(id)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } +} + +``` + diff --git "a/\347\216\213\345\217\221\346\247\220/2024.01.09.md" "b/\347\216\213\345\217\221\346\247\220/2024.01.09.md" new file mode 100644 index 0000000..053f115 --- /dev/null +++ "b/\347\216\213\345\217\221\346\247\220/2024.01.09.md" @@ -0,0 +1,392 @@ +```java +package com.pxx.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.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.pxx.controller") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +```java +package com.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan({"com.pxx.service","com.pxx.mapper"}) +@Import({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig implements WebMvcConfigurer { + + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.pxx.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.pxx.domain"); + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.pxx.mapper"); + return msc; + } +} +``` + +```java +package com.pxx.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("123456"); + return ds; + } +} +``` + +```java +package com.pxx.controller; + +import com.pxx.domain.Book; +import com.pxx.domain.R; +import com.pxx.service.BookService; +import org.apache.ibatis.annotations.Delete; +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); + } + // 查询单个 + @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); + } + // 增加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flag = bookService.addBook(book); + Integer code = flag != null ? 3001 : 3000; + Object data = flag != null ? "添加成功" : "添加失败"; + return new R(code,data); + } + + // 删除 + @DeleteMapping("/{id}") + public R deleById(@PathVariable Integer id){ + Boolean flag =bookService.deleById(id); + Integer code = flag !=null ?4001:4000; + Object data = flag != null ?"删除成功" : "删除失败"; + return new R(code,data); + } + // 更新 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag !=null ? 5001:5000; + Object data =flag!=null?"更新成功":"更新失败"; + return new R(code,data); + } + +} +``` + +```java +package com.pxx.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.pxx.domain; + + +public class Code { + + public static final Integer GET_OK = 2001; + public static final Integer POST_OK = 3001; + public static final Integer DELETE_OK = 4001; + public static final Integer PUT_OK = 5001; + + public static final Integer GET_ERR = 2000; + public static final Integer POST_ERR = 3000; + public static final Integer DELETE_ERR = 4000; + public static final Integer PUT_ERR = 5000; +} + +``` + +```java +package com.pxx.domain; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + 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 R (Integer code ,String msg,Object data){ + this.code=code; + this.msg=msg; + 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.pxx.mapper; + +import com.pxx.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 { + @Insert("insert into ssm.tb_book (book_name, author, publisher) VALUES (#{bookName},#{author},#{publisher})") + Integer addBook(Book book) ; + + @Delete("delete from ssm.tb_book where id =#{id}") + Integer deleById(Integer id) ; + + @Update("update ssm.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 ssm.tb_book order by id desc ") + List geAll(); + + + @Select("select id, book_name bookName, author, publisher from ssm.tb_book where id = #{id}") + Book getById(Integer id); +} +``` + +```java +package com.pxx.service; + +import com.pxx.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + Book getById(Integer id); + + Boolean addBook(Book book); + + Boolean deleById(Integer id); + + Boolean updateBook(Book book); +} + +``` + +```java +package com.pxx.service.impl; + +import com.pxx.domain.Book; +import com.pxx.mapper.BookMapper; +import com.pxx.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.geAll() ; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + public Boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean deleById(Integer id) { + return bookMapper.deleById(id)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } +} + +``` + diff --git "a/\347\216\213\345\217\221\346\247\220/2024.01.11.md" "b/\347\216\213\345\217\221\346\247\220/2024.01.11.md" new file mode 100644 index 0000000..053f115 --- /dev/null +++ "b/\347\216\213\345\217\221\346\247\220/2024.01.11.md" @@ -0,0 +1,392 @@ +```java +package com.pxx.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.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.pxx.controller") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +```java +package com.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan({"com.pxx.service","com.pxx.mapper"}) +@Import({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig implements WebMvcConfigurer { + + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.pxx.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.pxx.domain"); + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.pxx.mapper"); + return msc; + } +} +``` + +```java +package com.pxx.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("123456"); + return ds; + } +} +``` + +```java +package com.pxx.controller; + +import com.pxx.domain.Book; +import com.pxx.domain.R; +import com.pxx.service.BookService; +import org.apache.ibatis.annotations.Delete; +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); + } + // 查询单个 + @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); + } + // 增加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flag = bookService.addBook(book); + Integer code = flag != null ? 3001 : 3000; + Object data = flag != null ? "添加成功" : "添加失败"; + return new R(code,data); + } + + // 删除 + @DeleteMapping("/{id}") + public R deleById(@PathVariable Integer id){ + Boolean flag =bookService.deleById(id); + Integer code = flag !=null ?4001:4000; + Object data = flag != null ?"删除成功" : "删除失败"; + return new R(code,data); + } + // 更新 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag !=null ? 5001:5000; + Object data =flag!=null?"更新成功":"更新失败"; + return new R(code,data); + } + +} +``` + +```java +package com.pxx.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.pxx.domain; + + +public class Code { + + public static final Integer GET_OK = 2001; + public static final Integer POST_OK = 3001; + public static final Integer DELETE_OK = 4001; + public static final Integer PUT_OK = 5001; + + public static final Integer GET_ERR = 2000; + public static final Integer POST_ERR = 3000; + public static final Integer DELETE_ERR = 4000; + public static final Integer PUT_ERR = 5000; +} + +``` + +```java +package com.pxx.domain; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + 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 R (Integer code ,String msg,Object data){ + this.code=code; + this.msg=msg; + 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.pxx.mapper; + +import com.pxx.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 { + @Insert("insert into ssm.tb_book (book_name, author, publisher) VALUES (#{bookName},#{author},#{publisher})") + Integer addBook(Book book) ; + + @Delete("delete from ssm.tb_book where id =#{id}") + Integer deleById(Integer id) ; + + @Update("update ssm.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 ssm.tb_book order by id desc ") + List geAll(); + + + @Select("select id, book_name bookName, author, publisher from ssm.tb_book where id = #{id}") + Book getById(Integer id); +} +``` + +```java +package com.pxx.service; + +import com.pxx.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + Book getById(Integer id); + + Boolean addBook(Book book); + + Boolean deleById(Integer id); + + Boolean updateBook(Book book); +} + +``` + +```java +package com.pxx.service.impl; + +import com.pxx.domain.Book; +import com.pxx.mapper.BookMapper; +import com.pxx.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.geAll() ; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + public Boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean deleById(Integer id) { + return bookMapper.deleById(id)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } +} + +``` + diff --git "a/\347\216\213\345\217\221\346\247\220/2024.01.12.md" "b/\347\216\213\345\217\221\346\247\220/2024.01.12.md" new file mode 100644 index 0000000..053f115 --- /dev/null +++ "b/\347\216\213\345\217\221\346\247\220/2024.01.12.md" @@ -0,0 +1,392 @@ +```java +package com.pxx.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.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.pxx.controller") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +```java +package com.pxx.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@ComponentScan({"com.pxx.service","com.pxx.mapper"}) +@Import({MybatisConfig.class, JdbcConfig.class}) +public class SpringConfig implements WebMvcConfigurer { + + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} + +``` + +```java +package com.pxx.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.pxx.domain"); + ssfb.setDataSource(dataSource); + return ssfb; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.pxx.mapper"); + return msc; + } +} +``` + +```java +package com.pxx.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("123456"); + return ds; + } +} +``` + +```java +package com.pxx.controller; + +import com.pxx.domain.Book; +import com.pxx.domain.R; +import com.pxx.service.BookService; +import org.apache.ibatis.annotations.Delete; +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); + } + // 查询单个 + @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); + } + // 增加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean flag = bookService.addBook(book); + Integer code = flag != null ? 3001 : 3000; + Object data = flag != null ? "添加成功" : "添加失败"; + return new R(code,data); + } + + // 删除 + @DeleteMapping("/{id}") + public R deleById(@PathVariable Integer id){ + Boolean flag =bookService.deleById(id); + Integer code = flag !=null ?4001:4000; + Object data = flag != null ?"删除成功" : "删除失败"; + return new R(code,data); + } + // 更新 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag !=null ? 5001:5000; + Object data =flag!=null?"更新成功":"更新失败"; + return new R(code,data); + } + +} +``` + +```java +package com.pxx.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.pxx.domain; + + +public class Code { + + public static final Integer GET_OK = 2001; + public static final Integer POST_OK = 3001; + public static final Integer DELETE_OK = 4001; + public static final Integer PUT_OK = 5001; + + public static final Integer GET_ERR = 2000; + public static final Integer POST_ERR = 3000; + public static final Integer DELETE_ERR = 4000; + public static final Integer PUT_ERR = 5000; +} + +``` + +```java +package com.pxx.domain; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + 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 R (Integer code ,String msg,Object data){ + this.code=code; + this.msg=msg; + 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.pxx.mapper; + +import com.pxx.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 { + @Insert("insert into ssm.tb_book (book_name, author, publisher) VALUES (#{bookName},#{author},#{publisher})") + Integer addBook(Book book) ; + + @Delete("delete from ssm.tb_book where id =#{id}") + Integer deleById(Integer id) ; + + @Update("update ssm.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 ssm.tb_book order by id desc ") + List geAll(); + + + @Select("select id, book_name bookName, author, publisher from ssm.tb_book where id = #{id}") + Book getById(Integer id); +} +``` + +```java +package com.pxx.service; + +import com.pxx.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + Book getById(Integer id); + + Boolean addBook(Book book); + + Boolean deleById(Integer id); + + Boolean updateBook(Book book); +} + +``` + +```java +package com.pxx.service.impl; + +import com.pxx.domain.Book; +import com.pxx.mapper.BookMapper; +import com.pxx.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.geAll() ; + } + + public Book getById(Integer id) { + return bookMapper.getById(id); + } + public Boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public Boolean deleById(Integer id) { + return bookMapper.deleById(id)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } +} + +``` + -- Gitee