专栏首页程序员的时光cms项目系列(一)——SSM框架搭建

cms项目系列(一)——SSM框架搭建

写在前面:

小伙伴儿们,大家好!今天给大家分享一个SSM项目实战系列——cms系统; 第一篇是SSM框架搭建,后续会持续更新,敬请期待!

思维导图:

1,配置文件引入

1.1,创建Maven项目,添加依赖;

pom.xml文件:

<dependencies>
    <!-- 添加Servlet支持 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
    </dependency>

    <!-- 添加jtl支持 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- 添加Spring支持 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.3</version>
    </dependency>

    <!-- 添加日志支持 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.12</version>
    </dependency>

    <!-- 添加mybatis支持 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.3.0</version>
    </dependency>

    <!-- jdbc驱动包  -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
    </dependency>

    <!-- 添加shiro支持 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-core</artifactId>
      <version>1.2.4</version>
    </dependency>

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-web</artifactId>
      <version>1.2.4</version>
    </dependency>

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.2.4</version>
    </dependency>

    <!-- 添加百度编辑器ueditor支持 -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!-- apache公共包 -->
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.10</version>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.5</version>
    </dependency>

    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.0</version>
    </dependency>

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.1</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>

    <dependency>
      <groupId>net.sf.ezmorph</groupId>
      <artifactId>ezmorph</artifactId>
      <version>1.0.6</version>
    </dependency>

    <!-- 添加连接池druid支持 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.16</version>
    </dependency>
  </dependencies>

这里有的jar包还需要我们手动导入:

导入之后记得添加jar包:

1.2,Resources资源文件;

我们在main目录下新建两个文件夹,分别是java和resources,并把cms标记为Sources目录,resources标记为Resources目录;

导入5个配置文件:分别是Spring,SpringMVC,MyBatis和日志相关的配置文件;

ManagerMapper.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.cms.dao.ManagerDao">

 <resultMap type="Manager" id="ManagerResult">
  <result property="id" column="id"/>
  <result property="userName" column="userName"/>
  <result property="password" column="password"/>
 </resultMap>

</mapper>

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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
        

 <!-- 配置数据源 -->
 <bean id="dataSource"
  class="com.alibaba.druid.pool.DruidDataSource">
  <property name="url" value="jdbc:mysql://localhost:3306/db_cms?useUnicode=true&amp;characterEncoding=UTF-8"/>
  <property name="username" value="root"/>
  <property name="password" value="123456"/>
 </bean>

 <!-- 配置mybatis的sqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <!-- 自动扫描mappers.xml文件 -->
  <property name="mapperLocations" value="classpath:com/java/mappers/*.xml"></property>
  <!-- mybatis配置文件 -->
  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 </bean>

 <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.java.dao" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 </bean>

 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 
 <!-- 自定义Realm -->
 <bean id="myRealm" class="com.java.realm.MyRealm"/>  
 
 <!-- 安全管理器 -->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
     <property name="realm" ref="myRealm"/>  
 </bean>  
 
 <!-- Shiro过滤器 -->
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
     <!-- Shiro的核心安全接口,这个属性是必须的 -->  
     <property name="securityManager" ref="securityManager"/>
     <!-- 身份认证失败,则跳转到登录页面的配置 -->  
     <property name="loginUrl" value="/login.jsp"/> 
     <!-- Shiro连接约束配置,即过滤链的定义 -->  
     <property name="filterChainDefinitions">  
         <value>
    /login=anon
    /admin/**=authc
         </value>  
     </property>
 </bean>  
 
 <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>  
 
 <!-- 开启Shiro注解 -->
 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>  
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
     <property name="securityManager" ref="securityManager"/>  
    </bean>  
  
 <!-- 配置事务通知属性 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <!-- 定义事务传播属性 -->  
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="check*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>  
  
    <!-- 配置事务切面 -->  
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.java.service.*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  
    
   <!-- 自动扫描 -->
 <context:component-scan base-package="com.java.service" />
</beans>

log4j.properties:

log4j.rootLogger=DEBUG, Console
  
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
  
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

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.java.entity"/>
 </typeAliases>
</configuration>

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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/mvc   http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    

 
 <mvc:annotation-driven/>
 
 <mvc:resources mapping="/static/**" location="/static/"/>

 <!-- 视图解析器 -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/" />
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  
  <property name="defaultEncoding" value="UTF-8"/>  
     <property name="maxUploadSize" value="10000000"/>

 </bean>

 <!-- 使用注解的包,包括子集 -->
 <context:component-scan base-package="com.java.controller" />
</beans>  

2,创建数据库;

创建数据库db_cms,并加上一张表t_manager:

我们添加一条数据进去即可;

3,项目结构搭建;

dao数据访问层:ManagerDao接口:

package com.cms.dao;

/**
 * 管理员Dao接口
 * @author user
 *
 */
