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

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

SSM整合



前言

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


系统功能需求


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

  • 持久对象层:由若干持久化类(实体类)组成。
  • 数据访问层:曲若干DAO 接口和mybatis映射文件组成。接口的名称统一以DAO结尾,且 MyBatis 的映射文件名称要与接口的名称相同。
  • 业务逻辑层:该层由若干 Service 接口和实现类组成。在本系统中,业务逻辑层的接口统一使用Service结尾,共实现类名称统一在接口名后加Impl。该层主要用于实现系统的业务逻辑。
  • Web表现层:该层主要包括 SpringMVC 中的Controller类和 JSP 页面。Controller 类主要负责拦截用户请求,并调用业务逻拜层中相应组件的业务逻輯方法来处理用户请求,然后将相应的结果返回给 JSP 页面。
    数据库设计
    数据库设计比较简单,这里我直接放建表语句
create database news;

use news;

#创建一个角色表
create table t_role(
roleId int primary key,
roleName varchar(20)
);

#插入两条数据
insert into t_role values(1,'管理员'),(2,'信息员');

#创建一个名为t_user的表
create table t_user(
	`userId` int primary key AUTO_INCREMENT,
	`userName` varchar(20),
	`loginName` varchar(20),
	`password` varchar(20),
	`tel` varchar(50),
	`registerTime` datetime,
	`status` char(1),
	`roleId` int,
	foreign key (roleId) references t_role(roleId)
	);

#插入一条数据
insert into t_user(userName, loginName, `password`, `status`, roleId) values("至简","admin","abc123456","2",1);

#创建一个名称为t_category的表
create table t_category(
categoryId int primary key,
categoryName varchar(20)
);

#插入四条数据
insert into t_category values(1,"今日头条"),(2,"综合资讯"),(3,"国内新闻"),(4,"国际新闻");

#创建一个名为t_news的表
create table t_news(
newsId int primary key AUTO_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,
foreign key (categoryId) references t_category(categoryId),
foreign key (userId) references t_user(userId)
);


整合环境搭建

创建一个maven项目

添加Web整合(在Project structure里面的Facets中)

编辑pom.xml

<?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>***.hhzzy99</groupId>
    <artifactId>newsManagerment</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <spring.version>5.2.4.RELEASE</spring.version>
        <maven.***piler.source>1.8</maven.***piler.source>
        <maven.***piler.target>1.8</maven.***piler.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.***/artifact/org.apache.***mons/***mons-dbcp2 -->
        <dependency>
            <groupId>org.apache.***mons</groupId>
            <artifactId>***mons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.***mons</groupId>
            <artifactId>***mons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>***mons-logging</groupId>
            <artifactId>***mons-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.***/artifact/cglib/cglib -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.6</version>
        </dependency>

        <!-- https://mvnrepository.***/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.***/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.***/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>***.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>***.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>***.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--java工具类-->
        <dependency>
            <groupId>org.apache.***mons</groupId>
            <artifactId>***mons-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=***.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"?>
<beans xmlns="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-placeholder location="classpath:db.properties"/>
    <!--配置数据源-->
    <bean id="dataSource"
          class="org.apache.***mons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxTotal" value="${jdbc.maxTotal}"/>
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
    </bean>
    <!--事务管理器,依赖于数据源-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--传播行为-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--切面-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* ***.hzy.service.*.*(..))"/>
    </aop:config>

    <!--配置mybatis工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定核心配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--配置mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="***.hzy.dao"></property>
    </bean>
    <!--扫描Service-->
    <context:***ponent-scan base-package="***.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>
        <package name="***.hzy.po"/>
    </typeAliases>

    <mappers>
        <mapper resource="mapper/UserDao.xml"></mapper>
        <mapper resource="mapper/RoleDao.xml"></mapper>
        <mapper resource="mapper/CategoryDao.xml"></mapper>
        <mapper resource="mapper/NewsDao.xml"></mapper>
    </mappers>

</configuration>

springmvc-config.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: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:***ponent-scan base-package="***.hzy.controller"/>
    <!--配置注解驱动-->
    <mvc:annotation-driven/>
    <!--配置静态资源的访问映射,此配置中的文件将不被前端控制器拦截-->
    <mvc:default-servlet-handler/>
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
    <!--定义视图解析器-->
    <bean id="viewResoler"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--设置前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--设置后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--配置拦截器-->
    <mvc:interceptors>
        <!--使用bean直接定义在<mvc:interceptors>下面的Interceptor将拦截所有请求-->
        <!--<bean class="***.hzy.interceptor.LoginInterceptor"/>-->
        <mvc:interceptor>
            <!--配置拦截器作用的路径-->
            <mvc:mapping path="/**"/>
            <!--配置不需要拦截作用的路径-->
            <mvc:exclude-mapping path="/index.action"/>
            <bean class="***.hzy.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

编写/web/WEB-INF目录下的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_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>

    <!--系统默认页面-->
    <wel***e-file-list>
        <wel***e-file>index.jsp</wel***e-file>
    </wel***e-file-list>
</web-app>

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

运行结果图









结语

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

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

转载请说明出处内容投诉
CSS教程_站长资源网 » 【SSM整合】对Spring、SpringMVC、MyBatis的整合,以及Bootstrap的使用,简单的新闻管理系统

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买