SSM中(Spring-SpringMVC-Mybatis)(二:整合)

  1. 云栖社区>
  2. 博客>
  3. 正文

SSM中(Spring-SpringMVC-Mybatis)(二:整合)

曾将 2018-01-08 19:39:00 浏览1128
展开阅读全文

使用Maven整合一个SSM,当然要配有Maven环境,Maven的环境配置自行百度,挺好配的

创建一个Maven项目以后,目录结构如图所示:

 

然后就是各部分配置:

  首先pom导入依赖jar包:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>com.57.DXYHospital</groupId>
  5   <artifactId>demo1</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>DXYHospital Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <dependencies>
 11       <!-- Json依赖 -->
 12 <!-- https://mvnrepository.com/artifact/org.json/json -->
 13 <dependency>
 14 <groupId>com.fasterxml.jackson.core</groupId>
 15 <artifactId>jackson-core</artifactId>
 16 <version>2.8.0</version>
 17 </dependency>
 18 <dependency>
 19 <groupId>com.fasterxml.jackson.core</groupId>
 20 <artifactId>jackson-annotations</artifactId>
 21 <version>2.8.0</version>
 22 </dependency>
 23 <dependency>
 24 <groupId>com.fasterxml.jackson.core</groupId>
 25 <artifactId>jackson-databind</artifactId>
 26 <version>2.8.0</version>
 27 </dependency>
 28 
 29     <dependency>
 30         <groupId>org.json</groupId>
 31         <artifactId>json</artifactId>
 32         <version>20160810</version>
 33     </dependency>
 34     
 35 <dependency>
 36     <groupId>com.alibaba</groupId>
 37     <artifactId>fastjson</artifactId>
 38     <version>1.2.39</version>
 39 </dependency>
 40 <!-- maven fonts 字体 -->
 41 <!-- shiro相关依赖 -->
 42         <dependency>
 43             <groupId>org.apache.shiro</groupId>
 44             <artifactId>shiro-core</artifactId>
 45             <version>1.2.2</version>
 46         </dependency>
 47         <dependency>
 48             <groupId>org.apache.shiro</groupId>
 49             <artifactId>shiro-ehcache</artifactId>
 50             <version>1.2.2</version>
 51         </dependency>
 52         <dependency>
 53             <groupId>org.apache.shiro</groupId>
 54             <artifactId>shiro-web</artifactId>
 55             <version>1.2.2</version>
 56         </dependency>
 57         <dependency>
 58             <groupId>org.apache.shiro</groupId>
 59             <artifactId>shiro-quartz</artifactId>
 60             <version>1.2.2</version>
 61         </dependency>
 62         <dependency>
 63             <groupId>org.apache.shiro</groupId>
 64             <artifactId>shiro-spring</artifactId>
 65             <version>1.2.2</version>
 66         </dependency>
 67         <dependency>
 68             <groupId>mysql</groupId>
 69             <artifactId>mysql-connector-java</artifactId>
 70             <version>5.1.25</version>
 71         </dependency>
 72         <dependency>
 73             <groupId>com.alibaba</groupId>
 74             <artifactId>druid</artifactId>
 75             <version>0.2.23</version>
 76         </dependency>
 77  <!-- 单元测试 -->
 78         <dependency>
 79             <groupId>junit</groupId>
 80             <artifactId>junit</artifactId>
 81             <version>4.10</version>
 82             <scope>test</scope>
 83         </dependency>
 84 
 85         <!-- Spring 相关依赖 -->
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-context</artifactId>
 89             <version>4.1.3.RELEASE</version>
 90         </dependency>
 91         <dependency>
 92             <groupId>org.springframework</groupId>
 93             <artifactId>spring-beans</artifactId>
 94             <version>4.1.3.RELEASE</version>
 95         </dependency>
 96         <dependency>
 97             <groupId>org.springframework</groupId>
 98             <artifactId>spring-webmvc</artifactId>
 99             <version>4.1.3.RELEASE</version>
