SSM框架实战总结

遇到的问题和解决方法在这里

首先,数据库问题

使用的是navicat for mysql,要注意的就是多个表时,主键和外键的设置。
注意:在命令行中使用sql语句,一定要以分号结尾,分号才是一个命令结束的标志,中间可以随意空格、注释。

相关配置文件

db.properties

简单的记载数据库连接所需要的数据
问题一:报错:不建议在没有服务器身份验证的情况下建立SSL连接
答:貌似是早些版本的问题,通过在properties的url后加“?useSSL=false”来禁用SSL连接即可。MySQL 5.5.45+, 5.6.26+ and 5.7.6+ 版本才会遇到的问题。
SSL证书:用于在保证数据在服务器和客户端之间进行传输时的安全性,防止信息泄漏。

applicationContext

内容:读取db.properties,配置数据源(数据库相关设置和连接),事务管理器、驱动,通知、切面、mybatis工厂,mapper扫描器,service扫描器
可拆解成多个appCtx文件,在web.xml扫描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:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    			http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-4.2.xsd 
				http://www.springframework.org/schema/tx 
				http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
				http://www.springframework.org/schema/aop 
				http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
				http://www.springframework.org/schema/mvc
				http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
				
	<!-- 读取db.properties=============================================== -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置数据源====================================================== -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<!-- 数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!-- 连接数据库的url -->
		<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>
	
	
	<!-- 注册事务管理器驱动,开启事务注解============================================ -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<!-- 配置事务管理器,依赖于数据源 -->
	<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="delete*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="create*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="find*" 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.ssm.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="com.ssm.dao"></property>
	</bean>
	
	<!-- 扫描Service=================================================================== -->
	<context:component-scan base-package="com.ssm.service" />
	
	
</beans>

问题二:头文件的具体含义(未解决)

mybatis-config

内容:扫描po包
问题三:<!DOCTYPE…>的含义(未解决)

<?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.ssm.po"/>
	</typeAliases>
</configuration>

springmvc-config

内容:注解驱动,扫描controller包,视图解析器,静态资源访问(css.js.images),拦截器
视图解析器,前缀要记得在WEB-INF前加“/”
贴代码,留用

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


		<!-- 扫描指定的包,并开启注解驱动=============================================== -->
		<context:component-scan base-package="com.ssm.web.controller" />
		<mvc:annotation-driven />
		
		<!-- 配置静态资源的访问映射=================================================== -->
		<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
		<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
		<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
		
		<!-- 配置视图试图解析器======================================================== -->
		<bean 	id="viewResolver"
				class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/"/>
			<property name="suffix" value=".jsp" />
		</bean>
		
		<!-- 配置拦截器=============================================================== -->
		<mvc:interceptors>
			<mvc:interceptor>
				<!-- 拦截 -->
				<mvc:mapping path="/**"/>
				<!-- 不拦截 -->
				<mvc:exclude-mapping path="/index.action"/>
				<mvc:exclude-mapping path="/findNewsByCategoryIdPage.action"/>
				<mvc:exclude-mapping path="/findFrontNewsByNewsId.action"/>
				
				<!-- 拦截器位置 -->
				<bean class="com.ssm.interceptor.LoginInterceptor"/>
			</mvc:interceptor>
		</mvc:interceptors>
		
		

</beans>

web.xml

内容:加载spring文件的监听器,编码过滤器,前端控制器
注意包名是否写对

贴代码,留着用

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app.xsd"
		id="WebApp_ID" version="3.0">
	
	<!-- 配置加载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>
	
	<servlet>
		<!-- 配置前端控制器================================================================= -->
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 若没有标注init-param,加载时则会自动去web-inf下寻找命名方式为: -->
			<!-- servletName-servlet.xml 的配置文件 -->
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<!-- 如果没有这一句,则会在第一个servlet请求时加载 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 配置服务器启动时立即加载spring mvc配置文件================================================== -->
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	
	<!-- 系统默认页面================================================================================== -->
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
	
</web-app>

整合篇

SSM框架分为三层
数据访问层:po、dao接口、mapper文件
业务逻辑层:service接口、serviceimpl、controller、拦截器
表现层:配合业务层实现相关功能的前端文件

用户系统和新闻系统

1、po类

成员和get、set、toString方法

2、dao类接口和映射文件

dao类中写抽象的方法,映射文件中写每个dao类中方法实现的具体sql语句

3、service接口与serviceimpl

也就是业务逻辑层,调用dao的方法
因为前端的一个请求有时候可能是多个dao类方法的综合,所以在service层将这些方法整合成其他的方法

4、controller类

主要是调用,调用service层的方法,响应前端的请求,做出页面的跳转,数据的增删改查等功能

5、jsp文件

表现层,也叫web层。
和后端进行数据交互,配合controller的方法来实现想过操作,很复杂的感觉。

6、拦截器

根据权限拦截客户端的请求,检查seesion,做出判断。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值