diff --git "a/50 \345\274\240\350\265\267\347\221\236/20240110 \345\233\276\344\271\246\345\257\271.md" "b/50 \345\274\240\350\265\267\347\221\236/20240110 \345\233\276\344\271\246\345\257\271.md" new file mode 100644 index 0000000000000000000000000000000000000000..9342cdf618a2e2c8d2d6b1bfc7fa905075bd291d --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20240110 \345\233\276\344\271\246\345\257\271.md" @@ -0,0 +1,785 @@ +pom + +```java + + 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.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 + + + + + + + +``` + + + +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; + } +} + +``` + +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 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; + } +} + +``` + +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.service","com.mdd.mapper"}) +@Import({JdbcConfig.class, MybatisConfig.class}) +public class SpringConfig { +} +``` + +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"); + } +} +``` + +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[]{"/"}; + } +} +``` + +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 getBook(){ + return bookService.getBook(); + } + @PostMapping + public void insertBook(@RequestBody Book book){ + bookService.insertBook(book); + } + @DeleteMapping("/{id}") + public void deleteBook(@PathVariable Integer id){ + bookService.deleteBook(id); + } + @PutMapping + public void updateBook(@RequestBody Book book){ + bookService.updateBook(book); + } + @GetMapping("/{id}") + public List selectById(@PathVariable Integer id){ + return bookService.selectById(id); + } +} +``` + +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() { + } + + public Book(Integer id, String bookName, String author, String publisher) { + this.id = id; + this.bookName = bookName; + this.author = author; + this.publisher = publisher; + } +} +``` + +mapper.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 id,book_name as bookName,author,publisher from tb_book;") + List getBook(); + + @Insert("insert into tb_book(book_name,author,publisher) values (#{bookName},#{author},#{publisher})") + void insertBook(Book book); + + @Delete("delete from tb_book where id = #{id};") + void deleteBook(Integer id); + + @Update("update tb_book set book_name = #{bookName},author = #{author},publisher = #{publisher} where id = #{id};") + void updateBook(Book book); + + @Select("select id,book_name as bookName,author,publisher from tb_book where id = #{id};") + List selectById(Integer id); +} +``` + +service下面先建一个接口为BookService,然后再建一个软件包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 getBook() { + return bookMapper.getBook(); + } + + public void insertBook(Book book) { + bookMapper.insertBook(book); + } + + public void deleteBook(Integer id) { + bookMapper.deleteBook(id); + } + + public void updateBook(Book book) { + bookMapper.updateBook(book); + } + + public List selectById(Integer id) { + return bookMapper.selectById(id); + } +} +``` + +```java +package com.mdd.service; + +import com.mdd.domain.Book; + +import java.util.List; + +public interface BookService { + List getBook(); + + void insertBook(Book book); + + void deleteBook(Integer id); + + void updateBook(Book book); + + List selectById(Integer id); +} +``` + +webapp + +index.html + +```java + + + + + 图书管理系统 + + + + +

图书管理系统

+ + + + + +

图书列表

+ + + + + + + + + + + +
ID书名作者出版商操作
+ + +
+
+

编辑图书

+
+ +
+ +
+ +
+
+ + +
+
+
+
+ + + + + +``` + +```java +index.jsp + + + +

Hello World!

+ + +``` + +web.xml + +```java + + + + Archetype Created Web Application + +``` \ No newline at end of file