JavaSSM框架整合
ssm整合
在博客的前面介绍了mybatis,spring,springmvc的使用,那么这篇博客将介绍将mybatis和spring,springmvc的整合。
整合之前,我们需要明白一个点,spring和mybatis之间进行整合不像spring和springmvc一样是天然一体,所以在中间就需要一个磨合剂,那就是mybatis-spring
这个依赖。
jar包的导入
使用maven进行项目管理
<!--添加servlet的依赖--> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--jstl用于在控制器中将模型绑定到jsp中--> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!--添加spring的依赖--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</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-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <!--添加spring mvc的依赖--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</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-aspects</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</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-tx</artifactid> <version>${spring.version}</version> </dependency> <!--添加数据库mysql驱动--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>8.0.13</version> </dependency> <!--添加mybatis依赖--> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.2.6</version> </dependency> <!--整合mybatis与spring的依赖--> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.0</version> </dependency> <!-- mybatis分页依赖 --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>5.1.2</version> </dependency> <!-- 处理时间日期格式 --> <dependency> <groupid>joda-time</groupid> <artifactid>joda-time</artifactid> <version>2.9.9</version> </dependency> <!-- 用于md5加密 --> <dependency> <groupid>commons-codec</groupid> <artifactid>commons-codec</artifactid> <version>1.10</version> </dependency> <!--日志--> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>1.7.7</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.7.7</version> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <!--添加json依赖--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-annotations</artifactid> <version>${jackson.version}</version> </dependency> <!--有时候不加可能会报错--> <dependency> <groupid>net.sf.json-lib</groupid> <artifactid>json-lib</artifactid> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-core</artifactid> <version>${jackson.version}</version> </dependency> <!-- 新添加处理json为java bean --> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-core-asl</artifactid> <version>1.9.13</version> </dependency> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-mapper-asl</artifactid> <version>1.9.13</version> </dependency> <!-- 数据源的引入, 池化技术 --> <dependency> <groupid>com.mchange</groupid> <artifactid>c3p0</artifactid> <version>0.9.2.1</version> </dependency> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.2.2</version> </dependency> <!--阿里巴巴json库--> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2.47</version> </dependency> <!--java发送http请求的库--> <dependency> <groupid>com.github.kevinsawicki</groupid> <artifactid>http-request</artifactid> <version>5.6</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency>
在这里面导入的jar包有点多,不过可以基本满足我们的需求了。
文件的配置
首先,我们要明确文件的配置从哪里开始,那么肯定是需要最先配置web.xml
了,因为它管理着web项目的配置信息。
-
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd http://www.springframework.org/schema/mvc/spring-mvc.xsd" version="3.1"> <!--配置contextloaderlistener,初始化ioc容器 作用就是启动web容器时,自动装配applicationcontext的配置信息 经常配合下面<context-param>使用 --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 上下文 --> <context-param> <param-name>contextconfiglocation</param-name> <!--classpath其实就相当于resources文件夹--> <param-value>classpath:spring/applicationcontext.xml</param-value> </context-param> <!-- 防止spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.introspectorcleanuplistener</listener-class> </listener> <!--servlet分发--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:/spring/springmvc-servlet.xml</param-value> </init-param> </servlet> <!-- characterencodingfilter字符编码过滤器 --> <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> <init-param> <!--是否强制设置request的编码为encoding,即utf-8,默认为false --> <param-name>forcerequestencoding</param-name> <param-value>false</param-value> </init-param> <init-param> <!--是否强制设置response的编码为encoding,即utf-8 --> <param-name>forceresponseencoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterencodingfilter</filter-name> <!-- 这里必须写 /* --> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
applicationcontext.xml文件的配置
在web.xml文件配置中,我们关于上下文文件的引入就只写了一个
applicationcontext.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" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--引入spring和其他整合的配置文件 比如spring-mybatis.xml等--> <import resource="classpath:/spring/spring-*.xml"/> </beans>
-
配置springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描controller的注解 填写自己的控制器的包 --> <context:component-scan base-package="cc.weno.controller"/> <!--静态资源放行问题--> <mvc:default-servlet-handler/> <!--配置代理--> <mvc:annotation-driven/> <!--配置拦截器--> <mvc:interceptors> <!--配置一个拦截器 可以配置多个,那么拦截顺序则是从上到下 --> <mvc:interceptor> <!--拦截的uri /* 代表拦截这一层的任意字符 /** 代表拦截任意层次的任意字符 --> <mvc:mapping path="/**"/> <!--不进行拦截的uri--> <mvc:exclude-mapping path="/index"/> <mvc:exclude-mapping path="/login"/> <!-- 拦截器的包 --> <bean class="cc.weno.interceptor.logininterceptor"/> </mvc:interceptor> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" id="internalresourceviewresolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
在配置mybatis文件之前,我们需要配置一下
jdbc.properties
mysql.driver=com.mysql.jdbc.driver mysql.url=数据库地址 例如:jdbc:mysql://localhost:3306/test mysql.username=账号 mysql.password=密码 #定义初始连接数 dbcp.initialsize=0 #定义最大连接数 dbcp.maxactive=20 #定义最大空闲 dbcp.maxidle=20 #定义最小空闲 dbcp.minidle=1 #定义最长等待时间 dbcp.maxwait=60000
-
spring-mybatis.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: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/cache" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--自动扫描包,包括控制层和持久层 --> <context:component-scan base-package="cc.weno"/> <!--引入数据库配置文件--> <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <!-- value 填写jdbc.properties文件的位置 --> <property name="location" value="classpath:/config/jdbc.properties"/> </bean> <!--配置连接池--> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close" > <property name="driverclassname" value="${mysql.driver}"/> <property name="url" value="${mysql.url}"/> <property name="username" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> <!--定义数据库的连接--> <property name="initialsize" value="${dbcp.initialsize}"/> <property name="maxidle" value="${dbcp.maxidle}"/> <property name="minidle" value="${dbcp.minidle}"/> <property name="maxwait" value="${dbcp.maxwait}"/> <property name="maxactive" value="${dbcp.maxactive}"/> </bean> <!--整合spring和mybatis,就不需要像以前一样写mybati的配置文件了--> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource"/> <!-- 自动扫描mapping.xml文件 注意路径是"/"不是"."--> <property name="mapperlocations" value="classpath:mybatis/*.xml"/> <property name="configuration"> <!--可以将之前mybatis的一些配置项转移到这里来--> <bean class="org.apache.ibatis.session.configuration"> <property name="mapunderscoretocamelcase" value="true"/> </bean> </property> </bean> <!-- 扫描dao持久层接口 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="cc.weno.mapper"/> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/> </bean> <!--数据库事务管理--> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> </beans>
项目结构如图所示
层 | 作用 | 设计 |
---|---|---|
持久层(dao层) | dao层负责与数据库联系,进行数据持久化 | 首先设计接口,然后再配置×××mapper.xml文件 |
业务层(service层) | 负责业务模块的逻辑应用设计 | 先设计接口,再设计实现类,然后调用接口来实现逻辑业务(spring-mybatis.xml文件配置后,直接调用) |
control层(控制层) | 负责具体模块的控制 | 调用service层的接口来控制业务 |
dao层和前面的博客配置没有什么区别,让我们主要来看看service层的配置。
首先,先定义userservice的接口
public interface userservice { user getuser(string name); }
然后进行service接口的实现
public class userserviceimpl implements userservice { /** * mybatis的接口 */ @resource private usermapper usermapper; // 直接进行使用 @override public user getuser(string name) { usermapper.getuser(name); } }
在controller层中
@controller public class usercontroller { @resource private userservice userservice; @requestmapping(value = "/index") @responsebody public string index(@requestparam("name")string name) { user u =userservice.getuser(name); return u; } }
usermapper.xml文件倒是没有任何要更改的地方,只要别忘了配置<mapper namespace="cc.weno.mapper.usermapper">
文件就行了。
然后ssm框架基本的整合就到这里了,在这里面代码没几行,配置文件倒是写了一大堆〒_〒【此时好想说:springboot
真香】
下一篇: vue v-for动画bug