ssm框架简单整合思路
Ssm框架目录
Ssm框架目录
目录
1创建数据库连接配置文件jdbc.properties. 2
2.使用spring将spring和mybatis整合在一起applicationContext.xml 2
将jdbc.properties数据库属性文件加载进来... 3
使用spring和mybaties的支持jar包,将数据源注入sqlSessionFactory. 3
1创建数据库连接配置文件jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm
jdbc.username=root
jdbc.password=root
2.使用spring将spring和mybatis整合在一起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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/p
http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
将jdbc.properties数据库属性文件加载进来
<!-- 配置 读取properties文件 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
创建数据源
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
注:可以使用不同的数据库连接池类来配置,这里采用com.alibaba.druid.pool.DruidDataSource,基于阿里提供的jar包(druid-1.0.9.jar),可以自己选择出c3p0等。
使用spring和mybaties的支持jar包,将数据源注入sqlSessionFactory
<!-- 配置sqlSessionFactory 工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/yang/mapper/*.xml"></property>
</bean>
sqlSessionFactory工厂中(即org.mybatis.spring.SqlSessionFactoryBean)中拥有一个数组mapperLocations获取创建的mybaties与数据库对应的mapper配置文件,将dataSource注入进入工厂,则获得了与数据库的联系。Mybaties实现了根据接口与mapper.xml文件的动态代理类,框架帮助我们实现具体操作,我们只需要负责写映射关系。
扫描与映射文件对应的接口
注入<property name="basePackage" value="com.yang.mapper" />到org.mybatis.spring.mapper.MapperScannerConfigurer,使用mybaties扫描接口
Xml文件与接口建议放在同一包中
<!-- 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBean来生成Mapper接口的代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yang.mapper" />
<!-- sqlSessionFactoryBeanName返回值是String使用value注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
使用注解开发最重要的一步:开启注解扫描
可以放在springmvc-servlet.xml中也可以放在applicationContext.xml中。Spring完全可以将所有的信息全部配置,但如果需要利于开发,建议分层配置。此次仅为一个小案例搭建
<context:annotation-config />
<context:component-scan base-package="com.yang"/>
<mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation-driven>
配置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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/p
http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
3配置详细
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 指定所上传的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件 -->
<property name="maxUploadSize" value="200000"></property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 添加springmvc的配置 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet的初始化配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC-Servlet.xml</param-value>
</init-param>
<!-- 是否启动时就加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener> spring的核心监听器,由监听器负责将classpath:applicationContext.xml文件加载进入服务器进行解读,采用观察者模式,完成了在服务器启动时加载配置文件的功能,不会影响用户体验,相当于分出一个子线程去完成此操作,不影响主线程的执行。
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>