From fb7d173614209c4a264ebd9695f1e64d6963002a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B5=B7=E7=91=9E?= <3148024859@qq.com> Date: Tue, 9 Jan 2024 00:20:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20240105 \345\233\276\344\271\246.md" | 508 ++++++++++++++++++ 1 file changed, 508 insertions(+) create mode 100644 "50 \345\274\240\350\265\267\347\221\236/20240105 \345\233\276\344\271\246.md" diff --git "a/50 \345\274\240\350\265\267\347\221\236/20240105 \345\233\276\344\271\246.md" "b/50 \345\274\240\350\265\267\347\221\236/20240105 \345\233\276\344\271\246.md" new file mode 100644 index 0000000..2330b48 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20240105 \345\233\276\344\271\246.md" @@ -0,0 +1,508 @@ +### Config + + + +```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; + } +} + +``` + +```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({JdbcConfig.class, MyBatisConfig.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; + +@Configuration +@ComponentScan("com.mdd") +@EnableWebMvc +public class SpringMvcConfig { +} + +``` + +```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.domain.R; +import com.mdd.service.BookService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/books") +public class BookController { + @Autowired + private BookService bookService; + + @GetMapping + public R getAll() { + List books = bookService.getAll(); + return R.success(200, books); + } + + @PostMapping + public Book addBook(@RequestBody Book book) { + bookService.addBook(book); + return book; + } + + @PutMapping + public Book updateBook(@RequestBody Book book) { + bookService.updateBook(book); + return book; + } + + @DeleteMapping("/{id}") + public R deleteBookById(@PathVariable Integer id) { + Integer i = bookService.deleteBook(id); + if (i > 0) { + return R.success(200, "删除成功"); + }else{ + return R.failure(404, "删除失败"); + } + } + + @GetMapping("/{id}") + public Book getBook(@PathVariable Integer id) { + return bookService.getBook(id); + } + +} + +``` + +```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() { + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.publisher = publisher; + } +} + +``` + +```java +package com.mdd.domain; + +public class R { + private Integer code; // 200 404 + private String msg;// 提示信息 + private Object data;// 返回的数据 + + public R(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public R() { + } + + + public static R success(Integer code,Object data){ + R r = new R(); + r.setData(data); + r.setCode(code); + return r; + } + + public static R success(Integer code,String msg){ + R r = new R(); + r.setMsg(msg); + r.setCode(code); + return r; + } + + public static R failure(Integer code,String msg){ + R r = new R(); + r.setMsg(msg); + r.setCode(code); + return r; + } + + + @Override + public String toString() { + return "R{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } +} + +``` + +```java +package com.mdd.mapper; + +import com.mdd.domain.Book; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +import java.util.List; + +public interface BookMapper { + @Select("select id, book_name as bookName, author, publisher from tb_book order by id desc;") + List getAll(); + + @Insert("insert into tb_book (book_name, author, publisher) VALUES (#{bookName}, #{author}, #{publisher})") + void addBook(Book book); + + @Update("update tb_book set book_name = #{bookName},author=#{author},publisher=#{publisher} where id=#{id} ;") + void updateBook(Book book); + + @Delete("delete from tb_book where id=#{id};") + Integer deleteBook(Integer id); + + @Select("select id, book_name as bookName, author, publisher from tb_book where id = #{id}") + Book getBook(Integer id); +} + +``` + +```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; + @Override + public List getAll() { + return bookMapper.getAll(); + } + + public void addBook(Book book) { + bookMapper.addBook(book); + } + + public void updateBook(Book book) { + bookMapper.updateBook(book); + } + + public Integer deleteBook(Integer id) { + return bookMapper.deleteBook(id); + } + + public Book getBook(Integer id) { + return bookMapper.getBook(id); + } +} + +``` + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getAll(); + + void addBook(Book book); + + void updateBook(Book book); + + Integer deleteBook(Integer id); + + Book getBook(Integer id); +} + +``` + +```java + + + + Archetype Created Web Application + + +``` + +```java + + +

Hello World!

+ + + +``` + +```java + + 4.0.0 + com.mdd + ssm + war + 1.0-SNAPSHOT + + + + + org.springframework + spring-webmvc + 5.2.25.RELEASE + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + com.fasterxml.jackson.core + jackson-databind + 2.11.3 + + + + + org.springframework + spring-jdbc + 5.2.25.RELEASE + + + org.springframework + spring-test + 5.2.25.RELEASE + + + + org.mybatis + mybatis + 3.5.13 + + + + org.mybatis + mybatis-spring + 2.0.5 + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + junit + junit + 4.13.2 + test + + + + com.alibaba + druid + 1.1.23 + + + + + + + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 80 + / + + utf-8 + + + + + + + +``` + -- Gitee