100         </dependency>
101         <dependency>
102             <groupId>org.springframework</groupId>
103             <artifactId>spring-jdbc</artifactId>
104             <version>4.1.3.RELEASE</version>
105         </dependency>
106         <dependency>
107             <groupId>org.springframework</groupId>
108             <artifactId>spring-aspects</artifactId>
109             <version>4.1.3.RELEASE</version>
110         </dependency>
111 
112         <!-- Mybatis 依赖 -->
113         <dependency>
114             <groupId>org.mybatis</groupId>
115             <artifactId>mybatis</artifactId>
116             <version>3.4.2</version>
117         </dependency>
118 
119         <!-- mybatis整合Spring -->
120         <dependency>
121             <groupId>org.mybatis</groupId>
122             <artifactId>mybatis-spring</artifactId>
123             <version>1.3.0</version>
124         </dependency>
125 
126         <!-- Mysql 依赖 -->
127         <dependency>
128             <groupId>mysql</groupId>
129             <artifactId>mysql-connector-java</artifactId>
130             <version>5.1.6</version>
131         </dependency>
132 
133         <!-- log4j日志 依赖 -->
134         <!-- 如果log4j的版本在1.2.14以上,则会出现下面的错误:
135 Missing artifact javax.jms:jms:jar:1.1:compile 
136 Missing artifact com.sun.jdmk:jmxtools:jar:1.2.1:compile 
137 Missing artifact com.sun.jmx:jmxri:jar:1.2.1:compile
138 解决方案:原因是log4j在1.2.15及以上版本更换了协议,与jms冲突。
139 解决办法有两种:一是使用1.2.14版本,二是手动下载jms.jar,安装到maven本地仓库中
140  -->
141         <dependency>
142             <groupId>log4j</groupId>
143             <artifactId>log4j</artifactId>
144             <version>1.2.15</version>
145             <exclusions>
146                 <exclusion>
147                     <groupId>com.sun.jmx</groupId>
148                     <artifactId>jmxri</artifactId>
149                 </exclusion>
150                 <exclusion>
151                     <groupId>com.sun.jdmk</groupId>
152                     <artifactId>jmxtools</artifactId>
153                 </exclusion>
154                 <exclusion>
155                     <groupId>javax.jms</groupId>
156                     <artifactId>jms</artifactId>
157                 </exclusion>
158             </exclusions>
159         </dependency>
160 
161         <!-- JSP相关 -->
162         <dependency>
163             <groupId>jstl</groupId>
164             <artifactId>jstl</artifactId>
165             <version>1.2</version>
166         </dependency>
167         <dependency>
168             <groupId>javax.servlet</groupId>
169             <artifactId>servlet-api</artifactId>
170             <version>2.5</version>
171             <scope>provided</scope>
172         </dependency>
173         <dependency>
174             <groupId>javax.servlet</groupId>
175             <artifactId>jsp-api</artifactId>
176             <version>2.0</version>
177             <scope>provided</scope>
178         </dependency>
179   <!-- 阿里 -->
180     <dependency>
181             <groupId>com.alibaba</groupId>
182             <artifactId>druid</artifactId>
183             <version>0.2.23</version>
184         </dependency>
185   </dependencies>
186   <build>
187     <finalName>demo1</finalName>
188   </build>
189 </project>

里面的 shiro依赖可以去掉,和json依赖里面的fastjson也可以去掉,

首先配置 applicationContext-mybatis.xml

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"  
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans   
 7         http://www.springframework.org/schema/beans/spring-beans.xsd    
 8         http://www.springframework.org/schema/mvc   
 9         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
