KKB : SSM框架的整合
作者:互联网
框架的搭建
1、创建web-app 的maven项目
2、创建main包下的java文件夹和resource文件夹
3、添加依赖包,统一框架版本
规定spring的版本
<dependencies> <!-- 加入ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- MySQL依赖 start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- 加入MyBatis 依赖 start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- 引入Spring(包含SpringMVC) 依赖 start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springversion}</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springversion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springversion}</version> </dependency> <!-- 引用插件依赖:MyBatis整合Spring,如果mybatis版本在3.4及以上版本 mybatis-spring的版本要在1.3以上 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
<!-- 德鲁伊数据连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <!--处理json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <!--javaee--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!--文件上传下载--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies>
导入pom依赖包,复制进pom文件下,重新排版代码即可(有快捷键)
创建spring配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
将web-xml的配置头信息 从2.3的版本改成2.5版本的
在web-xml中需要配置前端控制器DispatcherServlet,记得还要初始化spring的配置文件
在web-xml中还需要配置处理post乱码,使用的类是
CharacterEncodingFilter,在filter中也需要配置初始值
下面编写业务逻辑代码,bean - dao - service - controller
<?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"
xmlns:p="http://www.springframework.org/schema/p"
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.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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.atshiyou"></context:component-scan>
<!--配置数据源-->
<bean id="db" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/express?userUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--创建SQLSessionFactory-->
<bean id="sqlFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="db"></property>
</bean>
<!--创建SQLSessionTemplate,没有无参构造-->
<bean id="sqlTem" class="org.mybatis.spring.SqlSessionTemplate">
<!--没有无参构造,我们就使用有参构造-->
<constructor-arg index="0" ref="sqlFactory"></constructor-arg>
</bean>
<!--配置事务-->
<bean id="mytx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="db"></property>
</bean>
<tx:annotation-driven transaction-manager="mytx"></tx:annotation-driven>
<!--启动springmvc注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置静态资源访问-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
配置spring-xml中的配置信息
配置mybatis的信息:注意这里的pageHelper的插件使用的是4.1版本的,使用的插件版本不同,插件代码不同
配置mapper映射的代码
在spring配置文件中指定mybatis配置文件和mapper配置文件的位置
标签:配置文件,框架,spring,springframework,SSM,mybatis,springversion,org,KKB 来源: https://blog.csdn.net/awodwde/article/details/113117122