狂神说SSM整合的项目增删改查,单个页面.mybatis配置文件,spring配置文件

狂神说SSM整合的项目增删改查,单个页面.mybatis配置文件,spring配置文件,第1张

狂神说SSM整合的项目增删改查,单个页面.mybatis配置文件,spring配置文件

这里写自定义目录标题
  • 狂神说SSM整合的项目 idea2020.1 maven3.8 jdk 1.8 数据库mysql8.0.23 tomcat8.5
    • 结构目录
    • pom.xml
    • mybatis-config.xml
    • database.xml
    • spring-dao.xml
    • web.xml
    • spring-service.xml
    • spring-mvc.xml
    • applicationContext.xml
    • polo层的Books类
    • dao层BookMapper
    • BookMapper.xml
    • service层BookService
    • controller层BookController
    • web-inf jsp下的前端页面

狂神说SSM整合的项目 idea2020.1 maven3.8 jdk 1.8 数据库mysql8.0.23 tomcat8.5

SSM整合 2021年12月1号 亲测可用
版本不同关系不大,
主要是tomcat
主义pom.xml文件里的版本号

结构目录

结构目录

pom.xml

    
        junit
        junit
        4.13.1
    

    
        mysql
        mysql-connector-java
        8.0.23
    

    
        com.mchange
        c3p0
        0.9.5.2
    
    
        com.mchange
        mchange-commons-java
        0.2.11
    
    
    
        javax.servlet
        servlet-api
        2.5
    

    
        javax.servlet.jsp
        jsp-api
        2.2
    
    
        javax.servlet
        jstl
        1.2
    

    
        org.mybatis
        mybatis
        3.5.3
    
    
        org.mybatis
        mybatis-spring
        2.0.6
    

    
        org.springframework
        spring-webmvc
        5.3.13
    
    
        org.springframework
        spring-jdbc
        5.3.13
    

    
        org.projectlombok
        lombok
        1.18.20
        compile
    


    
        
            
                src/main/java
                
                    ***.xml
                
                false
            
            
                src/main/resources
                
                    ***.xml
                
                false
            
        
    

mybatis-config.xml



    
        
    
    
        
    

database.xml

注意,这里换成自己的地址,8.0以上的数据库必须加上useSSL=false/true

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
spring-dao.xml


    
    
    

    

        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
        
        
        
        
    

    
    
    
        
        
        
        
    


    
web.xml


    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        
        classpath:applicationContext.xml
    
    1


DispatcherServlet
/


        

encodingFilter

    org.springframework.web.filter.CharacterEncodingFilter


    encoding
    utf-8



encodingFilter
/*


        

15

        
spring-service.xml


    
    
    
    
        
    
    
    
        
        
    

spring-mvc.xml


    
    
    
    
    
    
    
        
        
        
    
    
    

applicationContext.xml


    
    
    

polo层的Books类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private Integer bookID;
    private String bookName;
    private Integer bookCounts;
    private String detail;
}
dao层BookMapper
import com.yang.pojo.Books;

import java.util.List;
@SuppressWarnings(value ="all")
public interface BookMapper {
    //增加一个book
    int addBook(Books book);
    //根据id删除一个Book
    int deleteBookById(int id);
    //更新Book
    int updateBook(Books books);
    //根据id查询,返回一个Book
    Books queryBookById(int id);
     //查询全部Book,返回list集合
    List queryAllBook();
}
BookMapper.xml



    
    
insert into ssmbuild.books(bookName,bookCounts,detail)
values (#{bookName}, #{bookCounts}, #{detail})
     
    
    
  delete from ssmbuild.books where bookID=#{bookID}
    
    
    
  update ssmbuild.books
set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID}
    
    
    
   SELECT * from ssmbuild.books



service层BookService
import com.yang.pojo.Books;

import java.util.List;
@SuppressWarnings(value = "all")
public interface BookService {
    //增加一个book
    int addBook(Books book);
    //根据id删除一个Book
    int deleteBookById(int id);
    //更新Book
    int updateBook(Books books);
    //根据id查询,返回一个Book
    Books queryBookById(int id);
    //查询全部Book,返回list集合
    List queryAllBook();
}

BookServiceImpl

import java.util.List;

public class BookServiceImpl implements BookService{
//调用dao层的 *** 作,设置一个set接口,方便Spring管理
  private BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Books book) {
        return bookMapper.addBook(book);
    }

    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }

    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }

    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    public List queryAllBook() {
        return bookMapper.queryAllBook();
    }
}
controller层BookController
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")

    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model){
        List list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
    //页面跳转
    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }
    //添加的请求
    @RequestMapping("/addBook")
    public String addPaper(Books books) {
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
  //跳转页面
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        Books books = bookService.queryBookById(id);
        System.out.println(books);
        model.addAttribute("book",books );
        return "updateBook";
    }
 //请求
    @RequestMapping("/updateBook")
    public String updateBook(Model model, Books book) {
        System.out.println(book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}
web-inf jsp下的前端页面

addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    新增书籍
    
    
    




    
        
            
                
                    新增书籍					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5637595.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存