10         http://www.springframework.org/schema/context   
11         http://www.springframework.org/schema/context/spring-context.xsd  
12         http://www.springframework.org/schema/tx    
13         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
14 
15     <context:component-scan base-package="org.team57.dxyh.mapper"/>
16     <context:component-scan base-package="org.team57.dxyh.service"/>  
17 
18     <!-- Mybatis 和 Spring的整合 -->  
19     <!-- 数据源 -->
20  
21     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
22         <!-- 基本属性 url、user、password -->
23         <property name="url" value="jdbc:mysql://localhost:3306/ceshi"/>
24         <property name="username" value="root"/>
25         <property name="password" value="123456"/>
26 
27         <!-- 配置初始化大小、最小、最大 -->
28         <property name="initialSize" value="10"/>
29         <property name="minIdle" value="10"/>
30         <property name="maxActive" value="50"/>
31 
32         <!-- 配置获取连接等待超时的时间 -->
33         <property name="maxWait" value="60000"/>
34         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
35         <property name="timeBetweenEvictionRunsMillis" value="60000" />
36 
37         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
38         <property name="minEvictableIdleTimeMillis" value="300000" />
39         <property name="validationQuery" value="SELECT 'x'" />
40         <property name="testWhileIdle" value="true" />
41         <property name="testOnBorrow" value="false" />
42         <property name="testOnReturn" value="false" />
43 
44         <!-- 打开PSCache,并且指定每个连接上PSCache的大小  如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。-->
45         <property name="poolPreparedStatements" value="true" />
46         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
47         <!-- 配置监控统计拦截的filters -->
48         <property name="filters" value="wall,stat" />
49     </bean>
50   
51     <!-- 2.Mybatis 的 SqlSession的工厂:SqlSessionFactoryBean dataSource引用数据源 Mybatis   
52         定义数据源,同意加载配置 -->  
53     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
54         <property name="dataSource" ref="dataSource"></property>  
55         <property name="mapperLocations" value="classpath:org/team57/dxyh/mapper/*.xml" />
56         <property name="configLocation" value="classpath:mybatis-config.xml"></property>  
57     </bean>  
58   
59     <!-- 3. Mybatis自动扫描加载Sql映射文件/接口:MapperScannerConfigurer sqlSessionFactory   
60         basePackage:指定sql映射文件/接口所在的包(自动扫描) -->  
61     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
62         <property name="basePackage" value="org.team57.dxyh.mapper"></property>  
63         <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
64     </bean>  
65   
66     <!-- 4.事务管理:DataSourceTransactionManager dataSource 引用上面定义好的数据源 -->  
67     <bean id="txManager"  
68         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
69         <property name="dataSource" ref="dataSource"></property>  
70     </bean>  
71   
72     <!-- 5.使用声明式事务: transaction-manager = "txManager" tx:advice 这种 是用 aop方式管理事物   
73         annotation-driven 这种是注解方式管理事物 第一种方式,需要在spring配置文件配置一些参数 第二种方式,需要在 类里 加一些注解进行事物管理   
74         用一种就行,没必须都用 -->  
75     <tx:annotation-driven transaction-manager="txManager" />  
78      
79 </beans>  

 

再配置 mybatis-config.xml 这是一个全局配置,可以省略

 1 <?xml version="1.0" encoding="UTF-8" ?>  
 2 <!DOCTYPE configuration  
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
 5 <configuration>  
 6     <!-- 配置打印 SQL 到控制台 以及懒加载 -->  
 7     <settings>  
 8         <setting name="logImpl" value="STDOUT_LOGGING" />  
 9         <!-- 打开延迟加载的全局开关 -->  
10         <setting name="lazyLoadingEnabled" value="true" />  
11         <setting name="aggressiveLazyLoading" value="false" />  
12         <setting name="mapUnderscoreToCamelCase" value="true" />  
13     </settings>  
14     <typeAliases>  
15            <package name="org.team57.dxyh.entity"/>                   
16     </typeAliases>  
17     <mappers>  
18         <package name="org.team57.dxyh.mapper"/>  
19     </mappers>  
20 </configuration>  

再配置 spring-MVC.xml

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:task="http://www.springframework.org/schema/task"  
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans   
 8         http://www.springframework.org/schema/beans/spring-beans.xsd    
 9         http://www.springframework.org/schema/mvc   