public interface ManagerDao {

}

这里要对应上文的ManagerMapper.xml文件,形成映射,这是MyBatis核心文件;

entity实体层:Manager实体类:

package com.cms.entity;

/**
 * 管理员实体
 * @author user
 *
 */
public class Manager {

    private Integer id; // 编号
    private String userName; // 用户名
    private String password; // 密码

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

这里就是一个与数据库表t_manager相对应的实体类;还有与mybatis-config.xml相对应,里面的别名就是实体层;

service事务层:ManagerService接口:

package com.cms.service;

/**
 * 管理员Service接口
 * @author user
 *
 */
public interface ManagerService {

}

ManagerServiceImpl实现类:

package com.cms.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cms.dao.ManagerDao;
import com.cms.service.ManagerService;

/**
 * 管理员Service实现类
 * @author user
 *
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService{

 @Resource
 private ManagerDao managerDao;
}

这属于事务层,通过继承具体实现该类;

controller控制层:ManagerController类:

package com.cms.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.service.ManagerService;

/**
 * 管理员Controller层
 * @author user
 *
 */
@Controller
@RequestMapping("/manager2")
public class ManagerController {

    @Resource
    private ManagerService managerService;
}

具体的控制层,先搭个框架;

Shiro认证层:自定义MyRealm类:

package com.cms.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义Reaml
 * @author user
 *
 */
public class MyRealm extends AuthorizingRealm{

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // TODO Auto-generated method stub
        return null;
    }

}

4,框架测试;

我们在配置完tomcat环境之后,将cms项目导入进去,运行即可;

当运行出现index.jsp文件的内容时,表示SSM框架已经搭建成功了!

本文分享自微信公众号 - 程序员的时光(gh_9211ec727426),作者:时光太瘦哇

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-07-08

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 深入浅出SpringMVC系列~

    首先我们看一下项目架构吧!我们新建一个controller类,Spring的配置文件等;

    用户7544680
  • 漫画趣解——栈

    用户7544680
  • SpringBoot原理?属性配置?在这里

    找到pom.xml中的spring-boot-starter-parent,Crtl+点击跳转到了spring-boot-starter-parent的pom....

    用户7544680
  • SSM 单体框架 - 教育平台后台管理系统:Maven 进阶使用

    教育后台管理系统是提供给相关业务人员使用的一个后台管理系统,业务人员可以在这个后台管理系统中,对课程信息、广告信息、用户信息、 权限信息等数据进行维护。

    RendaZhang
  • 记录一个pom文件

    用户1225216
  • [有人@我]SSH框架整合教程

    SSH:Struts2+Spring+Hibernate整合的web应用程序开源框架。

    南风
  • 【Maven篇】---解决Maven线上部署java.lang.ClassNotFoundException和no main manifest attribute解决方法

    maven 线上部署的话会出现一些问题比如java.lang.ClassNotFoundException或者no main manifest attribut...

    LhWorld哥陪你聊算法
  • springmvc

    MVC 是一种架构思想,指的是model(模型)-view(视图)-controller(控制器),spring mvc将各层解耦,每一层都负责对应的功能和职责...

    逍遥壮士
  • maven教程5(聚合工程)

      所谓聚合项目,实际上就是对项目分模块,互联网项目一般来说按照业务分(订单模块、VIP模块、支付模块、CMS模块…),传统的软件项目,大多采用分层的方式(Da...

    用户4919348
  • 如何基于SSM框架,快速搭建maven后台项目?

    写在前面:2020年面试必备的Java后端进阶面试题总结了一份复习指南在Github上,内容详细,图文并茂,有需要学习的朋友可以Star一下! GitHub地...

    用户5546570

扫码关注云+社区

领取腾讯云代金券