| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> Java知识库 -> SSM整合 -> 正文阅读 |
|
[Java知识库]SSM整合 |
一 配置文件1.pom文件; 2.web.xml; 3.整合需要druid的properties的文件,存放连接数据库的数据; 4.mybatis的配置文件; 5.spring-mybatis的配置文件; 6.Spring-MVC的配置文件; 7.外部的配置文件;(js、jQuery) 二 类1.bean; 2.service包含实现类(Impl); 3.mapper; 4.controller; 三 实现3.1 pom文件<?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.openlab</groupId> <artifactId>ssmdemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.4.1</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.7.RELEASE</version> </dependency> <!-- jstl,servlet-api,junit --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- MBG --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> <scope>test</scope> </dependency> </dependencies> </project> 3.2 druid.propertiesjdbc.driverClassName:com.mysql.cj.jdbc.Driver jdbc.url:jdbc:mysql:///web?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true jdbc.name:root jdbc.password:123456 3.3 mybatis-config.xml<?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> <!-- 驼峰命名开启--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 映射器 mybatis中的 --> <typeAliases> <package name="com.openlab.bean"/> </typeAliases> <!-- 分页工具pager --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration> 3.4 Spring-mybatis.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描--> <context:component-scan base-package="com.openlab"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--加载本地的配置文件--> <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder> <!--配置数据源--> <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"> <property name="password" value="${jdbc.password}"></property> <property name="username" value="${jdbc.name}"></property> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> </bean> <!--与mybatis整合--> <!-- 获得sqlSessionFactorybuild对象--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/openlab/mapper/UserMapper.xml"></property> </bean> <!--获得sqlSession对象--> <bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean> <!--获得mapper对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.openlab.mapper"></property> <property name="sqlSessionTemplateBeanName" value="sessionTemplate"></property> </bean> <!--获得事务管理对象--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事务注解--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans> 3.5 springMVC.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启扫描--> <context:component-scan base-package="com.openlab" use-default-filters="false"> <!-- 设置只扫描的注解--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--加载静态文件--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--能进入controller里--> <mvc:annotation-driven></mvc:annotation-driven> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <mvc:view-controller path="/index" view-name="index"></mvc:view-controller> <mvc:view-controller path="/adduser" view-name="adduser"></mvc:view-controller> </beans> 3.6?web.xml<?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"> <!--对语言的修改 字符编码滤波器--> <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> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--支持resoult风格 隐藏的方法滤波器--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--注册servlet 调度servlet--> <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> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring_Mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 3.7?bean(实体类)package com.openlab.bean; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter @NoArgsConstructor @AllArgsConstructor public class User { private String id; private String username; private String password; private Integer roleId; private String iconURL; } 3.8?sevice(服务层)(业务实现层)package com.openlab.service; import com.github.pagehelper.PageInfo; import com.openlab.bean.User; public interface UserService { public int save(User userr); // public int delete(Integer userId); // public int update(User user); public PageInfo getPageInfo(int pageNo,int nums,int pageSize); } package com.openlab.service.Impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.openlab.bean.User; import com.openlab.mapper.UserMapper; import com.openlab.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class UserSeriviceImpl implements UserService { @Autowired public UserMapper userMapper; @Override public int save(User user) { return userMapper.save(user); } /* @Override public int delete(Integer userId) { return userMapper.delete(userId); } @Override public int update(User user) { return userMapper.update(user); }*/ @Override public PageInfo getPageInfo(int pageNo, int nums, int pageSize) { Page<User> page= PageHelper.startPage(pageNo,pageSize); List<User> list=userMapper.findAll(); PageInfo<User> pageInfo=new PageInfo<>(list,nums); return pageInfo; } } 3.9?mapper(mapper)package com.openlab.mapper; import com.openlab.bean.User; import java.util.List; public interface UserMapper { public int save(User uesr); /* public int delete(Integer userId); public int update(User user);*/ public List<User> findAll(); } 3.10?controller(控制层)package com.openlab.Controller; import com.github.pagehelper.PageInfo; import com.openlab.bean.User; import com.openlab.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import java.util.Map; @Controller public class UserConrtoller { @Autowired @Qualifier("userSeriviceImpl") public UserService userService; @GetMapping("/user") public String list(int pageNo, Map<String,Object> map){ int pageSize=5; int nums=5; PageInfo pageInfo=userService.getPageInfo(pageNo,nums,pageSize); map.put("pageInfo",pageInfo); return "userlist"; } @PostMapping("/user") public String add(User user,int pageNo,Map<String,Object> map){ int pageSize=5; int nums=5; int t=userService.save(user); if(t!=1){ return "userlist"; }else{ PageInfo pageInfo=userService.getPageInfo(pageNo,nums,pageSize); map.put("pageInfo",pageInfo); return "userlist"; } } } 3.11?表现层(jsp)index.jsp(页面开始画面) <%-- Created by IntelliJ IDEA. User: ASUS Date: 2021/9/28 Time: 11:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <a href="${pageContext.request.contextPath}/user?pageNo=1">查看所有人员</a> </body> </html> userlist.jsp(查看所有人员页面) <%-- Created by IntelliJ IDEA. User: ASUS Date: 2021/9/28 Time: 16:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css"/> <script type="text/javascript" src="${pageContext.request.contextPath}/jQuery/jquery-1.12.4.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script> </head> <body> <div id="container"> <table class="table table-striped" title="table"> <tr> <td>人员编号</td> <td>人员姓名</td> <td>人员密码</td> <td>操作</td> </tr> <c:forEach items="${pageInfo.list}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td> <a href="${pageContext.request.contextPath}/adduser"><span class="label label-success">添加</span></a> <a href=""><span class="label label-warning">修改</span></a> </td> </tr> </c:forEach> </table> <div style="margin-left: 100px"> <nav aria-label="Page navigation" style=""> <ul class="pagination"> <li> <a href="#" aria-label="${pageInfo.prePage}"> <span aria-hidden="true">«</span> </a> </li> <c:forEach items="${pageInfo.navigatepageNums}" var="num"> <li><a href="${pageContext.request.contextPath}/user?pageNo=${num}">${num}</a></li> </c:forEach> <li> <a href="#" aria-label="${pageInfo.nextPage}"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </div> </div> </body> </html> adduser.jsp(添加人员页面) <%-- Created by IntelliJ IDEA. User: ASUS Date: 2021/9/28 Time: 21:09 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/user?pageNo=1" method="post"> 编号:<input type="text" name="id"> 姓名:<input type="text" name="username"> 密码:<input type="text" name="password"> 菜单编号:<input type="text" name="roleId"> <input type="submit" value="提交"> </form> </body> </html> 分层: 表示层(css.jquery.js) 控制层(servlet、jsp) 业务实现层 Dao(mapper) Service(接口的声明) bean(实体对象) |
|
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/23 18:49:20- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |