0


【SSM整合】对Spring、SpringMVC、MyBatis的整合,以及Bootstrap的使用,简单的新闻管理系统

✅作者简介:热爱Java后端开发的一名学习者,大家可以跟我一起讨论各种问题喔。
🍎个人主页:Hhzzy99
🍊个人信条:坚持就是胜利!
💞当前专栏:【Spring】
🥭本文内容:SSM框架的整合使用,还有bootstrap等前端框架的简单使用,做一个简单的新闻管理系统

SSM整合


文章目录


前言

在前文中,我们学完了

Spring

SpringMVC

的内容,现在我们就将学过的内容做一个整合使用,做一个新闻管理系统。


系统功能需求

结构图
系统结构设计
本系统根据功能的不同,项目结构可以划分为以下几个层次:

  • 持久对象层:由若干持久化类(实体类)组成。
  • 数据访问层:曲若干DAO 接口和Mybatis映射文件组成。接口的名称统一以DAO结尾,且 MyBatis 的映射文件名称要与接口的名称相同。
  • 业务逻辑层:该层由若干 Service 接口和实现类组成。在本系统中,业务逻辑层的接口统一使用Service结尾,共实现类名称统一在接口名后加Impl。该层主要用于实现系统的业务逻辑。
  • Web表现层:该层主要包括 SpringMVC 中的Controller类和 JSP 页面。Controller 类主要负责拦截用户请求,并调用业务逻拜层中相应组件的业务逻輯方法来处理用户请求,然后将相应的结果返回给 JSP 页面。数据库设计 数据库设计比较简单,这里我直接放建表语句
createdatabase news;use news;#创建一个角色表createtable t_role(
roleId intprimarykey,
roleName varchar(20));#插入两条数据insertinto t_role values(1,'管理员'),(2,'信息员');#创建一个名为t_user的表createtable t_user(`userId`intprimarykeyAUTO_INCREMENT,`userName`varchar(20),`loginName`varchar(20),`password`varchar(20),`tel`varchar(50),`registerTime`datetime,`status`char(1),`roleId`int,foreignkey(roleId)references t_role(roleId));#插入一条数据insertinto t_user(userName, loginName,`password`,`status`, roleId)values("至简","admin","abc123456","2",1);#创建一个名称为t_category的表createtable t_category(
categoryId intprimarykey,
categoryName varchar(20));#插入四条数据insertinto t_category values(1,"今日头条"),(2,"综合资讯"),(3,"国内新闻"),(4,"国际新闻");#创建一个名为t_news的表createtable t_news(
newsId intprimarykeyAUTO_INCREMENT,
title varchar(60),
contentTitle varchar(120),
titlePicUrl varchar(120),
content TEXT,
contentAbstract varchar(300),
keywords varchar(100),
author varchar(30),
publishTime dateTime,
clicks int,
publishStatus char(1),
categoryId int,
userId int,foreignkey(categoryId)references t_category(categoryId),foreignkey(userId)references t_user(userId));

整合环境搭建

创建一个maven项目

添加Web整合(在Project structure里面的Facets中)
在这里插入图片描述

编辑pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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.hhzzy99</groupId><artifactId>newsManagerment</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><spring.version>5.2.4.RELEASE</spring.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><java.version>1.8</java.version></properties><dependencies><!--Spring相关的依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</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-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!--数据库相关的依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.3</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.3</version></dependency><!--连接池相关的依赖--><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId><version>2.1.1</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.9.6</version></dependency><dependency><groupId>org.apache.ant</groupId><artifactId>ant-launcher</artifactId><version>1.9.6</version></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.ow2.asm</groupId><artifactId>asm</artifactId><version>7.2</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.5</version><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/cglib/cglib --><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.2.6</version></dependency><!-- https://mvnrepository.com/artifact/org.javassist/javassist --><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.24.1-GA</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.12.1</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.11.0</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.30</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.22</version></dependency><!-- https://mvnrepository.com/artifact/ognl/ognl --><dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.2.10</version></dependency><!--JSTL标签库--><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-spec</artifactId><version>1.2.5</version></dependency><dependency><groupId>org.apache.taglibs</groupId><artifactId>taglibs-standard-impl</artifactId><version>1.2.5</version></dependency><!--Jackson框架所需要的jar包--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.10.1</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.10.1</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.10.1</version></dependency><!--java工具类--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency></dependencies><!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --><build><resources><resource><directory>src/main</directory><includes><include>**/*.properties</include><include>**/*.xml</include><include>**/**/*.xml</include></includes><filtering>false</filtering></resource></resources><plugins><!--加上此代码即可--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.0.0</version><configuration><webResources><resource><directory>web</directory></resource></webResources></configuration></plugin></plugins></build></project>

