2021 最新 SSM(Spring+Spring MVC,java 分布式系统面试题
<?xml version="1.0" encoding="UTF-8"?> <beans 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 "> </beans> []( )4、Mybatis 层编写 --------------------------------------------------------------------------- 1. **数据库配置文件 database.properties** jdbc.driver=com.mysql.jdbc.Driver # 如果使用的是 MySql8.0+,增加一个时区的配置;serverTimezone=Asia/Shanghai jdbc.url=jdbc:mysql://localhost:3306/springmvc?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root `注意:database.properties 文件中不能有空格!` 2. **关联数据库** <context:property-placeholder location="classpath:database.properties"/> 3. **编写 MyBatis 的核心配置文件** <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.shun.pojo"/> </typeAliases> <mappers> <mapper class="com.shun.dao.BookMapper"/> </mappers> </configuration> 4. #### []( )编写数据库对应的实体类 pojo.Books 使用 Lombok 插件 @Data @AllArgsConstructor @NoArgsConstructor public class Books { private Integer bookID; private String bookName; private Integer bookCounts; private String detail; } 5. **编写 Dao 层 的 Mapper 接口!** public interface BookMapper { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId") int id); //查询全部的书 List<Books> queryAllBook(); } 6. 编写接口对应的 Mapper.xml 文件。需要导入 MyBatis 的包; <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.shun.dao.BookMapper"> <insert id="addBook" parameterType="Books"> insert into springmvc.books(bookName, bookCounts, detail) values (#{bookName},#{bookCounts},#{detail}) </insert> <delete id="deleteBookById" parameterType="int"> delete from springmvc.books where bookID = #{bookId} </delete> <update id="updateBook" parameterType="Books"> update springmvc.books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID = #{bookID}; </update> <select id="queryBookById" resultType="Books"> select * from springmvc.books where bookID = #{bookId} </select> <select id="queryAllBook" resultType="list"> select * from springmvc.books </select> </mapper> 7. #### []( )编写 Service 层的接口和实现类 接口: public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(int id); //查询全部的书 List<Books> 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<Books> queryAllBook() { return bookMapper.queryAllBook(); } } #### []( )OK,到此,底层需求操作编写完毕! []( )5、Spring 层 ------------------------------------------------------------------------ 1. #### []( )配置 Spring 整合 MyBatis,这里数据源使用 c3p0 连接池; 2. #### []( )编写 Spring 整合 Mybatis 的相关的配置文件;spring-dao.xml <?xml version="1.0" encoding="UTF-8"?> <beans 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 "> <!--1.关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!--2.连接池 dbcp:半自动化操作 ,不能自动连接 c3p0: 自动化操作(自动化的加载配置文件,并且可以自动设置到对象中!) druid : hikari --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!–c3p0 连接池的私有属性 –> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!–关闭连接后不自动 commit–> <property name="autoCommitOnClose" value="false"/> <!–获取连接超时时间 –> <property name="checkoutTimeout" value="1000"/> <!–当获取连接失败重试次数 –> <property name="acquireRetryAttempts" value="2"/> </bean> <!--3.SQLSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定 Mybatis 的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--4.配置 dao 接口扫描包,动态的实现了 Dao 接口可以注入到 Spring 容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入 sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要扫描的 dao 包--> <property name="basePackage" value="com.shun.dao"/> </bean> </beans> `注意:数据库错误,建议使用 spring 默认连接池` <!--spring 默认连接池--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> 3. **Spring 整合 service 层** <?xml version="1.0" encoding="UTF-8"?> <beans 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 ``` 【一线大厂 Java 面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】 浏览器打开:qq.cn.hn/FTf 免费领取 ``` ework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--扫描 service 下的包--> <context:component-scan base-package="com.shun.service"/> <!--2.将我们所有的业务类,注入到 Spring,可以通过配置,或者注解实现--> <bean id="BookServiceImpl" class="com.shun.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--3.申明式事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> </beans> #### []( )Spring 层搞定! []( )6、SpringMVC 层 --------------------------------------------------------------------------- 1. **web.xml** web 项目创建方式: ? 右键项目 --> Add Frameworks Support --> Java EE --> Web Application --> 选中,然后点击 OK,就创建成功了! <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 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"> <!--DispatchServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--乱码过滤--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Session 过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app> 2. **spring-mvc.xml** <?xml version="1.0" encoding="UTF-8"?> <beans 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"> <!--1.注解驱动--> <mvc:annotation-driven/> <!--2.静态资源过滤--> <mvc:default-servlet-handler/> <!--3.扫描包:controller--> <context:component-scan base-package="com.shun.controller"/> <!--4.视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> 3. **Spring 配置整合文件,applicationContext.xml** <?xml version="1.0" encoding="UTF-8"?> <beans 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 "> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans> #### []( )配置文件,暂时结束!Controller 和 视图层编写 []( )7、Controller 和 视图层编写 ----------------------------------------------------------------------------------- 1. **BookController 类编写 , 方法一:查询全部书籍** @Controller @RequestMapping("/book") public class BookController { @Autowired @Qualifier("BookServiceImpl") private BookService bookService; @RequestMapping("/allBook") public String list(Model model) { List<Books> list = bookService.queryAllBook(); model.addAttribute("list", list); return "allBook"; } } 2. **编写首页 index.jsp** <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE HTML> <html> <head> <title>首页</title> <style type="text/css"> 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: 4px; } </style> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">点击进入列表页</a> </h3> </body> </html> 3. **书籍列表页面 allbook.jsp** <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>书籍列表</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>书籍列表 —— 显示所有书籍</small> </h1> </div> </div> </div> <div class="row"> <div class="col-md-4 column"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>书籍编号</th> <th>书籍名字</th> <th>书籍数量</th> <th>书籍详情</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach var="book" items="${requestScope.get('list')}"> <tr> <td>${book.getBookID()}</td>
评论