From ec21e5e5f826c44282bda1f077766eaecaed1ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=85=E5=BF=A0=E9=92=A6?= <11785125+mei-zhongqin@user.noreply.gitee.com> Date: Tue, 16 Jan 2024 09:06:31 +0000 Subject: [PATCH] =?UTF-8?q?56=20=E6=A2=85=E5=BF=A0=E9=92=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 梅忠钦 <11785125+mei-zhongqin@user.noreply.gitee.com> --- .../2023.01.05\344\275\234\344\270\232.md" | 356 ++++++++++++++++++ 1 file changed, 356 insertions(+) create mode 100644 "\346\242\205\345\277\240\351\222\246/2023.01.05\344\275\234\344\270\232.md" diff --git "a/\346\242\205\345\277\240\351\222\246/2023.01.05\344\275\234\344\270\232.md" "b/\346\242\205\345\277\240\351\222\246/2023.01.05\344\275\234\344\270\232.md" new file mode 100644 index 0000000..e443065 --- /dev/null +++ "b/\346\242\205\345\277\240\351\222\246/2023.01.05\344\275\234\344\270\232.md" @@ -0,0 +1,356 @@ +```java +ackage com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@ComponentScan({"com.mdd.service","com.mdd.mapper"}) +@PropertySource({"classpath:JDBC.properties"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +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 bean = new SqlSessionFactoryBean(); + bean.setDataSource(dataSource); + bean.setTypeAliasesPackage("com.mdd.pojo"); + return bean; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer mapper = new MapperScannerConfigurer(); + mapper.setBasePackage("com.mdd.mapper"); + return mapper; + } +} +package com.mdd.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + @Value("${jdbc.driver}") + private String driver; + @Value("${jdbc.localUrl}") + private String url; + @Value("${jdbc.username}") + private String userName; + @Value("${jdbc.classroomPassword}") + private String password1; + @Value("${jdbc.homePassword}") + private String password2; + @Bean + public DataSource dataSource(){ + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setUsername(userName); + dataSource.setPassword(password2); + dataSource.setUrl(url); + dataSource.setDriverClassName(driver); + return dataSource; + } +} +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@ComponentScan("com.mdd.controller") +@EnableWebMvc +public class SpringMvcConfig { +} +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[]{"/"}; + } +} +//实体类 +package com.mdd.pojo; + +public class Book { + private Integer id; + private String bookName; + private String author; + private String publisher; + + 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; + } +} +package com.mdd.pojo; + +public class Result { + private Integer code; + private String msg; + private Object data; + + public static Result success(Integer code,Object data){ + Result result =new Result(); + result.setCode(code); + result.setData(data); + return result; + } + public static Result success(Integer code,String msg){ + Result result = new Result(); + result.setCode(code); + result.setMsg(msg); + return result; + } + public static Result failure(Integer code,String msg){ + Result result =new Result(); + result.setCode(code); + result.setMsg(msg); + return result; + } + + public String toString() { + return "Result{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + '}'; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Result() { + } + + public Result(Integer code, String msg, Object data) { + this.code = code; + this.msg = msg; + this.data = data; + } +} +//表现层 +package com.mdd.controller; + +import com.mdd.pojo.Book; +import com.mdd.pojo.Result; +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 Result books(){ + List books = bookService.getAllBook(); + if (books!=null) return Result.success(200,books); + else return Result.failure(404,"查询失败,无数据"); + } + @GetMapping("/{id}") + public Result book(@PathVariable Integer id){ + Book book = bookService.getBook(id); + if (book!=null){ + return Result.success(200,book); + }else { + return Result.failure(404,"无法查询到和该ID对应的数据"); + } + } + @PostMapping + public Result update(@RequestBody Book book){ + Integer i = bookService.updateBook(book); + if (i>0) return Result.success(200,"更新成功"); + else return Result.failure(404,"更新失败"); + } + @PutMapping + public Result addBook(@RequestBody Book book){ + Integer i = bookService.addBook(book); + if (i>0) return Result.success(200,"新增成功"); + else return Result.failure(404,"新增失败"); + } + @DeleteMapping("/{id}") + public Result delBook(@PathVariable Integer id){ + Integer i = bookService.delBook(id); + if (i>0) return Result.success(200,"删除成功"); + else return Result.failure(404,"删除失败"); + } +} +//业务层 +package com.mdd.service; + +import com.mdd.pojo.Book; + +import java.util.List; + +public interface BookService { + List getAllBook(); + + Book getBook(Integer id); + + Integer updateBook(Book book); + + Integer addBook(Book book); + + Integer delBook(Integer id); +} +package com.mdd.service.Impl; + +import com.mdd.mapper.BookMapper; +import com.mdd.pojo.Book; +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 getAllBook() { + List books = bookMapper.getAllBook(); + return books; + } + + public Book getBook(Integer id) { + return bookMapper.getBook(id); + } + + public Integer updateBook(Book book) { + return bookMapper.updateBook(book); + } + + public Integer addBook(Book book) { + return bookMapper.addBook(book); + } + + public Integer delBook(Integer id) { + return bookMapper.delBook(id); + } +} +//mapper映射接口(数据层) +package com.mdd.mapper; + +import com.mdd.pojo.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); + + @Update("update tb_book set book_name = #{bookName},author=#{author},publisher=#{publisher} where id=#{id}") + Integer updateBook(Book book); + + @Select("select id, book_name as bookName, author, publisher from tb_book order by id desc;") + List getAllBook(); + + @Select("select id, book_name as bookName, author, publisher from tb_book where id = #{id};") + Book getBook(Integer id); + + @Delete("delete from tb_book where id= #{id};") + Integer delBook(Integer id); +} +``` + -- Gitee