对最近练习的一个SSM整合项目进行整体总结,本总结分为三部分,分别是(一)(二)(三)
本项目使用的编译器是IntelliJ IDEA,使用的数据库是oracle,数据库管理工具是PLSQL Developer
注:本项目开发过程中没有在本地安装Tomcat服务器,只是在web模块下的pom.xml中配置了一个tomcat7的插件,然后在idea中配置服务器的地方配置了如下信息:名称(heima-ssm-web)、运行的工程是哪个模块(ssm_web)、运行的命令(tomcat7:run)。在pom.xml中的配置如下:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration><port>8888</port></configuration>
<version>2.2</version>
</plugin>
SSM项目企业权限管理系统
一. 项目中类的结构
数据库的表名 | 对应domain | 对应dao | 对应service的接口 | 对应service的实现类 | 对应controller |
---|---|---|---|---|---|
产品Product | Product | IProductDao | IProductService | ProductServiceImpl | ProductController |
订单Orders | Orders | IOrdersDao | IOrdersService | OrdersServiceImpl | OrdersController |
会员Member | Member | IMemberDao | – | – | – |
游客Traveller | Traveller | TravellerDao | – | – | – |
用户Users | UserInfo | IUserDao | IUserService | UserServiceImpl | UserController |
角色Role | Role | IRoleDao | IRoleService | RoleServiceImpl | RoleController |
权限Permission | Permission | IPermissionDao | IPermissionService | PermissionServiceImpl | PermissionController |
系统日志sysLog | SysLog | ISysLogDao | ISysLogService | SysLogServiceImpl | SysLogController(这里的控制器只用来查询所有日志,以在前端页面上显示,真正生成日志内容的是LogAop类) |
采用的是LogAop(不是控制器,只是一个切面,因此注解为@Component<代表是一个Bean>和@Aspect<代表是一个切面>)
中间表:order_traveller、users_role、role_permission
二.配置文件
(一)applicationContext.xml
1.配置扫描dao和service
<context:component-scan base-package="com.itheima.ssm.service"></context:component-scan>
<context:component-scan base-package="com.itheima.ssm.dao"></context:component-scan>
2.spring整合mybatis
配置连接池、交给IOC管理SqlSessionFactory(包含传入PageHelper插件)、扫描dao接口
<!--表明存放数据库信息的文件位置-->
<context:property-placeholder location="classpath:db.properties">
</context:property-placeholder>
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<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">oracle</prop>
<!--分页合理化参数,当pageNum<=0时会查询第一页,pageNum超过总页数时会查询最后一页-->
<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.itheima.ssm.dao"/>
</bean>
要点:注意db.properties文件中本地IP的获取:命令行输入ipconfig,找到正确的本地ip
3.配置事务管理
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
(二)spring-mvc.xml
1.配置扫描web
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="com.itheima.ssm.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/>
(三)web.xml
配置前端控制器、配置springmvc编码过滤器、配置springsecurity过滤器、配置加载类路径的配置文件、配置监听器
<!--前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数,创建完DispatchServlet对象,加载springmvc.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--服务器启动的时候,让DispatchServlet对象创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置 springMVC 编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置springsecurity的filter-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置监听器,监听request域对象的创建和销毁的-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
(四)spring-security.xml
配置不拦截的资源、配置采用何种方式对方法进行权限控制(JSR-250注解、@Secured注解、支持表达式的注解)、配置具体规则(包括是否使用默认登录界面、具体路径下的文件具有何种权限才能访问、定义不同情况下跳转的界面等)、配置切换成数据库中的用户名和密码(配置加密方式)
<!--
配置具体的规则
auto-config="true" 不用自己编写登录的界面,框架提供默认登录页面
user-expressions="false" 是否使用SPEL表达式(没学习过)
-->
<!--下面user-expressions设为true是因为在jsp页面中引用了hasRole,所以具体的拦截规则中的access由"ROLE_USER,ROLE_ADMIN"变成下方的"hasAnyRole('ROLE_USER','ROLE_ADMIN')"-->
<security:http auto-config="true" use-expressions="true">
<!--配置具体的拦截规则,pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER或ROLE_ADMIN的角色"-->
<!--这里因为上方设置use-expression为true,表示使用表达式,所以access="这里使用表达式",如果设置不使用表达式,则access="ROLE_USER,ROLE_ADMIN"-->
<security:intercept-url pattern="/**" access="hasAnyRole('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"
authentication-success-forward-url="/index.jsp"
/>
<!--关闭跨域请求-->
<security:csrf disabled="true"/>
<!--退出-->
<security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp"/>
</security:http>
<!-- 切换成数据库中的用户名和密码 -->
<security:authentication-manager>
<!--下面的user-service-ref的值是在UserServiceImpl的@Service注解中标出的名字,这里是“userService”-->
<security:authentication-provider user-service-ref="userService">
<!-- 配置加密的方式 -->
<security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
</security:authentication-manager>
<!--配置加密类-->
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
(五)log4j.properties
日志配置文件,直接复制
后接总结(二)