SpringMvc与myBatis整合(8)
2017-08-11 14:10
429 查看
前记:这是很早之前自学学习spingMvc时的笔记,内容比较基础,适合新手,内容基本是来自网略,如有雷同,敬请谅解!
SpringMvc与myBatis整合
系统架构
第一步:整合 Dao层
Mybatis与spring整合,通过spring管理mapper接口,使用mapper的扫描器自动扫描mapper接口在spring中进行注册
第二部:整合Service接口
1、通过Spring管理Service接口,通过注解或者配置方式
2、实现事务控制
第三步:整合springmvc
不用整合(因为springMvc就是由spring提供)
整合Dao
目标:spring与mybatis进行整合SqlMapConfig.xml
这是mybatis自己的配置文件<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!—全局setting配置 根据需要加-->
<!—配置别名-->
<typeAliases>
<!—批量扫描的别名-->
<pakage name="包名" />
</typeAliases>
<!—配置mapper-->
<!—使用了spring和mybatis的整合包进行扫描,这里就不需要配置了-->
<!—但必须遵循:mapper.xml和mapper.java文件同名并且在同一个目录-->
<!—<mappers>
<mapper resource="com/njupt/pojo/User.xml" />
</mappers>--> </configuration>
applicationContext-Dao.xml(spring与mybatis整合的配置)
配置项:数据源
sqlsessionFactory
mapper扫描器
这就是一个spring配置文件
<!-- 系统缓存 -->
<bean
id="systemCache"class="com.cstor.gframe.system.cache.SystemCache"factory-method="getInstance">
<property
name="systemId"
value="${system.id}"/>
</bean>
<!-- 数据库配置 -->
<bean
id="defaultDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property
name="driverClassName"
value="${db.driverClassName}"
/>
<property
name="url"
value="${db.url}"/>
<property
name="username"
value="${db.username}"/>
<property
name="password"
value="${db.password}"/>
</bean>
<!-- sqlsessionFactory -->
<!--在spring与mybatis整合包中的类-->
<bean
id="
sqlsessionFactory"
class="org.mybatis.spring.SqSessionFactoryBean">
<!--数据库连接池(数据库)-->
<property
name="dataSource"
ref="defaultDataSource"
/>
<!--加载mybatis全局配置文件-->
<property
name="configLocation"value="classpath:ibatis/sql-map-config.xml"/>
</bean>
<!--mapper扫描器 -->
<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描的包路径,如果扫描多个用“,”隔开即可-->
<property
name="
basePackage"
value="com.cn.hnust.dao"/>
<property
name="sqlSessionFactoryBeanName"
value="sqlsessionFactory"
/>
</bean>
整合Service
让spring来管理Service接口用配置方式在spring容器中配置Service
例如创建spring的xml文件applicationContext-Service.xml来配置Service<!--在spring配置文件中配置bean -->
<bean
id="Service对象名"
class="包名.类名"
/>
事务控制--在Service整合时实现事务控制的配置
使用spring的声明式事务控制方法<!--定义一个事务管理器
对mybatis的操作数据库的事务控制,spring使用的是jdbc的事务控制
-->
<bean
id="transationManager"
class=org.springframework.jdbc.datasource.DatasourceTransationManager">
<!--数据源defaultDataSource在8.2.2已经配置-->
<property
name="dataSource"ref="defaultDataSource"
/>
</bean>
<!--配置通知
通知要给事务管理器transaction-manager -->
<tx:advice
id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--配置传播行为
REQUIRED—----必须要的
Supports--------有事务就支持事务,没有事务也不会再添加事务
-->
<tx:method
name="init*"read-only="true"
propagation="REQUIRED"
/>
<tx:method name="select*"
read-only="true"
propagation="REQUIRED"
/>
<tx:method
name="query*"read-only="true"
propagation="REQUIRED"
/>
<tx:method
name="get*"read-only="true"
propagation="REQUIRED"
/>
<tx:method
name="find*"read-only="true"
propagation="REQUIRED"
/>
</tx:attributes>
</tx:advice>
<!--配置aop
用来调用通知 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(public * com.cstor..service.*.
*(..))" />
</aop:config>
<!—除事务之外,而外的aop配置->
<bean
id="logMethod"class="包名.类名"
/>
<aop:config>
<aop:pointcut
id="allServiceAndDaoPublicMethod"
expression="(execution(public * com.cstor..dao.*.*(..)) or
execution(public * com.cstor..service.*.
*(..)))and not execution(public * com.cstor..*
.get*(..)) and not execution(public * com.csto
r..*.set*(..))"
/>
<aop:aspect
id="logMethodAdvice"
ref="logMethod"
>
<aop:around
method="logMethodTrace"
pointcut-ref="allServiceAndDaoPublicMethod"
/>
</aop:aspect>
</aop:config>
整合springMVC
1、可以创建一个xml文件:springmvc.xml2、来配置处理器映射器,处理器适配器,视图解析器,
3、配置前端控制器,
加载spring容器
要将 mapper、Service、controller都加载到spring容器中1、Mapper用了扫描器
2、Service用户声明式配置方式
3、Controller 用了组件扫描
建议用通配符的方法加载上面的多个配置文件
在web.xml中添加spring的监听器,这个监听器就加载spring的容器
<!--加载spring容器-->
<context-param>
<param-name>contextConfiglocation</param-name>
<!--spring*.xml
中的*就是通配符 -->
<param-value>classpath:spring/spring*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
建立spring-mybatis.xml配置文件
这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。spring-mybatis.xml
1.
<!-- 自动扫描 -->
2. <context:component-scan base-package="com.cn.hnust" />
3.
<!-- 引入配置文件 -->
4. <bean id="propertyConfigurer"
5.
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
6. <property name="location" value="classpath:jdbc.properties" />
7.
</bean>
8.
9.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
10. destroy-method="close">
11.
<property name="driverClassName" value="${driver}" />
12. <property name="url" value="${url}" />
13.
<property name="username" value="${username}" />
14. <property name="password" value="${password}" />
15.
<!-- 初始化连接大小 -->
16. <property name="initialSize" value="${initialSize}"></property>
17.
<!-- 连接池最大数量 -->
18. <property name="maxActive" value="${maxActive}"></property>
19.
<!-- 连接池最大空闲 -->
20. <property name="maxIdle" value="${maxIdle}"></property>
21.
<!-- 连接池最小空闲 -->
22. <property name="minIdle" value="${minIdle}"></property>
23.
<!-- 获取连接最大等待时间 -->
24. <property name="maxWait" value="${maxWait}"></property>
25.
</bean>
26.
27.
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
28. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
29.
<property name="dataSource" ref="dataSource" />
30. <!-- 自动扫描mapping.xml文件 -->
31.
<property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>
32. </bean>
33.
34. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
35.
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
36. <property name="basePackage" value="com.cn.hnust.dao" />
37.
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
38. </bean>
39.
40. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
41.
<bean id="transactionManager"
42. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
43.
<property name="dataSource" ref="dataSource" />
44. </bean>
Log4j的配置
log4j.properties
1.
log4j.rootLogger=INFO,Console,File
2. #定义日志输出目的地为控制台
3.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
4. log4j.appender.Console.Target=System.out
5.
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
6. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
7.
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
8.
9.
#文件大小到达指定尺寸的时候产生一个新的文件
10. log4j.appender.File = org.apache.log4j.RollingFileAppender
11.
#指定输出目录
12. log4j.appender.File.File = logs/ssm.log
13.
#定义文件最大大小
14. log4j.appender.File.MaxFileSize = 10MB
15.
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
16. log4j.appender.File.Threshold = ALL
17.
log4j.appender.File.layout = org.apache.log4j.PatternLayout
18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
19.
web.xml整合SpringMVC和Mybatis
1. <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css,现在http://localhost/static/foo.css-->
2.
<!-- 不拦截静态文件 -->
3. <servlet-mapping>
4.
<servlet-name>default</servlet-name>
5. <url-pattern>/js/*</url-pattern>
6.
<url-pattern>/css/*</url-pattern>
7. <url-pattern>/images/*</url-pattern>
8.
<url-pattern>/fonts/*</url-pattern>
9. </servlet-mapping>
10.
11. <!-- 配置字符集 -->
12.
<filter>
13. <filter-name>encodingFilter</filter-name>
14.
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
15. <init-param>
16.
<param-name>encoding</param-name>
17. <param-value>UTF-8</param-value>
18.
</init-param>
19. <init-param>
20.
<param-name>forceEncoding</param-name>
21. <param-value>true</param-value>
22.
</init-param>
23. </filter>
24.
<filter-mapping>
25. <filter-name>encodingFilter</filter-name>
26.
<url-pattern>/*</url-pattern>
27. </filter-mapping>
28.
29. <!-- 初始化 DispatcherServlet时,该框架在 web应用程序WEB-INF目录中寻找一个名为[servlet-名称]-servlet.xml的文件,
30.
并在那里定义相关的Beans,重写在全局中定义的任何Beans -->
31. <servlet>
32.
<servlet-name>springMybatis</servlet-name>
33. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34.
<load-on-startup>1</load-on-startup>
35. </servlet>
36.
<servlet-mapping>
37. <servlet-name>springMybatis</servlet-name>
38.
<!-- 所有的的请求,都会被DispatcherServlet处理 -->
39. <url-pattern>/</url-pattern>
40.
</servlet-mapping>
41.
42.
<context-param>
43. <param-name>contextConfigLocation</param-name>
44.
<param-value>/WEB-INF/config/spring-*.xml</param-value>
45. </context-param>
46.
<listener>
47. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
48.
</listener>
49. <!-- druid web 监控 -->
50.
<servlet>
51. <servlet-name>DruidStatView</servlet-name>
52.
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
53. </servlet>
54.
<servlet-mapping>
55. <servlet-name>DruidStatView</servlet-name>
56.
<url-pattern>/druid/*</url-pattern>
57. </servlet-mapping>
58.
59. <error-page>
60.
<error-code>404</error-code>
61. <location>/error/404.jsp</location>
62.
</error-page>
63. <error-page>
64.
<error-code>500</error-code>
65. <location>/error/500.jsp</location>
</error-page>
相关文章推荐
- springmvc,mybatis,logback整合相关
- idea 部署spring springMVC 与mybatis的整合
- 3、springmvc和mybatis整合
- 3、springmvc和mybatis整合
- springMVC、spring、mybatis整合测试
- SSM框架(一)之SSM框架整合(Spring,SpringMVC,MyBatis)
- 使用intellij idea搭建spring-springmvc-mybatis整合框架环境
- mybatis3+springmvc4+spring4整合教程
- SpringMVC MyBatis PostgreSQL整合
- 【SSM】SpringMVC、Spring(5.0.3)、MyBatis(3.4.5)三大框架的整合项目入门示例
- Spring MVC入门第2天--Spring、SpringMVC与MyBatis三大框架整合
- 使用maven 创建 springmvc整合mybatis项目 踩过的坑
- springmvc mybatis整合
- SpringMVC和Mybatis的整合(配置部分)
- SpringMVC整合mybatis中我们希望看到操作数据库的语句通过日志进行打印
- java后台框架 springmvc整合mybatis框架源码 bootstrap html5 mysql oracle
- spring、springmvc、mybatis整合笔记
- 在配置springmvc整合MyBatis中遇到的各种问题
- springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven SSM
- springmvc 和 mybatis 整合时因缺少jar包引起的错误