?作者簡介:熱愛Java后端開發(fā)的一名學習者,大家可以跟我一起討論各種問題喔。
??個人主頁:Hhzzy99
??個人信條:堅持就是勝利!
??當前專欄:【Spring】
??本文內容:SSM框架的整合使用,還有bootstrap等前端框架的簡單使用,做一個簡單的新聞管理系統(tǒng)
SSM整合
前言
在前文中,我們學完了
Spring
、SpringMVC
的內容,現(xiàn)在我們就將學過的內容做一個整合使用,做一個新聞管理系統(tǒng)。
系統(tǒng)功能需求
系統(tǒng)結構設計
本系統(tǒng)根據(jù)功能的不同,項目結構可以劃分為以下幾個層次:
- 持久對象層:由若干持久化類(實體類)組成。
- 數(shù)據(jù)訪問層:曲若干DAO 接口和
Mybatis
映射文件組成。接口的名稱統(tǒng)一以DAO結尾,且MyBatis
的映射文件名稱要與接口的名稱相同。 - 業(yè)務邏輯層:該層由若干
Service
接口和實現(xiàn)類組成。在本系統(tǒng)中,業(yè)務邏輯層的接口統(tǒng)一使用Service結尾,共實現(xiàn)類名稱統(tǒng)一在接口名后加Impl。該層主要用于實現(xiàn)系統(tǒng)的業(yè)務邏輯。 - Web表現(xiàn)層:該層主要包括
SpringMVC
中的Controller類和 JSP 頁面。Controller 類主要負責攔截用戶請求,并調用業(yè)務邏拜層中相應組件的業(yè)務邏輯方法來處理用戶請求,然后將相應的結果返回給 JSP 頁面。
數(shù)據(jù)庫設計
數(shù)據(jù)庫設計比較簡單,這里我直接放建表語句
create database news;
use news;
#創(chuàng)建一個角色表
create table t_role(
roleId int primary key,
roleName varchar(20)
);
#插入兩條數(shù)據(jù)
insert into t_role values(1,'管理員'),(2,'信息員');
#創(chuàng)建一個名為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)
);
#插入一條數(shù)據(jù)
insert into t_user(userName, loginName, `password`, `status`, roleId) values("至簡","admin","abc123456","2",1);
#創(chuàng)建一個名稱為t_category的表
create table t_category(
categoryId int primary key,
categoryName varchar(20)
);
#插入四條數(shù)據(jù)
insert into t_category values(1,"今日頭條"),(2,"綜合資訊"),(3,"國內新聞"),(4,"國際新聞");
#創(chuàng)建一個名為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)
);
整合環(huán)境搭建
創(chuàng)建一個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>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>
<!--數(shù)據(jù)庫相關的依賴-->
<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>
<!-- 如果不添加此節(jié)點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下創(chuàng)建相應的文件
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"?>
<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"/>
<!--配置數(shù)據(jù)源-->
<bean id="dataSource"
class="org.apache.commons.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>
<!--事務管理器,依賴于數(shù)據(jù)源-->
<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(* com.hzy.service.*.*(..))"/>
</aop:config>
<!--配置mybatis工廠-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入數(shù)據(jù)源-->
<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="com.hzy.dao"></property>
</bean>
<!--掃描Service-->
<context:component-scan base-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>
<package name="com.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:component-scan base-package="com.hzy.controller"/>
<!--配置注解驅動-->
<mvc:annotation-driven/>
<!--配置靜態(tài)資源的訪問映射,此配置中的文件將不被前端控制器攔截-->
<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="com.hzy.interceptor.LoginInterceptor"/>-->
<mvc:interceptor>
<!--配置攔截器作用的路徑-->
<mvc:mapping path="/**"/>
<!--配置不需要攔截作用的路徑-->
<mvc:exclude-mapping path="/index.action"/>
<bean class="com.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文件的監(jiān)聽器-->
<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>
<!--系統(tǒng)默認頁面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
此時配置文件基本完成
下面是項目的目錄結構
運行結果圖
結語
以上就是今天要講的內容,主要是SSM的整合使用,因為代碼太多了,不好放在這上面,所以大家可以關注公眾號 回復 新聞就可以獲取代碼喲。文章來源:http://www.zghlxwxcb.cn/news/detail-457619.html
如果各位大哥大姐對我所寫的內容覺得還行的話,希望可以點個關注,點個收藏,您的支持就是我最大的動力,非常感謝您的閱讀(?′?`?)文章來源地址http://www.zghlxwxcb.cn/news/detail-457619.html
到了這里,關于【SSM整合】對Spring、SpringMVC、MyBatis的整合,以及Bootstrap的使用,簡單的新聞管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!