Spring整合Mybatis和SpringMVC框架

本文详细介绍了如何在Spring MVC框架中集成MyBatis,包括配置web.xml、pom文件依赖、spring-config.xml,创建spring-model.xml和spring-web.xml,配置数据源和MyBatis映射文件,以及创建测试类验证数据访问。

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

第一步:配置web.xml

<!-- 配置spring mvc前端控制器 -->
  <servlet>
      <servlet-name>frontController</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
           <!-- 通过此参数的配置加载spring配置文件,然后初始化资源 -->
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring-configs.xml</param-value>
      </init-param>
      <!--让tomcat启动则加载此servlet对象,数值越小优先级越高 -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置Spring MVC前端控制器映射(servlet映射) -->
  <servlet-mapping>
       <servlet-name>frontController</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>

第二步:配置pom文件依赖:

<dependencies>
<!--配置连接池-->
  	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>druid</artifactId>
  		<version>1.1.10</version>
  	</dependency>
  	<!--配置springMVC的依赖-->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>4.3.9.RELEASE</version>
  	</dependency>
  	<!--配置json串的转换-->
  	<dependency>
  		<groupId>com.fasterxml.jackson.core</groupId>
  		<artifactId>jackson-databind</artifactId>
  		<version>2.8.5</version>
  	</dependency>
  	<!--配置单元测试-->
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
  	<!--配置spring整合mybatis-->
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis-spring</artifactId>
  		<version>1.3.1</version>
  	</dependency>
  	<!--添加mybatis依赖-->
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.2.8</version>
  	</dependency>
  	<!--添加数据库依赖-->
  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.40</version>
  	</dependency>
  	<!--spring整合jdbc-->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-jdbc</artifactId>
  		<version>4.3.9.RELEASE</version>
  	</dependency>
  </dependencies>

第三步配置:spring-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
      <!-- 扫描 指定的包,获取由spring管理的bean信息
      在指定包下面会查找有@Controller,@Service等注解的相关的类-->
      <context:component-scan base-package="com.db"/>
      <!-- 导入configs.properties文件  系统底层会创建一个bean对象(类型为properties类型)-->
      <util:properties  id="cfg" location="classpath:configs.properties"/>
      <!-- 导入spring-model.xml文件(MVC中m的信息会配置在model文件配置) -->
      <import resource="classpath:spring-model.xml"/>
      <!-- 导入spring-web.xml文件(MVC中VC的信息会配置在这里配置) -->
      <import resource="classpath:spring-web.xml"/>
</beans>

第四步:创建spring-model.xml和spring-web.xml
1.配置spring-mvc

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
       <!--启用mvc默认配置(内置一部分bean对象的定义)-->
       <mvc:annotation-driven/>
       <!--注册视图解析对象 -->
       <bean id="viewResovler"
             class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="Prefix" value="/WEB-INF/pages/"/>
             <property name="Suffix" value=".html"/>
       </bean>
</beans>

2.spring-model.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
      <import resource="classpath:spring-datasource.xml"/>
      <import resource="classpath:spring-mybatis.xml"/>
</beans>

第五步创建.spring-datasource.xml和spring-mybatis.xml文件
1.spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
      <!-- 整合mybatis框架,系统底层会通过sqlSessionFactoryBean
      创建sqlSessionFactory -->
        <bean id="sqlSessionFactory"
             class="org.mybatis.spring.SqlSessionFactoryBean">
             <property name="DataSource" 
                       ref="dataSource"/>
                       <!-- 设置mapper文件的位置 -->
             <property name="MapperLocations"
              value="classpath*:mapper/sys/*Mapper.xml"/>
                       </bean>
      <!-- 配置mybatic包扫描(对指定包下的dao接口进行扫描) 
      系统底层会基于接口创建一个类的对象,并将其储存到Spring容器-->
       <bean id="daoScanner" 
             class="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <property name="BasePackage" value="com.db.**.dao"/>
             <!-- 当spring容器只有一个sqlSessionFactory时如下配置可以不写 -->
             <property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       </bean>
