服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - springmvc mybatis集成配置示例

springmvc mybatis集成配置示例

2020-06-15 12:27路之石 JAVA教程

本文主要介绍springmvc+mybatis集成配置,这里提供了实例代码,和简单说明,有需要的小伙伴可以参考下

简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求。闲暇时间把项目配置文件共享出来,供大家参看:

1.首先我们来看下依赖的pom:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!-- spring -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</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-tx</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-jdbc</artifactId>
  <version>${spring.version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring.version}</version>
  <scope>test</scope>
 </dependency>
 
 <!-- mybatis 包 -->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.2.8</version>
 </dependency>
 
 <!--mybatis spring 插件 -->
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.2.2</version>
 </dependency>
 
 <!-- mysql连接 -->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
 </dependency>
 
 <!-- 数据源 -->
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.12</version>
 </dependency>
 
 <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.4</version>
 </dependency>
 
 <!-- log4j -->
 <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
 </dependency>
 
 <!-- servlet -->
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>3.0-alpha-1</version>
 </dependency>
 
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
 </dependency>
 
 <!-- json -->
 <dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
 </dependency>
 
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.3</version>
 </dependency>
 
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
 </dependency>
 <!-- 文件上传 -->
 <dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
 </dependency>
 
 <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.2.2</version>
 </dependency>

spring 选用的是4.1.4的版本,根据系统需要我们可以选择自己适合的版本。

2.相关的配置文件:

    a)spring.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 
 <!--引入配置属性文件 -->
 <context:property-placeholder location="classpath:config.properties" />
 
 <!--自动扫描含有@Service将其注入为bean -->
 <context:component-scan base-package="com.demo.report.web.service" />

    b)spring-mvc.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.1.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
 
 <!-- 自动扫描controller包下的所有类,如果@Controller注入为bean -->
 <context:component-scan base-package="com.demo.report.web.controller" />
 
 <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
 <bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 <property name="supportedMediaTypes">
  <list>
  <value>text/html;charset=UTF-8</value>
  </list>
 </property>
 </bean>
 
 <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
 <bean
 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
 <property name="messageConverters">
  <list>
  <!-- json转换器 -->
  <ref bean="mappingJacksonHttpMessageConverter" />
  </list>
 </property>
 </bean>
 
 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
 <bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass"
  value="org.springframework.web.servlet.view.JstlView" />
 <property name="prefix" value="" />
 <property name="suffix" value="" />
 </bean>
 
 <!-- 配置多文件上传
 <bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="defaultEncoding">
  <value>UTF-8</value>
 </property>
 <property name="maxUploadSize">
  <value>32505856</value>
 </property>
 <property name="maxInMemorySize">
  <value>4096</value>
 </property>
 </bean>-->
 
</beans>

  c)spring-mybatis.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
 ">
 
 <!-- 配置数据源 使用的是Druid数据源 -->
 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
 init-method="init" destroy-method="close">
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
 
 <!-- 初始化连接大小 -->
 <property name="initialSize" value="0" />
 <!-- 连接池最大使用连接数量 -->
 <property name="maxActive" value="20" />
 
 <!-- 连接池最小空闲 -->
 <property name="minIdle" value="0" />
 <!-- 获取连接最大等待时间 -->
 <property name="maxWait" value="60000" />
 <property name="poolPreparedStatements" value="true" />
 <property name="maxPoolPreparedStatementPerConnectionSize"
  value="33" />
 <!-- 用来检测有效sql -->
 <property name="validationQuery" value="${validationQuery}" />
 <property name="testOnBorrow" value="false" />
 <property name="testOnReturn" value="false" />
 <property name="testWhileIdle" value="true" />
 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
 <property name="timeBetweenEvictionRunsMillis" value="60000" />
 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
 <property name="minEvictableIdleTimeMillis" value="25200000" />
 <!-- 打开removeAbandoned功能 -->
 <property name="removeAbandoned" value="true" />
 <!-- 1800秒,也就是30分钟 -->
 <property name="removeAbandonedTimeout" value="1800" />
 <!-- 关闭abanded连接时输出错误日志 -->
 <property name="logAbandoned" value="true" />
 <!-- 监控数据库 -->
 <property name="filters" value="mergeStat" />
 </bean>
 
 <!-- myBatis文件 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
 <property name="mapperLocations" value="classpath:com/demo/report/web/mapper/*.xml" />
 </bean>
 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.feidai.report.web.mapper" />
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 </bean>
 
 <!-- 配置事务管理器 -->
 <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 </bean>

    d)web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<display-name>springmvc_mybatis_demo</display-name>
 
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
 </context-param>
 
 <filter>
 <filter-name>encodingFilter</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>
 <init-param>
  <param-name>forceEncoding</param-name>
  <param-value>true</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!-- 防止spring内存溢出监听器 -->
 <listener>
 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
 </listener>
 
 <servlet>
 <description>spring mvc servlet</description>
 <servlet-name>rest</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  classpath:spring-mvc.xml
  </param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>rest</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <servlet>
 <servlet-name>DruidStatView</servlet-name>
 <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>DruidStatView</servlet-name>
 <url-pattern>/druid/*</url-pattern>
 </servlet-mapping>
 
 <!-- 配置session超时时间,单位分钟 -->
 <session-config>
 <session-timeout>30</session-timeout>
 </session-config>
 
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

使用了druid的数据源,在web中的详细配置可以参看代码。

 以上就是对springmvc mybatis集成配置的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

延伸 · 阅读

精彩推荐