spring+springmvc+mybatis 整合

本文详细介绍SSM框架(Spring、SpringMVC、MyBatis)的搭建过程及整合步骤,包括配置数据库连接、整合MyBatis与Spring、实现事务控制等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建ssm框架:

(1):MyBatis和Spring整合,通过Spring管理mapper接口。

  使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。

(2):通过Spring管理Service接口。

  使用配置方式将Service接口配置在Spring配置文件中。

  实现事务控制。

(3):由于SpringMVC是Spring的模块,无需整合这两个。

SSM工程:


操作步骤:
 
  1)创建工程,导入ssm框架包
   2)引入spring框架
   3)重新创建javaproject工程,mybatis-generator工具来生成对应的实体类,接口,映射文件
   4)配置mybatis-config.xml文件放在src目录下!
 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

	<!-- 全局的设置参数 -->
	<settings>
		<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
		<setting name="useGeneratedKeys" value="true" />
	</settings>

	<!-- 配置实体类别名 -->
	<typeAliases>
		<!-- 批量扫描别名 -->
		<package name="com.hlx.entity" />
	</typeAliases>

	<!-- 配置mapper 由于使用spring和mybatis的整合包进行整合,这了无需配置 但必须遵循:mapper.java和mapper.xml文件同名且在同一目录 -->
	<!--<mappers></mappers> -->

	<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<property name="dialect" value="mysql" />
			<property name="reasonable" value="true" />
		</plugin>
	</plugins>

</configuration>

   5)配置数据库jdbc.properties文件放在src目录下!    

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java178?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

   6)配置applicationContext-dao.xml文件数据源、SqlSessionFactory、mapper扫描器放在src目录下!

<!-- 读取数据库配置 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>

	<!-- 配置mybatis SqlSessionFactory -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>

	<!-- mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
		<property name="basePackage" value="com.hlx.mapper" />
		<!-- 指定会话工厂; 注意:必须是value; -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
	</bean>


   7)业务层service接口,具体实现的类StudentsServiceImpl(忽略)
   8)配置applicationContext-service.xml文件

<!-- 配置扫描器 -->
	<context:component-scan base-package="com.hlx.service" />

   9)配置applicationContext-tx.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/aop
	http://www.springframework.org/schema/aop/spring-aop-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/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
	">

	<!-- 事务管理器对象 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置事务 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*save*" propagation="REQUIRED" />
			<tx:method name="*del*" propagation="REQUIRED" />
			<tx:method name="*update*" propagation="REQUIRED" />
			<tx:method name="*find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="*all*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 切面 -->
	<aop:config>
	 <aop:pointcut expression="execution(* com.hlx.service.*.*(..))" id="mycut"/>
	 <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
	</aop:config>

</beans>

   10)控制层springmvc类 (忽略)
   11)配置springmvc.xml文件

<!-- 配置扫描器 -->
	<context:component-scan base-package="com.hlx.controller" />

	<!-- 注解配置 -->
	<mvc:annotation-driven conversion-service="conversionServiceFactory" />

	<!-- 配置日期转化格式 -->
	<bean id="conversionServiceFactory"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.hlx.util.StringToDateConvert" /> <!-- 转化类 -->
			</set>
		</property>
	</bean>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/>

   12)在web.xml中配置前端控制器

<!-- 加载 applicationContext.xml文件 -->
	<context-param>
		<description>设置Spring加载时的配置文件位置,默认位置在web-inf/lib下</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-*.xml</param-value>
	</context-param>

	<!-- 启动spring容器 -->
	<listener>
		<display-name>监听spring</display-name>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- springMVC前端控制器 -->
	<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:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>


	<!-- 解决中文 -->
	<filter>
		<filter-name>China</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>
	</filter>

	<filter-mapping>
		<filter-name>China</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

效果:

(1)没有条件查询数据


(2)根据一个条件查询数据


(3)根据多个条件查询数据


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值