</beans>

2.spring-datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
      <!-- 整合DRUID连接池 -->
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            init-method="init" destroy-method="close" lazy-init="false">
           <property name="DriverClassName" value="#{cfg.jdbcDriver}"/> 
           <property name="Url" value="#{cfg.jdbcUrl}"/> 
           <property name="Username" value="#{cfg.jdbcUser}"/> 
           <property name="Password" value="#{cfg.jdbcPassword}"/> 
   </bean>
</beans>

第六步创建configs.properties文件

jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8
jdbcUser=root
jdbcPassword=root
jdbcMaxActive=100

第七部:创建测试类测试数据
a)创建测试父类

public class TestBase {
	protected ClassPathXmlApplicationContext ctx;
	@Before
	public void init(){
		 ctx = new ClassPathXmlApplicationContext("spring-configs.xml");
	}
	@Test
	public void testCtx(){
		System.out.println(ctx);
	}
	/**测试<util:properties>标签定义的bean*/
	@Test
	public void testproperties(){
		//1.获取bean对象
		//Properties bean = ctx.getBean("Property",Properties.class);
		Object obj=ctx.getBean("cfg");
		//2.获取bean对象的具体类型
		System.out.println(obj.getClass().getName());
		//3.获取bean对象中存储的内容
		System.out.println(obj);
	}
	@After
	public void destory(){
		ctx.close();
	}

b)测试数据源

public class TestDataSource extends TestBase{
@Test
	public void testDruidDataSource() throws Exception{
		DataSource ds = ctx.getBean("dataSource",DataSource.class);
		System.out.println(ds);
		System.out.println(ds.getConnection());
	}
	}

c)测试mybatis

package com.test;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import com.db.sys.dao.SysLogDao;

public class TestMyBatis extends TestBase {
	@Test
	public void testSqSessionFactory(){
		SqlSessionFactory factory = ctx.getBean("sqlSessionFactory",SqlSessionFactory.class);
		System.out.println(factory);
	}
	@Test
	public void testSqlsession01(){
		//1.获取SqlSessionFactory工厂(由Spring框架处理)
		SqlSessionFactory factory = ctx.getBean("sqlSessionFactory",SqlSessionFactory.class);
		//2.获取一个SqlSession对象(会话对象)
		SqlSession session = factory.openSession();
		//3.基于SqlSession对象访问数据库(进行对话)
		//3.1基于statement获取sql信息
		String statement="com.db.sys.dao.SysLogDao.findLogs";
		//3.2将sql发送到数据库端实现与数据库的会话
		List<Map<String,Object>> list = session.selectList(statement);
		for (Map<String, Object> map : list) {
			System.out.println(map);
		}
		//4.会话结束释放资源
		session.close();
		
	}
	@Test
	public void testSqlsession02(){
		//1.获取SqlSessionFactory工厂(由Spring框架处理)
		SqlSessionFactory factory = ctx.getBean("sqlSessionFactory",SqlSessionFactory.class);
		//2.获取一个SqlSession对象(会话对象)
		SqlSession session = factory.openSession();
		//3.基于SqlSession对象访问数据库(进行对话)
		SysLogDao dao=session.getMapper(SysLogDao.class);
		List<Map<String,Object>> list = dao.findLogs();
		for (Map<String, Object> map : list) {
			System.out.println(map);
		}
		//4.会话结束释放资源
		session.close();
		
	}
	@Test
	public void testSqlsession03(){
		//1.获取Dao对象
		//1.1系统底层为dao接口创建了一个实现类的对象
		//1.2底层创建的对象会获取SqlSessionFactory
		//1.3底层会基于SqlSessionFactory创建SqlSession
		SysLogDao dao=ctx.getBean("sysLogDao",SysLogDao.class);
		//2.基于Dao实现与熟即可的会话
		List<Map<String,Object>> list = dao.findLogs();
		for (Map<String, Object> map : list) {
			System.out.println(map);
		}
	}
}

第八步创建controller层 service层 和Mapper层

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值