10         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
11         http://www.springframework.org/schema/context   
12         http://www.springframework.org/schema/context/spring-context.xsd  
13         http://www.springframework.org/schema/tx    
14         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
15         http://www.springframework.org/schema/task
16         http://www.springframework.org/schema/task/spring-task-4.0.xsd">  
17     <!-- 开启注解 -->  
18     <mvc:annotation-driven/>  
19     <!-- 配置自定扫描包 -->  
20     <context:component-scan base-package="org.team57.dxyh.controller"/>  
21     <context:component-scan base-package="org.team57.dxyh.service"/>  
22     <context:component-scan base-package="org.team57.dxyh.Shiro"/> 
23         <context:component-scan base-package="org.team57.dxyh.service.impl"/>
24         
25         <!-- 定时器,住院自动扣费 -->
26         <task:executor pool-size="5" id="executor"/>
27         <task:scheduler pool-size="10" id="scheduler"/>
28         <task:annotation-driven scheduler="scheduler" executor="executor"/>
29           
30     <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->  
31     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
32         <property name="prefix" value="/"></property>  
33         <property name="suffix" value=".jsp"></property>  
34     </bean>  
35     <!-- 处理静态资源 -->  
36     <mvc:resources location="/js/" mapping="/js/**"/>
37     <mvc:resources location="/css/" mapping="/css/**"/>
38     <mvc:resources location="/fonts/" mapping="/fonts/**"/>
39 </beans>  

 

最后配置web.xml

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4  
 5 <web-app>
 6   <!-- 加载spring与mybatis的整合配置 -->
 7  <context-param>
 8   <param-name>contextConfigLocation</param-name>
 9   <param-value>classpath:applicationContext-mybatis.xml</param-value>
10  </context-param>
11  
12  
13   <!-- 解决工程编码过滤器  
14         注意:请将此过滤器放在所有过滤器的前面,否则有可能出现乱码  -->
15  <filter>
16   <filter-name>encodingFilter</filter-name>
17   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18   <init-param>
19    <param-name>encoding</param-name>
20    <param-value>UTF-8</param-value>
21   </init-param>
22   <init-param>
23    <param-name>forceEncoding</param-name>
24    <param-value>true</param-value>
25   </init-param>
26  </filter>
27  <filter>  
28       <filter-name>shiroFilter</filter-name>  
29       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
30       <init-param>  
31           <param-name>targetFilterLifecycle</param-name>  
32           <param-value>true</param-value>  
33       </init-param>  
34       </filter>  
35       <filter-mapping>  
36           <filter-name>shiroFilter</filter-name>  
37           <url-pattern>/*</url-pattern>  
38       </filter-mapping>  
39  <filter>
40   <filter-name>HiddenHttpMethodFilter</filter-name>
41   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
42  </filter>
43  <filter-mapping>
44   <filter-name>encodingFilter</filter-name>
45   <url-pattern>/*</url-pattern>
46  </filter-mapping>
47 
48  <!-- 配置HiddenHttpMethodFilter过滤器,把 POST 请求 转换为 DELETE/PUT请求 -->
49  <filter-mapping>
50   <filter-name>HiddenHttpMethodFilter</filter-name>
51   <url-pattern>/*</url-pattern>
52  </filter-mapping>
53  <!-- 加载Spring 容器配置 -->
54  <listener>
55   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
56  </listener>
57  <!-- 防止Spring内存溢出监听器 -->
58  <listener>
59   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
60  </listener>
61  <!-- 配置 springMVC 核心控制器 -->
62  <servlet>
63   <servlet-name>springMVC</servlet-name>
64   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
65   <init-param>
66    <param-name>contextConfigLocation</param-name>
67    <param-value>classpath:spring-MVC.xml</param-value>
68   </init-param>
69   <load-on-startup>1</load-on-startup>
70  </servlet>
71 
72 
73  <servlet-mapping>
74   <servlet-name>springMVC</servlet-name>
75   <url-pattern>/</url-pattern>
76  </servlet-mapping>
77  <welcome-file-list>
78   <welcome-file>login.jsp</welcome-file>
79  </welcome-file-list>
80  <login-config>
81   <auth-method>BASIC</auth-method>
82  </login-config>
83 </web-app>
欢迎大家一起说出自己的想法。

网友评论

登录后评论
0/500
评论
曾将
+ 关注