分包
编写配置文件:
ssm整合第一步: 根据dao开发先编写sqlSessionFactory mybatied的配置文件
spring将要管理dao,controller,service 等 所以开始配置spring配置文件(配置为了使用di和aop)
<? xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:mybatis = "http://mybatis.org/schema/mybatis-spring" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p = "http://www.springframework.org/schema/p" xmlns:context = "http://www.springframework.org/schema/context"
xmlns:mvc = "http://www.springframework.org/schema/mvc" xmlns:tx = "http://www.springframework.org/schema/tx"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://mybatis.org/schema/mybatis-spring
</beans>引入spring规约
<context:property-placeholder location = "classpath:jdbc.properties" /> 加载我们位于classpath:下面的jdbc的配置文件cclasspath:只有在spring的配置文件中才使用
<!-- 数据源 -->
<bean id = "dataSource" class = "org.apache.commons.dbcp.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}" />
</bean>
配置数据源: 数据源,数据的来源,保存了我们jdbc的链接,我们的mybaties通过数据源来访问我们的数据库,而且使用数据源的好处可以使用完后放回数据池中我使用的是DB2在这里并没有配置
<!-- mybaties和spring整合 SqlSessionFactoryBean -->
<bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
<property name = "dataSource" ref = "dataSource" />
<!-- mybaties的全局配置文件 -->
<property name = "configLocation" value = "classpath:mybaties/sqlMapConfig.xml" />
<property name = "plugins" >
<array>
<bean class = "com.github.pagehelper.PageInterceptor" >
<!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
<property name = "properties" >
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
当mybaties与spring整合的时候我们会导入
现在就不需要我们来管理mybaities了,所以我们要告诉spring 我们配置的mybaties在哪里 或者在运行时候需要哪些参数等..
使用sqlSessionFactopryBean 使用该工厂初始化我们的mybaties配置
一般我们需要dataSource 告诉spring配置的mybaties工厂Bean我们用的哪个数据库,还有就是我们没整合spring时候
我们会自己加载我们的mybaties.xml的配置信息我们也要告诉他
下面的配置
是用于pageHelper的配置
我们的mybaties还缺少一个核心的东西,那就是我们为mybaties写的mapper.xml中sql还没有告诉他,所以我们还要配置
<!-- mapper扫描 -->
<bean id = "mapperScan" class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name = "basePackage" value = "cn.nsu.ssm.nsu12306.mapper" />
<property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" />
</bean>
在这里
我们需要配置basePackage 告诉mapper的位置 但是这样并不能为我们生成代理对象
我们生成代理对象是靠的sqlSessionFactory 所以我们还要告诉我们的mapperScannerConfigurer对象我们的sqlSessionFactory是谁 ,所以我们有了sqlSessionFavtoryBeanName 注意这是一个value不是ref,
value指的就是我们在配置spring管理但是SqlSessionFavtoryBean的id .好了 我们的mybaties的配置信息终于配置完了。
现在我们就应该来配置我们的业务层了。
<? xml version = "1.0" encoding = "UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:mybatis = "http://mybatis.org/schema/mybatis-spring"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p = "http://www.springframework.org/schema/p"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:mvc = "http://www.springframework.org/schema/mvc"
xmlns:tx = "http://www.springframework.org/schema/tx"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd " >
<!-- 用户服务类 -->
<bean id = "userService" class = "cn.nsu.ssm.nsu12306.service.impl.IUserServiceImpl" ></bean>
<!-- 管理員服务类 -->
<bean id = "adminService" class = "cn.nsu.ssm.nsu12306.service.impl.AdminServiceImpl" > </bean>
<!-- 火车服务类 -->
<bean id = "iTrainService" class = "cn.nsu.ssm.nsu12306.service.impl.ITrainServiceImpl" ></bean>
<!-- 日志服务类 -->
<bean id = "iLogService" class = "cn.nsu.ssm.nsu12306.service.impl.ILogServiceImpl" ></bean>
<!-- 订单服务类 -->
<bean id = "orderService" class = "cn.nsu.ssm.nsu12306.service.impl.OrderServiceImpl" ></bean>
<!-- 用户联系服务类 -->
<bean id = "concatService" class = "cn.nsu.ssm.nsu12306.service.impl.IContactServiceImpl" ></bean>
<!-- 车站管理服务类 -->
<bean id = "cityService" class = "cn.nsu.ssm.nsu12306.service.impl.ICityServiceImpl" ></bean>
</beans>
我采用了声明式 没有采用注解 还有之前听老师说 我们bean中的字段对于spring来说就是setXXX 这也是为什么spring就有两种 设置和构造
我们service配置完了但是我们的事物还没有配置 以前我们@Transactional 这样很方便但是 我们如果想规范我们的方法命名我们应该写成声明的方式
<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<!-- jdbc的事务管理器dataSourceTransactionManager -->
<property name = "dataSource" ref = "dataSource" />
</bean>
由于mybaties是对jdbc的轻量的封装所以我们使用的是jdbc的事物管理器
事物 acid 原子性,一致性,隔离性,持久性 如果不考虑事物 1:脏读 2:不可重复读 3:虚度update欢度Inert
好了回到正题我们配置了事务管理器但是事务管理器并不知道要管理谁,所以事务管理有个字段dataSource 我要告诉它我们用的哪个数据库
配置完了事物管理器我们要在哪些地方使用呢? 当然是我们操作数据库的时候对于crud,我们在查的时候没有必要添加事物,对于
spring来说 他最伟大的di我们早就使用了 现在就是aop的时候了,以前我们总是纵向的来使用我们要的功能,现在他是横向的切入了一个方法,
复习一下 aop 面向切面编程,是面向对象的扩充,但不能取代面向对象,面向切面从字面意思来说,切面 我们要插入的方法是哪里,
我们插入后 我们要执行的代码是什么?(执行的代码就是advice 通知) 切点就是pointcut 切面就是 切点+增强代码 aspect就是多个切点和增强
<tx:advice id = "txAdvice" transaction-manager = "transactionManager" >
<tx:attributes>
<!-- 传播行为 -->
<tx:method name = "save*" propagation = "REQUIRED" />
<tx:method name = "insert*" propagation = "REQUIRED" />
<tx:method name = "get*" propagation = "SUPPORTS" read-only = "true" />
<tx:method name = "find*" propagation = "SUPPORTS" read-only = "true" />
<tx:method name = "update*" propagation = "REQUIRED" />
<tx:method name = "delete*" propagation = "REQUIRED" />
</tx:attributes>
</tx:advice>
配置事务 在方法名为save*insert*.... propagation传播行为必须活着支持 read-onl 读的时候不会再改方法添加事物
现在我们就要配置我们的切面了
<aop:config>
advice-ref 我们定义的增强 poincut 切点 我们要在哪里执行他是aspectj的表达式
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.nsu.ssm.nsu12306.service.impl.*.*(..))"/>
</aop:config>
例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..))
execution(* cn.nsu.ssm.nsu12306.service.impl.*.*(..))
execution()是最常用的切点函数,其语法如下所示:
整个表达式可以分为五个部分:
1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,
com.nsu.12306.service.impl包、子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。
5、第三个*号 该类下的所有方法名
5、(..)表示方法的参数,两个句点表示任何参数。
这样我们的dao service 就缺web啦!!
这是我web的包比起和struts整合时爽多了,自己用自己的
定义配置文件
<? 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:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
</beans>
老方式想要他(spring)知道他的命名空间,避免和别的配置文件起冲突
<!-- 扫描注解 -->
<context:component-scan base-package = "cn.nsu.ssm.nsu12306.controller,cn.nsu.ssm.nsu12306.adminComtroller" />
会自动扫描该包下加了@controller的Pojo类
<mvc:annotation-driven conversion-service = "stringTodateConverter" /> 会自动生成
一个是映射处理器
一个是控制处理器 调用我们映射处理器对应的方法
还却少一个那就是试图解析器
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
现在我们都配置好了 但是我们还不能运行我们要在web.xml中配置 我们要告诉tomacat我们用了哪些东西 我们所有的框架入口都是在web.xml 像struts的时候
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value> /WEB-INF/classes/spring/applicationContext-*.xml,/WEB-INF/classes/spring/applicationContext.xml </param-value>
</context-param>
配置我们spring容器所需要的 初始化我们容器的 bean
<servlet>
<servlet-name> springMvc-user </servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name> contextConfigLocation </param-name>
<param-value> classpath:spring/springMvc-*.xml </param-value>
</init-param>
<load-on-startup> 3 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> springMvc-user </servlet-name>
<url-pattern> / </url-pattern>
</servlet-mapping>
配置我们的springMvc的配置信息现在是一个servlet了不再像struts的filter
<!-- POST中文编码过滤器 -->
<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>
配置Post的中文乱码问题
由于我们的springMvc拦截/ 像我们的img或者js或者css的他都会拦截所以我们要告诉它别拦截我们的静态资源
<!-- 对静态资源不拦截 -->
<servlet-mapping>
<servlet-name> default </servlet-name>
<url-pattern> *.js </url-pattern>
<url-pattern> *.css </url-pattern>
<url-pattern> /images/* </url-pattern>
</servlet-mapping>
上面是在web.xml 下面是在我们的配置文件
springMvc.xml中配置
<mvc:default-servlet-handler/>
到此我们的环境完成了; 。