编写配置文件

在resources下创建相应的文件
db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:/localhost:3306/news?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"><!--读取db.properties--><context:property-placeholderlocation="classpath:db.properties"/><!--配置数据源--><beanid="dataSource"class="org.apache.commons.dbcp2.BasicDataSource"><propertyname="driverClassName"value="${jdbc.driver}"/><propertyname="url"value="${jdbc.url}"/><propertyname="username"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/><propertyname="maxTotal"value="${jdbc.maxTotal}"/><propertyname="maxIdle"value="${jdbc.maxIdle}"/><propertyname="initialSize"value="${jdbc.initialSize}"/></bean><!--事务管理器,依赖于数据源--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!--通知--><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><!--传播行为--><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="insert*"propagation="REQUIRED"/><tx:methodname="add*"propagation="REQUIRED"/><tx:methodname="create*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="find"propagation="SUPPORTS"read-only="true"/><tx:methodname="select*"propagation="SUPPORTS"read-only="true"/><tx:methodname="get*"propagation="SUPPORTS"read-only="true"/></tx:attributes></tx:advice><!--切面--><aop:config><aop:advisoradvice-ref="txAdvice"pointcut="execution(* com.hzy.service.*.*(..))"/></aop:config><!--配置mybatis工厂--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--注入数据源--><propertyname="dataSource"ref="dataSource"/><!--指定核心配置文件位置--><propertyname="configLocation"value="classpath:mybatis-config.xml"/></bean><!--配置mapper扫描器--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.hzy.dao"></property></bean><!--扫描Service--><context:component-scanbase-package="com.hzy.service"/></beans>

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><!--配置别名--><typeAliases><packagename="com.hzy.po"/></typeAliases><mappers><mapperresource="mapper/UserDao.xml"></mapper><mapperresource="mapper/RoleDao.xml"></mapper><mapperresource="mapper/CategoryDao.xml"></mapper><mapperresource="mapper/NewsDao.xml"></mapper></mappers></configuration>

springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"><!--指定需要扫描的包--><context:component-scanbase-package="com.hzy.controller"/><!--配置注解驱动--><mvc:annotation-driven/><!--配置静态资源的访问映射,此配置中的文件将不被前端控制器拦截--><mvc:default-servlet-handler/><mvc:resourcesmapping="/js/**"location="/js/"></mvc:resources><mvc:resourcesmapping="/css/**"location="/css/"></mvc:resources><mvc:resourcesmapping="/images/**"location="/images/"></mvc:resources><!--定义视图解析器--><beanid="viewResoler"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--设置前缀--><propertyname="prefix"value="/WEB-INF/jsp/"/><!--设置后缀--><propertyname="suffix"value=".jsp"/></bean><!--配置拦截器--><mvc:interceptors><!--使用bean直接定义在<mvc:interceptors>下面的Interceptor将拦截所有请求--><!--<bean class="com.hzy.interceptor.LoginInterceptor"/>--><mvc:interceptor><!--配置拦截器作用的路径--><mvc:mappingpath="/**"/><!--配置不需要拦截作用的路径--><mvc:exclude-mappingpath="/index.action"/><beanclass="com.hzy.interceptor.LoginInterceptor"/></mvc:interceptor></mvc:interceptors></beans>

编写/web/WEB-INF目录下的web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-appxmlns="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"><!--配置加载Spring文件的监听器--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--编码过滤器--><filter><filter-name>encoding</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>encoding</filter-name><url-pattern>*.action</url-pattern></filter-mapping><!--配置SpringMVC前端控制器--><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*:springmvc-config.xml</param-value></init-param><!--配置服务器启动时立即加载SpringMVC配置文件--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><!--系统默认页面--><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

此时配置文件基本完成
下面是项目的目录结构
目录结构

运行结果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


结语

以上就是今天要讲的内容,主要是SSM的整合使用,因为代码太多了,不好放在这上面,所以大家可以关注公众号 回复 新闻就可以获取代码哟。

如果各位大哥大姐对我所写的内容觉得还行的话,希望可以点个关注,点个收藏,您的支持就是我最大的动力,非常感谢您的阅读(❁´◡`❁)


本文转载自: https://blog.csdn.net/XUHUANGHOST/article/details/128575502
版权归原作者 Hhzzy99 所有, 如有侵权,请联系我们删除。

“【SSM整合】对Spring、SpringMVC、MyBatis的整合,以及Bootstrap的使用,简单的新闻管理系统”的评论:

还没有评论