xmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> []( )4、Mybatis层编写 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/springmvc?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root 注意:database.properties文件中不能有空格! )编写数据库对应的实体类 pojo.Books 使用Lombok插件 @Data @AllArgsConstructor @NoArgsConstructor public class Books { private Integer bookID; private String bookName; private Integer bookCounts; private String detail; } 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(); } insert into springmvc.books(bookName, bookCounts, detail) values (#{bookName},#{bookCounts},#{detail}) delete from springmvc.books where bookID = #{bookId} update springmvc.books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID = #{bookID}; select * from springmvc.books where bookID = #{bookId} select * from springmvc.books )编写Service层的接口和实现类 接口: public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(int id); //查询全部的书 List queryAllBook(); } 实现类: public class BookServiceImpl implements BookService { //调用dao层的操作,设置一个set接口,方便Spring管理 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } public int addBook(Books books) { return bookMapper.addBook(books); } 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(); } } )OK,到此,底层需求操作编写完毕! []( )5、Spring层 )配置Spring整合MyBatis,这里数据源使用c3p0连接池; )编写Spring整合Mybatis的相关的配置文件;spring-dao.xml xmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:context=“http://www.springframework.org/schema/context” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> 注意:数据库错误,建议使用spring默认连接池 xmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:context=“http://www.springframework.org/schema/context” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springfram ework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> )Spring层搞定! []( )6、SpringMVC层 web项目创建方式: 右键项目 --> Add frameworks Support --> Java EE --> Web Application --> 选中,然后点击OK,就创建成功了! xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd” version=“4.0”> springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 springmvc / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter /* 15 xmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:context=“http://www.springframework.org/schema/context” xmlns:mvc=“http://www.springframework.org/schema/mvc” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> mvc:annotation-driven/ mvc:default-servlet-handler/ xmlns=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> )配置文件,暂时结束!Controller 和 视图层编写 []( )7、Controller 和 视图层编写 @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”; } } <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8” %> 点击进入列表页 <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %> <%@ page contentType=“text/html;charset=UTF-8” language=“java” %> 书籍列表 —— 显示所有书籍 新增
【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
书籍编号 书籍名字 书籍数量 书籍详情 操作



