6. SpringMVC Integrates Mapper of Mybatis

SpringMVC Integrates Mybatis Thoughts

  • Step 1: Integrate the dao layer

    • mybatis integrates with spring to manage the mapper interface through spring.
    • Use mapper's scanner to automatically scan the mapper interface for registration in spring.
  • Step 2: Integrate the service layer

    • Manage service interfaces through spring.
    • Use configuration to configure the service interface in the spring configuration file.
    • Implement transaction control.
  • Step 3: Integrate springmvc

    • Since springmvc is a spring module, there is no need for integration.

Integrate dao

First add two files under the resource folder: the database configuration file and the log configuration file

  • Database configuration file db.properties


Mybatis's own configuration file.Create a new mybatis folder in the resources directory and a new sqlMapConfig.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    <!--Global setting s configuration, add as needed-->
    <!--Configure Alias-->
        <!--Batch Scan Alias-->
        <package name="com.iot.learnssm.firstssm.po"/>
    <!--Configure mapper
    Since you use the spring and mybatis package for mapper scanning, there is no need to configure it here.
    Must follow: mapper.xml and mapper.java files have the same name and are in a directory
    <!-- <mappers>
    </mappers> -->


Create a new spring folder in the resources directory and a new applicationContext-dao.xml file.To configure:

  • data source
  • SqlSessionFactory
  • mapper scanner
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Load db.properties The contents of the file, db.properties File key There must be some special rules for naming -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- Configure the data source, dbcp -->
    <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" />
    <!-- Look in the package. org.mybatis:mybatis-spring:1.2.4 -->
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- Database Connection Pool -->
        <property name="dataSource" ref="dataSource" />
        <!-- Load mybatis Global Profile -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    <!-- mapper Scanner -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- Scan package paths, separated by a half-corner comma if multiple packages need to be scanned -->
        <property name="basePackage" value="com.iot.learnssm.firstssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
       <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
       //This causes the data source configuration to become unusable and the database connection to fail.And spring 4 is deprecated -->

Manually Define Commodity Query Mapper

For comprehensive query mapper, generally there will be related queries, it is recommended to customize mapper

  • ItemsMapperCustom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.iot.learnssm.firstssm.mapper.ItemsMapperCustom" >

   <!-- Defining a commodity query sql Fragment, which is the commodity query condition -->
   <sql id="query_items_where">
   	<!-- Use Dynamic sql,adopt if Judgement, conditional execution sql Stitching -->
   	<!-- Commodity Query Conditions Passed ItemsQueryVo In wrapper object itemsCustom Attribute Transfer -->
   		<if test="itemsCustom!=null">
   			<if test="itemsCustom.name!=null and itemsCustom.name!=''">
   				items.name LIKE '%${itemsCustom.name}%'
  	<!-- Commodity List Query -->
  	<!-- parameterType Incoming wrapper object(Wrapped query criteria)
  		resultType Recommended use of extended objects
  	<select id="findItemsList" parameterType="com.iot.learnssm.firstssm.po.ItemsQueryVo"
  		SELECT items.* FROM items  
  			<include refid="query_items_where"></include>
  • ItemsMapperCustom.java
package com.iot.learnssm.firstssm.mapper;

public interface ItemsMapperCustom {
    //Commodity Query List
    List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
  • po class ItemsCustom
package com.iot.learnssm.firstssm.po;

 * Extended Class for Commodity Information
public class ItemsCustom extends Items{
    //Extended attributes for adding commodity information
  • Enter the wrapper class for pojo
package com.iot.learnssm.firstssm.po;

public class ItemsQueryVo {

    //Commodity Information
    private Items items;

    //For system scalability, extend the originally generated po
    private ItemsCustom itemsCustom;

    public Items getItems() {
        return items;

    public void setItems(Items items) {
        this.items = items;

    public ItemsCustom getItemsCustom() {
        return itemsCustom;

    public void setItemsCustom(ItemsCustom itemsCustom) {
        this.itemsCustom = itemsCustom;

Tags: Programming Mybatis Spring JDBC xml

Posted on Thu, 07 Nov 2019 09:05:04 -0500 by capitala