Configuring mybatis in spring project

There are many ways to integrate mybatis in spring, which can be fully xml configured or annotated. A simple configuration method is recorded here.

Project directory:

1. Reference the maven configuration of the jar package.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    		<dependency>
	    	<groupId>mysql</groupId>
	    	<artifactId>mysql-connector-java</artifactId>
	    	<version>5.1.10</version>
		</dependency>
	     	<dependency>
    		<groupId>com.alibaba</groupId>
    		<artifactId>druid</artifactId>
    		<version>1.1.6</version>
	 	</dependency>	
		<dependency>
    		<groupId>org.mybatis</groupId>
    		<artifactId>mybatis-spring</artifactId>
    		<version>1.3.1</version>
		</dependency>
    <dependency>
    		<groupId>org.mybatis</groupId>
    		<artifactId>mybatis</artifactId>
    		<version>3.4.4</version>
		</dependency>
     <!--Reflection generated entity class-->  
    <dependency>  
        <groupId>org.mybatis.generator</groupId>  
        <artifactId>mybatis-generator-core</artifactId>  
        <version>1.3.5</version>
    </dependency> 	

2. Configure the spring application context.

Add configuration in namespace:

 xmlns:mybatis="http://mybatis.org/schema/mybatis-spring

Add configuration in xsi:schemaLocation:

http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd

The configuration is as follows:

2. Configure the package and sqlSessionFactory where the mapping mapper interface of mybatis is scanned.

        <!-- Definition mybaits -->
        <!-- mybatis:scan Will automatically scan com.altnum.mybatis.mapper All interfaces in the package are treated as mapper Configure, then import automatically mapper class-->
        <mybatis:scan base-package="com.altnum.mybatis.mapper"/>
        <!-- To configure sqlSessionFactory  org.mybatis.spring.SqlSessionFactoryBean Is for consolidation spring Of bean-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" 
        	p:dataSource-ref="dataSource"/>

dataSource is a database connection pool. Configuration is not given here.

3. com.altnum.mybatis.domain.TestUser. Database table mapping bean

package com.altnum.mybatis.domain;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class TestUser implements Serializable
{
	private Integer id; //id
	private String name; //Full name
	private String password; //Password
	private String sex; //Gender
	
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public String getSex()
	{
		return sex;
	}
	public void setSex(String sex)
	{
		this.sex = sex;
	}
	
	public String toString() {
		return ToStringBuilder.reflectionToString(this,ToStringStyle.DEFAULT_STYLE);
	}
}

4. mybatis annotation mapping interface.

mybatis:scan will automatically scan all interfaces in com.altnum.mybatis.mapper package as mapper configuration, and then automatically introduce mapper class.

package com.altnum.mybatis.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.altnum.mybatis.domain.TestUser;

public interface TestMapper
{
	@Select("select user_id id,user_name name,user_password password,user_sex sex from thnb_user "
			+ "where user_id = #{id}")
	TestUser findUserWithId(@Param("id") int id);
}

5. Test call.

Inject the reference mapper mapping interface directly in the control layer.

package com.altnum.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.altnum.common.GetUTCTimeUtil;
import com.altnum.mybatis.domain.TestUser;
import com.altnum.server.TestServer;

@Controller
@RequestMapping(value = "/test")
public class TestController
{
	@Resource TestServer testServerImpl;
	//Auto inject userMapper
	@Autowired
	private com.altnum.mybatis.mapper.TestMapper TestMapper; 
	
	private static final Logger logger = LogManager.getLogger(TestController.class);
	
	@RequestMapping(value = "test",method=RequestMethod.POST)
	@ResponseBody
	public Object test(HttpServletRequest request, HttpServletResponse response,
			@RequestBody(required = false) String json)
	{
		logger.info("test:" + json);
		TestUser tu = TestMapper.findUserWithId(1);
		logger.info("testMyabtis:" + tu.toString());
		return tu.toString();
	}
}

Keywords: Mybatis Spring Java Apache

Added by ViralStef on Tue, 31 Mar 2020 23:20:32 +0300