From 53c617c68adbfda2dff5f2ef91cd3b5c44524261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <2431466589@qq.com> Date: Wed, 10 Jan 2024 12:02:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\345\220\210\347\254\224\350\256\260.md" | 582 ++++++++++++++++++ 1 file changed, 582 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/20240109 SSM\346\225\264\345\220\210\347\254\224\350\256\260.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/20240109 SSM\346\225\264\345\220\210\347\254\224\350\256\260.md" "b/37 \346\217\255\351\230\263\344\270\275/20240109 SSM\346\225\264\345\220\210\347\254\224\350\256\260.md" new file mode 100644 index 0000000..e6f996f --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/20240109 SSM\346\225\264\345\220\210\347\254\224\350\256\260.md" @@ -0,0 +1,582 @@ +# SSM + +1. 新建班级-姓名文件夹,解压对应的压缩包 + +![image-20240108080858649](assets/image-20240108080858649.png) + +![image-20240108080935859](assets/image-20240108080935859.png) + +1. 将SSM目录拖入个人文件夹 + +![image-20240108080949150](assets/image-20240108080949150.png) + +1. 解压本地仓库压缩包,启用IDEA,设置本地仓库目录 + + ![image-20240108081550289](assets/image-20240108081550289.png) + +2. 打开SSM目录 + +![image-20240108081735890](assets/image-20240108081735890.png) + +1. 启动MySql + + ![image-20240108081212741](assets/image-20240108081212741.png) + +2. IDEA中,打开右侧数据库,输入用户名和密码,确认,如果右下角有弹窗提示mysql方言,点一下确认 + +![image-20240108081920261](assets/image-20240108081920261.png) + +4. 导入提供的sql文件 + + ![image-20240108082205065](assets/image-20240108082205065.png) + +1. 刷新Pom文件中的依赖 + + ![image-20240108082344015](assets/image-20240108082344015.png) + + ![image-20240108082714186](assets/image-20240108082714186.png) + + 1. 设置启用项tomcat7:run + + ![image-20240108082606502](assets/image-20240108082606502.png) + + + + 2. 新建各个包 + + 1. config + + 1. WebConfig ,继承AACD + + ![image-20240108083641002](assets/image-20240108083641002.png) + + ```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[]{"/"}; + } + } + ``` + + + + 2. 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 { // 实现WMC + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); + } + } + + + ``` + + + + 3. 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.service", "com.mdd.mapper"}) + @Import({JdbcConfig.class, MybatisConfig.class}) + public class SpringConfig { + } + + ``` + + + + 4. 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) { //SSFB + SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); + ssfb.setDataSource(dataSource); + ssfb.setTypeAliasesPackage("com.mdd.domain"); + return ssfb; + } + + @Bean + public MapperScannerConfigurer mapperScannerConfigurer() { //MSC + MapperScannerConfigurer msc = new MapperScannerConfigurer(); + msc.setBasePackage("com.mdd.mapper"); + return msc; + } + } + + + ``` + + + + 5. 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(); //DDS + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///ssm"); + ds.setUsername("root"); + ds.setPassword("root");//123456 + return ds; + } + + } + + ``` + + + + 2. domain + + 1. 与数据表名一致,tb_book表名对应的实体类名就是Book + + ```java + package com.mdd.domain; + + public class Book { + private Integer id; + private String bookName; + private String author; + private String publisher; + + public Book() { + } + + @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; + } + } + + ``` + + + + 3. controller + + 1. BookController + + ```java + package com.mdd.controller; + + import com.mdd.domain.Book; + import com.mdd.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); + + } + // 添加 + @PostMapping + public R addBook(@RequestBody Book book){ // 不要忘记@RequestBody 注解 + Boolean flag = bookService.addBook(book); + Integer code = flag ? 3001 : 3000; + Object data = flag ? "添加成功!":"添加失败!"; + return new R(code, data); + } + // 修改 + @PutMapping + public R updateBook(@RequestBody Book book){ + Boolean flag = bookService.updateBook(book); + Integer code = flag ? 5001 : 5000; + Object data = flag ? "修改成功!":"修改失败!"; + 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 flag = bookService.delById(id); + Integer code = flag ? 4001 : 4000; + Object data = flag ? "删除成功!":"删除失败!"; + return new R(code, data); + } + + } + + + ``` + + + + 2. R + + ```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) { + 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; + } + } + + ``` + + + + 3. Code + + ```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 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; + } + + ``` + + 4. service + + 1. BookService 接口 + + ```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); + } + + ``` + + + + 2. 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 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; + } + } + + ``` + + + + 5. mapper + + 1. BookMapper 接口 + + ```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 * 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 * from tb_book where id = #{id} ") + Book getById(Integer id); + + @Delete("delete from tb_book where id=#{id};") + Integer delById(Integer id); + } + + ``` + + + + 3. 新建各个类和接口名(同上) + + 4. 按顺序写类和接口的代码 + + ![image-20240108083524149](assets/image-20240108083524149.png) + + + + 5. 使用POSTMAN测试相关功能 + + 1. 查询所有 + + ![image-20240108094359409](assets/image-20240108094359409.png) + + 2. 添加书本 + + ![image-20240108094454233](assets/image-20240108094454233.png) + + 1. 修改书本 + + ![image-20240108094532181](assets/image-20240108094532181.png) + + 1. 按id查询 + + ![image-20240108094602360](assets/image-20240108094602360.png) + + 1. 按id删除 + + ![image-20240108094626039](assets/image-20240108094626039.png) + + 6. 整合前端代码 + + 1. 解压前端资料,复制到webapp目录下 + + ![image-20240108101441337](assets/image-20240108101441337.png) + + 1. 修改SpringMvcConfig + + ![image-20240108101528135](assets/image-20240108101528135.png) + +3.根据controller类中的返回值类型,修改index.html 中js的代码 + +```java + // 查所有 + @GetMapping + public List getAll() { + List bookList = bookService.getAll(); +// // 成功,失败都可能 +// Integer code = bookList != null ? 2001 : 2000; +// Object data = bookList != null ? bookList:"查询数据失败,可能没有数据!"; +// +// return new R(code, data); + return bookList; + } +``` + +```js + // 获取所有图书 + function getBooks() { + var xhr = new XMLHttpRequest(); + xhr.open('GET', '/books'); + xhr.onreadystatechange = function () { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + showBooks(JSON.parse(xhr.responseText)); 直接返回数据 + // showBooks(JSON.parse(xhr.responseText).data); // 返回R + } else { + alert('获取图书列表失败:' + xhr.statusText); + } + } + }; +``` + -- Gitee