Spring+SpringMVC+Mybatis項目—企業權限管理系統(1)

JavaEE:Spring+SpringMVC+Mybatis項目—企業權限管理系統

 

首先給出項目演示地址:http://www.youngxy.top:8080/SSM/

項目架構圖:

 

一:功能需求

1.1 商品查詢 


基於SSM整合基礎上完成商品查詢,要掌握主面頁面main.jsp及商品顯示頁面product-list.jsp頁面的創建。 1.3 商品添加 
進一步鞏固SSM整合,並完成商品添加功能,要注意事務操作以及product-add.jsp頁面生成。 


1.2 訂單查詢 


訂單的查詢操作,它主要完成簡單的多表查詢操作,查詢訂單時,需要查詢出與訂單關聯的其它表中信息,所以大 家一定要了解訂單及其它表關聯關係 


1.3 訂單分頁查詢 


訂單分頁查詢,我們使用的是mybatis分頁插件PageHelper,要掌握PageHelper的基本使用。 


1.4 訂單詳情查詢 


訂單詳情是用於查詢某一個訂單的信息,這個知識點主要考覈學生對複雜的多表查詢操作的掌握。 


1.5 Spring Security 概述 


 Spring Security是 Spring 項目組中用來提供安全認證服務的框架,它的使用很複雜,我們在課程中只介紹了 spring Security的基本操作,大家要掌握spring Security框架的配置及基本的認證與授權操作。

 
1.8 用戶管理 


用戶管理中我們會介紹基於spring Security的用戶登錄、退出操作。以及用戶查詢、添加、詳情有等操作,這些功 能的練習是對前期SSM知識點的進一步鞏固。 


1.9 角色管理 


角色管理主要完成角色查詢、角色添加 。


1.10 資源權限管理 


資源權限管理主要完成查詢、添加操作,它的操作與角色管理類似,角色管理以及資源權限管理都是對權限管理的 補充。

 

1.11 權限關聯與控制 


主要會講解用戶角色關聯、角色權限關聯,這兩個操作是爲了後續我們完成授權操作的基礎,關於授權操作我們會 在服務器端及頁面端分別講解 。


1.12 AOP日誌處理 


 AOP日誌處理,我們使用spring AOP切面來完成系統級別的日誌收集。

 

二:項目架構

1.1技術選型

Spring、SpringMVC、Mybatis框架整合,這樣解決了業務層、dao層、表現層代碼的繁瑣。

在用戶登陸認證過程中,採用Spring Security框架,進行權限管理。

頁面數據展示分頁時,採用PageHelper插件管理。

後臺界面採用流行的AdminLTE模板。

 

三:前期準備

1.1數據庫

首先基於Oracle的數據庫要創建用戶,之後創建表結構。

在這裏博主給出了項目Sql的文件:點此下載。

 

1.2AdminLTE

大家可以參考官網教程,或者使用本項目的中文模板。

點此下載本項目模板。

 

 

四:環境搭建

 

1.1開發環境

創建Maven工程,採用模板創建:

然後創建不同模塊:

在工程總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.xy</groupId>
    <artifactId>SSM</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.6.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <oracle.version>11.2.0</oracle.version>
        <mybatis.version>3.4.5</mybatis.version>
        <spring.security.version>5.0.1.RELEASE</spring.security.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>compile</scope>
        </dependency>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId> <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version> </dependency>
        <dependency>
            <groupId>org.springframework</groupId> <artifactId>spring-context</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>org.springframework</groupId> <artifactId>spring-test</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-jdbc</artifactId> <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>${oracle.version}</version>
        </dependency>

    </dependencies>
    <modules>
        <module>SSM_domain</module>
        <module>SSM_dao</module>
        <module>SSM_service</module>
        <module>SSM_utils</module>
        <module>SSM_web</module>
    </modules>


</project>

 

1.2框架搭建

在web模塊下創建資源文件夾:

新建配置文件如下:

db.properties:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=ssm
jdbc.password=ylh013954
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

把相關數據庫用戶名和密碼修改成能運行。

log4j.properties:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:/java/axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

 

