欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

整合SSM

时间:2023-06-10
文章目录

六、整合SSM

6.1、搭建框架6.2、小项目测试(书籍展示)

6.2.1、数据库环境6.2.2、基本环境6.2.3、Mybatis层6.2.4、Spring层6.2.5、SpringMVC层 六、整合SSM 6.1、搭建框架

1、pom.xml导入项目依赖(junit,mysql-connector-java,c3p0,servlet-api,jsp-api,jstl,mybatis,mybatis-spring,spring-webmvc,spring-jdbc,lombok,aspectjweaver)

junit junit 4.12 test mysql mysql-connector-java 8.0.26 com.mchange c3p0 0.9.5.5 javax.servlet servlet-api 2.5 javax.servlet.jsp jsp-api 2.2 javax.servlet jstl 1.2 org.mybatis mybatis 3.5.7 org.mybatis mybatis-spring 2.0.6 org.springframework spring-webmvc 5.3.14 org.springframework spring-jdbc 5.3.15 org.projectlombok lombok 1.18.22 org.aspectj aspectjweaver 1.9.7

2、配置maven过滤问题

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

3、建立基本结构和配置框架!

com.rui.pojocom.rui.daocom.rui.servicecom.rui.controller

4、编写数据库配置文件 database.properties

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=truejdbc.username=rootjdbc.password=123456

5、编写MyBatis的核心配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

6、编写Spring整合Mybatis的相关的配置文件;spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>

7.编写Spring整合service层的相关的配置文件;spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>

8、编写SpringMVC层的web.xml

<?xml version="1.0" encoding="UTF-8"?> springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 springmvc / encoding org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encoding *.properties ***.properties **/*.xml false

2、建立基本结构和配置框架!

com.kuang.pojocom.kuang.daocom.kuang.servicecom.kuang.controller 6.2.3、Mybatis层

1、数据库配置文件 database.properties

jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=truejdbc.username=rootjdbc.password=123456

2、IDEA关联数据库

3、编写MyBatis的核心配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

4、编写数据库对应的实体类 com.rui.pojo.Books

使用lombok插件!

package com.rui.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }

5、编写Dao层的 Mapper接口!BookMapper.java

package com.rui.dao;import com.rui.pojo.Books;import org.apache.ibatis.annotations.Param;import java.util.List;public interface BookMapper { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId")int id); //查询全部的书 List queryAllBook(); //查询指定书 Books queryBookByName(@Param("bookName")String bookName);}

6、编写接口对应的 Mapper.xml 文件。需要导入MyBatis的包;

<?xml version="1.0" encoding="UTF-8" ?> 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;

7、编写Service层的接口和实现类

接口:

package com.rui.service;import com.rui.pojo.Books;import org.apache.ibatis.annotations.Param;import java.util.List;public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId")int id); //查询全部的书 List queryAllBook(); //查询指定书 Books queryBookByName(String bookName);}

实现类:

package com.rui.service;import com.rui.dao.BookMapper;import com.rui.pojo.Books;import java.util.List;public class BookServiceImpl implements BookService{ //service调用dao层 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } @Override public Books queryBookById(int id) { return bookMapper.queryBookById(id); } @Override public List queryAllBook() { return bookMapper.queryAllBook(); } @Override public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); }}

6.2.4、Spring层

1、编写Spring整合Mybatis的配置文件;spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>

2、Spring整合service层配置文件spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>

6.2.5、SpringMVC层

1、web配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?> springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 springmvc / encoding org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encoding /* 15

2、SpringMVC到servlet的核心配置文件spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

3、Spring配置整合文件,applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

4、编写Controller 和 视图层

Controller:创建BookController

package com.rui.controller;import com.rui.pojo.Books;import com.rui.service.BookService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;import java.util.List;@Controller@RequestMapping("/book")public class BookController { //controller层调用service层 @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查询全部书籍,并返回到一个所有书籍页面 @RequestMapping("/allBook") public String list(Model model){ List books = bookService.queryAllBook(); model.addAttribute("list",books); return "allBook"; } //跳转到增加书籍的页面 @RequestMapping("/toAddBook") public String toAddPaper(){ return "addBook"; } //添加书籍的请求 @RequestMapping("/addBook") public String addBook(Books books){ bookService.addBook(books); return "redirect:/book/allBook";//重定向 @RequestMapping("/allBook") } //Integer id:"可选的int参数'id'存在但由于被声明为基本类型而无法转换为空值" //跳转到修改页面 @RequestMapping("/toUpdateBook") public String toUpdatePaper(int id,Model model){ Books books = bookService.queryBookById(id); model.addAttribute("QBook",books); return "updateBook"; } //修改书籍的请求 @RequestMapping("/updateBook") public String updateBook(Books books){ bookService.updateBook(books); return "redirect:/book/allBook";//重定向 @RequestMapping("/allBook") } //删除书籍 @RequestMapping("/deleteBook/{bookId}") public String deleteBook(@PathVariable("bookId") int id){ bookService.deleteBookById(id); return "redirect:/book/allBook";//重定向 @RequestMapping("/allBook") } //查询书籍 @RequestMapping("/queryBook") public String queryBook(String queryBookName,Model model){ Books books = bookService.queryBookByName(queryBookName); List list = new ArrayList(); list.add(books); if(books==null){ list = bookService.queryAllBook(); model.addAttribute("error","未找到"); } model.addAttribute("list",list); return "allBook"; }}

视图层:index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 首页 a{ text-decoration: none; color:black; font-size:18px; } h3{ width: 180px; height: 38px; margin: 100px auto; text-align: center; line-height: 38px; background: deepskyblue; border-radius: 5px; } 进入书籍页面

在WEB-INF文件夹下创建jsp文件夹:allBook.jsp、addBook.jsp、updateBook.jsp

allBook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %> 书籍展示 <%-- BootStrap美化界面--%>

书籍展示——————显示所有书籍

新增书籍 显示全部书籍 书籍编号 书籍名称 书籍数量 书籍详情 操作 ${book.bookID} ${book.bookName} ${book.bookCounts} ${book.detail} 修改 | 删除

addBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 新增书籍 书籍展示——————新增书籍

updateBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 修改书籍 书籍展示——————修改书籍

项目结构如图:

最后启动Tomcat测试!

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。