當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC第五次课 SSM整合
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC第五次课 SSM整合
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
SSM框架整合之第五次課
復(fù)習(xí):
? spring 業(yè)務(wù)層
? springmvc 表現(xiàn)層
? mybatis 持久層
1.整合目標(biāo)
Spring框架,來整合其他框架,想到Spring第一次課,兼容性比較好。還可以整合其他xxx框架。
Spring框架整合其他兩個框架
2.項目搭建
自己搭建,比較簡單的maven web項目.
3.依賴創(chuàng)建
pom.xml的jar
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.aaa</groupId><artifactId>ssm01</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><!--版本號 --><spring.version>5.3.8</spring.version><mybatis.version>3.5.7</mybatis.version><mysql.version>8.0.25</mysql.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><!-- 其他jar依賴,需要的時候,再加 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.4</version></dependency></dependencies></project>4.web.xml文件
這個文件,在tomcat,只要tomcat啟動,就加載這個文件.
<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_3_1.xsd" version="3.1"><display-name>Archetype Created Web Application</display-name><!-- 啟動spring監(jiān)聽器,加載spring核心配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></context-param><!--配置前端核心控制器DispatcherServlet,加載springmvc的核心配置文件--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring_mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- utf-8解決中文亂碼 --><filter><filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>5.db.properties配置
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmdb?useTimezone=true&useUnicode=true&characterEncoding=utf8&useSSL=false jdbc.username=root jdbc.password=root6.spring.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><context:component-scan base-package="com.aaa.service,com.aaa.dao"/><!-- spring加載數(shù)據(jù)庫配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- dataSource --><bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置sqlsessionfactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"></property><!-- 掃描所有的mapper xml --><property name="mapperLocations" value="classpath:mapper/*.xml"></property><!-- <property name="configLocation" value="classpath:mybatisConfig.xml"></property> --></bean><!-- 把所有mapper接口生成代理類,注入到Spring容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.aaa.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean> </beans>7.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"><!--掃描控制器包--><context:component-scan base-package="com.aaa.controller"></context:component-scan><mvc:default-servlet-handler/><mvc:annotation-driven></mvc:annotation-driven><!--配置spring mvc默認(rèn)的jsp視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--配置返回視圖的前綴--><property name="prefix" value="/WEB-INF/views/"></property><!--配置返回視圖的后綴--><property name="suffix" value=".jsp"></property></bean> </beans>8.java層實現(xiàn)
控制器代碼:
package com.aaa.controller;import com.aaa.entity.Room; import com.aaa.service.RoomService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView;@Controller @RequestMapping("room") public class RoomController {@AutowiredRoomService roomService;@RequestMapping("/saveRoom")public String saveRoom(Room room){int result=roomService.saveRoom(room);System.out.println(room);System.out.println(result);return "room/roomlist";}@RequestMapping("/toaddRoom")public String toAddRoom(){return "room/addRoom";} }dao層接口實現(xiàn)
package com.aaa.dao;import com.aaa.entity.Room;public interface RoomDao {public int saveRoom(Room room); }實體類代碼實現(xiàn)
package com.aaa.entity;import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;@Data @NoArgsConstructor @AllArgsConstructor public class Room {private Integer id;private String roomcode;private Integer status; }業(yè)務(wù)層代碼實現(xiàn)
package com.aaa.service;import com.aaa.entity.Room;public interface RoomService {public int saveRoom(Room room); }業(yè)務(wù)層實現(xiàn)類代碼:
package com.aaa.service.impl;import com.aaa.dao.RoomDao; import com.aaa.entity.Room; import com.aaa.service.RoomService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service("roomService") public class RoomServiceImpl implements RoomService {@AutowiredRoomDao roomDao;@Overridepublic int saveRoom(Room room) {return roomDao.saveRoom(room);} }dao層實現(xiàn)類,在resources下新建mapper目錄,創(chuàng)建RoomDaoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.aaa.dao.RoomDao"><insert id="saveRoom" parameterType="com.aaa.entity.Room">insert into room values(#{id},#{roomcode},#{status})</insert> </mapper>9.測試頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <html> <head><title>增加自習(xí)室</title><base href="<%=basePath%>"> </head> <body> <%-- <form action="saveRoom" method="post">--%><form action="room/saveRoom" method="post"><label for="rid">編號</label><input type="text" name="id" id="rid"><br><label for="roomcode">名稱</label><input type="text" name="roomcode" id="roomcode"><br><label for="rstatus">狀態(tài)</label>空閑<input type="radio" name="status" id="rstatus" value="0"/>借用<input type="radio" name="status" checked="checked" value="1"/><input type="submit" value="提交"></form> </body> </html>7.1 測試整合之后的helloworld,就是index.jsp頁面是否正常運行;
7.2 可以自己動手寫代碼,把各層類 、接口填充完畢;或自動生成;測試Controller的跳轉(zhuǎn);
兩個路徑問題:
7.3 4等來回測試頁面action跳轉(zhuǎn)問題.
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><base href="<%=basePath%>">總結(jié)
以上是生活随笔為你收集整理的SpringMVC第五次课 SSM整合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring整合问题集合1
- 下一篇: SpringMVC之拦截器和异常处理