spring.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:aop="http://www.springframework.org/schema/aop"
       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
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 開啓註解掃描,管理service和dao -->
    <context:component-scan base-package="com.xy.service">
    </context:component-scan>
    <context:component-scan base-package="com.xy.dao">
    </context:component-scan>

    <context:property-placeholder location="classpath:db.properties"/>
    <!--spring整合mybatis-->
    <!-- 配置連接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- 把交給IOC管理 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 傳入PageHelper的插件 -->
        <property name="plugins">
            <array>
                <!-- 傳入插件的對象 -->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!-- 掃描dao接口 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xy.dao"/>
    </bean>
    <!-- 配置Spring的聲明式事務管理 -->
    <!-- 配置事務管理器 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice transaction-manager="dataSourceTransactionManager" id="txAdvice">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP增強-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xy.service.impl.*.*(..))"></aop:advisor>
    </aop:config>
</beans>

 

spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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.xsd
           http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
           ">
    <!-- 掃描controller的註解,別的不掃描 -->
    <context:component-scan base-package="com.xy.controller">
    </context:component-scan>

    <!-- 配置視圖解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- JSP文件所在的目錄 -->
        <property name="prefix" value="/pages/" />
        <!-- 文件的後綴名 -->
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 設置不過濾靜態資源-->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/img/" mapping="/img/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/plugins/" mapping="/plugins/**" />

    <!-- 開啓對SpringMVC註解的支持 -->
    <mvc:annotation-driven />
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

 

 

 

1.3插件引入

需要spring security、pageHelper,在pom.xml引入。

參考1.2

 

spring security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:security="http://www.springframework.org/schema/security"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">

	<!--開啓方法級權限控制-->
	<security:global-method-security jsr250-annotations="enabled" secured-annotations="enabled" pre-post-annotations="enabled"></security:global-method-security>
	<!-- 配置不攔截的資源 -->
	<security:http pattern="/login.jsp" security="none"/>
	<security:http pattern="/failer.jsp" security="none"/>
	<security:http pattern="/css/**" security="none"/>
	<security:http pattern="/img/**" security="none"/>
	<security:http pattern="/plugins/**" security="none"/>

	<!--
        配置具體的規則
        auto-config="true"	不用自己編寫登錄的頁面,框架提供默認登錄頁面
        use-expressions="false"	是否使用SPEL表達式(沒學習過)
    -->
	<security:http auto-config="true" use-expressions="false">
		<!-- 配置具體的攔截的規則 pattern="請求路徑的規則" access="訪問系統的人,必須有ROLE_USER的角色" -->
		<security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>

		<!-- 定義跳轉的具體的頁面 -->
		<security:form-login
				login-page="/login.jsp"
				login-processing-url="/login.do"
				default-target-url="/index.jsp"
				authentication-failure-url="/failer.jsp"
		/>

		<!-- 關閉跨域請求 -->
		<security:csrf disabled="true"/>

		<!-- 退出 -->
		<security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />

	</security:http>

	 <!--切換成數據庫中的用戶名和密碼 -->
	<!--<security:authentication-manager>-->
		<!--<security:authentication-provider user-service-ref="userService">-->
			<!--&lt;!&ndash;&lt;!&ndash; 配置加密的方式 &ndash;&gt;&ndash;&gt;-->
			<!--<security:password-encoder  ref="passwordEncoder"/>-->
		<!--</security:authentication-provider>-->
	<!--</security:authentication-manager>-->
	<!-- 配置加密類 -->
	<!--<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>-->
	<!-- 提供了入門的方式,在內存中存入用戶名和密碼-->
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>












 

 

1.4Maven創建

完成以上工作,項目初步構建成功,可以在tomcat服務器上運行看是否能通過初步測試。

 

五:代碼實現

 

5.1dao層

如圖,編寫代碼:

 

詳細代碼可以在項目源碼查看,這裏給出代碼邏輯及實現總體流程。

 

 

5.2業務層

 

5.3表現層

 

 

接下來介紹各個功能的具體實現細節:

Spring+SpringMVC+Mybatis項目—企業權限管理系統(2)

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章