diff --git "a/29 \350\267\257\347\216\262/20240109 mvc08.md" "b/29 \350\267\257\347\216\262/20240109 mvc08.md" new file mode 100644 index 0000000000000000000000000000000000000000..70e5d1d8ede0d5f0c15cf20ac4e1d9f3c9f255f3 --- /dev/null +++ "b/29 \350\267\257\347\216\262/20240109 mvc08.md" @@ -0,0 +1,445 @@ +com.mdd.domain.Book + +```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() { + } +} + +``` + +com.mdd.config.JdbcConfig + +```java +package com.mdd.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///ssm"); + ds.setUsername("root"); + ds.setPassword("root"); + return ds; + } +} +``` + +com.mdd.config.MybatisConfig + +```java +package com.mdd.config; + +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.mapper.MapperScannerConfigurer; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class MybatisConfig { + + //连接数据库,实体类 + @Bean + public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfn = new SqlSessionFactoryBean(); + ssfn.setTypeAliasesPackage("com.mdd.domain"); + ssfn.setDataSource(dataSource); + return ssfn; + } + + + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + } + +``` + +com.mdd.config.SpringConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@ComponentScan({"com.mdd.mapper","com.mdd.service"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +``` + +com.mdd.config.SpringMvcConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +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 { + + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } +} +``` + +com.mdd.config.WebConfig + +```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[]{"/"}; + } +} + +``` + +com.mdd.controller.Code + +```java +package com.mdd.controller; + +public class Code { + //增加,删除,修改,查询四种操作,code尾数为1则是成功,尾数为0则是失败 + + public static final Integer GET_OK = 2001; + public static final Integer POTS_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; + + +} +``` + +com.mdd.controller.R + +```java +package com.mdd.controller; + +public class R { + private Integer id; + private String msg; + private Object data;//一定要写成object类型 + + public R() { + } + + public R(Integer id, String msg) { + this.id = id; + this.msg = msg; + } + + public R(Integer id, Object data) { + this.id = id; + this.data = data; + } + + public R(Integer id, String msg, Object data) { + this.id = id; + this.msg = msg; + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + 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; + } + + @Override + public String toString() { + return "R{" + + "id=" + id + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } +} + +``` + +com.mdd.controller.BookController + +```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 List getBooks() { + List bookList = bookService.getBooks(); // 查询结果可能是空,也可能有数据 + Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR; + Object data = bookList != null ? bookList : "查询失败!"; + return bookList; + } + + //增加 + @PostMapping + public R addBook(@RequestBody Book book){ + Boolean falg = bookService.addBook(book); + if (falg) { + return new R(Code.POST_ERR,"添加成功!"); + }else{ + return new R(Code.POST_ERR,"添加失败!"); + } + } + + //修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag ? Code.PUT_OK :Code.PUT_ERR; + String msg = flag ? "修改成功!":"修改失败!"; + return new R(code,msg); + } + + + + //根据id删除 + @DeleteMapping("/{id}") + public R deleteBook(@PathVariable Integer id){ + Boolean flag = bookService.deleteBook(id); + Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR; + String msg = flag ? "删除成功!":"删除失败!"; + return new R(code,msg); + } + + + + + //按照id查询 + + @GetMapping("/{id}") + public R getBookById(@PathVariable Integer id){ + Book book = bookService.getBookById(id); + Integer code = book !=null ? Code.GET_OK:Code.GET_ERR; + Object data = book != null ? book:"查询失败!"; + return new R(code,data); + } + + + +} +``` + +com.mdd.service.BookService + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + Boolean addBook(Book book); + + List getBooks(); + + Boolean updateBook(Book book); + + Book getBookById(Integer id); + + Boolean deleteBook(Integer id); +} +``` + +com.mdd.service.Impl.BookServiceImpl + +```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 Boolean addBook(Book book) { + return bookMapper.addBook(book)>0; + } + + public List getBooks() { + return bookMapper.getBook(); + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } + + public Book getBookById(Integer id) { + return bookMapper.getBookById(id); + } + + public Boolean deleteBook(Integer id) { + return bookMapper.deleteBook(id); + } +} +``` + +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 { + @Insert("insert into tb_book (book_name, author, publisher) values (#{bookName}, #{author}, #{publisher}) ") + Integer addBook(Book book); + + @Select("select id, book_name bookName, author, publisher from tb_book") + List getBook(); + + @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 getBookById(Integer id); + + @Delete("delete from tb_book where id = #{id}") + Boolean deleteBook(Integer id); +} + +``` + diff --git "a/29 \350\267\257\347\216\262/20240110 mvc09.md" "b/29 \350\267\257\347\216\262/20240110 mvc09.md" new file mode 100644 index 0000000000000000000000000000000000000000..83d8c8faa4f48a04143295fd2b18cb12223f21ee --- /dev/null +++ "b/29 \350\267\257\347\216\262/20240110 mvc09.md" @@ -0,0 +1,549 @@ +springmvc完整版(开发) + +pom.xml + +```xml + + 4.0.0 + com.mdd + ssm + war + 1.0-SNAPSHOT + + + + UTF-8 + UTF-8 + + + + + + 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/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/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.controller") +@EnableWebMvc +public class SpringMvcConfig implements WebMvcConfigurer { + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico"); + } +} +``` + +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/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:///ssm"); + ds.setUsername("root"); + ds.setPassword("root");//记得看密码是不是一样的 + 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.setDataSource(dataSource); + ssfb.setTypeAliasesPackage("com.mdd.domain"); + return ssfb; + } + + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + } + +} +``` + +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/controller/R.java + +```java +package com.mdd.controller; + +public class R { + private Integer code; + private String msg; + private Object data; + + public R() { + } + + 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 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; + } + + @Override + public String toString() { + return "R{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } +} + +``` + +src/main/java/com/mdd/controller/Code.java + +```java +package com.mdd.controller; + +public class Code { + + public static final Integer GET_OK = 2000; + public static final Integer POST_OK = 3000; + public static final Integer PUT_OK = 4000; + public static final Integer DELETE_OK = 5000; + + public static final Integer GET_ERR = 2001; + public static final Integer POST_ERR = 3001; + public static final Integer PUT_ERR = 4001; + public static final Integer DELETE_ERR = 5001; + + +} +``` + +src/main/java/com/mdd/controller/BookController.java + +```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 List getBooks(){ + List bookList = bookService.getBooks(); + Integer code = bookList != null ? Code.GET_OK:Code.GET_ERR; + Object data = bookList != null ? bookList:"查询失败!"; + return bookList; + } + + //添加 + @PostMapping + public R postBook(@RequestBody Book book){ + Boolean flag = bookService.postBook(book); + if (flag) { + return new R(Code.POST_OK,"添加成功!"); + }else { + return new R(Code.POST_ERR,"添加失败!"); + } + } + + //修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag ? Code.PUT_OK:Code.PUT_ERR; + String data = flag ? "修改成功!":"修改失败!"; + return new R(code,data); + } + + //删除 + @DeleteMapping("/{id}") + public R deleteBook(@PathVariable Integer id){ + Boolean flag = bookService.DeleteBook(id); + Integer code = flag ? Code.DELETE_OK:Code.DELETE_ERR; + String data = flag ? "删除成功!":"删除失败"; + return new R(code,data); + } + + //按照id查询 + @GetMapping("/{id}") + public R getId(@PathVariable Integer id){ + Book book = bookService.getId(id); + Integer code = book != null ? Code.GET_OK:Code.GET_ERR; + Object data = book != null ? book:"根据id查询失败!"; + return new R(code,data); + + } + +} + +``` + +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 getBooks(); + + Boolean postBook(Book book); + + Boolean updateBook(Book book); + + Boolean DeleteBook(Integer id); + + Book getId(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 getBooks() { + return bookMapper.getBooks(); + } + + public Boolean postBook(Book book) { + return bookMapper.postBook(book)>0; + } + + public Boolean updateBook(Book book) { + return bookMapper.updateBook(book)>0; + } + + public Boolean DeleteBook(Integer id) { + return bookMapper.deteleBook(id)>0; + } + + public Book getId(Integer id) { + return bookMapper.getId(id); + } +} +``` + +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 bookName, author, publisher from tb_book") + List getBooks(); + + @Insert("insert into tb_book (book_name, author, publisher) VALUES (#{bookName}, #{author}, #{publisher})") + Integer postBook(Book book); + + @Update("update tb_book set book_name = #{bookName},author = #{author},publisher = #{publisher} where id = #{id}") + Integer updateBook(Book book); + + @Delete("delete from tb_book where id = #{id}") + Integer deteleBook(Integer id); + + @Select("select id, book_name bookName, author, publisher from tb_book where id=#{id}") + Book getId(Integer id); +} +``` + +别忘记在webapp李添加index.html文件 \ No newline at end of file