Spring Boot integrates Mybatis environment construction example

1. Spring Boot integrates Mybatis environment to build

1. Create the maven project spring-boot-mybatis and configure it as follows in the pom.xml file:

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
	</parent>
  	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

  <dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	
	<!-- hot reload -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
	
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>1.3.0</version>
	</dependency>
	
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
  </dependencies>

 2. Create application.properties in the resources directory and add the following configuration:

 

spring.datasource.url=jdbc:mysql://localhost:3306/coredb

spring.datasource.username=root

spring.datasource.password=password

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

 

mybatis.mapper-locations= classpath:config/*.xml

 

3. Create a config directory in the resources directory, and create a UserMapper.xml file with the following contents:

<?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.tuozixuan.springbootmybatis.dao.UserDao">

	<!-- update name-->
	<update id="updateName" parameterType="map">
		update user set name = #{name, jdbcType=VARCHAR}  where id= #{id, jdbcType=INTEGER}
	</update>

</mapper>

 4. Write business and database operation sample code

 

4.1 UserController code

package com.tuozixuan.springbootmybatis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tuozixuan.springbootmybatis.service.UserService;

@RestController
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/updateName")
	public String updateName() {
		userService.updateName();
		return "updateName";
	}
}

 4.2 UserService code

package com.tuozixuan.springbootmybatis.service;

public interface UserService {

	void updateName();
}

 

 

4.3 UserServiceImpl code

package com.tuozixuan.springbootmybatis.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tuozixuan.springbootmybatis.dao.UserDao;
import com.tuozixuan.springbootmybatis.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateName() {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", 1);
		paramMap.put("name", "tuozixuan4");
		userDao.updateName(paramMap);
		
		paramMap = new HashMap<String, Object>();
		paramMap.put("id", 1);
		paramMap.put("name", "tuozixuan5");
		userDao.updateName(paramMap);
	}
}

 

4.4 UserDao code

package com.tuozixuan.springbootmybatis.dao;

import java.util.Map;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserDao {
	
	int updateName(Map<String, Object> paramMap);
}

 5. Add the launcher code and run

package com.tuozixuan.springbootmybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application
{
    public static void main(String[] args )
    {
        SpringApplication.run(Application.class, args);
    }
}

 6. Import test data

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'tuozixuan');

 6. 访问http://localhost:8080/updateName查看运行结果,默认支持事务,可使用@Transactional来声明一个事务

 

二、总结

 

1. 需要引入Spring Boot和Mybatis相关依赖,如spring-boot-starter、mybatis-spring-boot-starter等

 2. 需要在Spring Boot默认配置文件application.properties中加入数据源配置及mybatis扫描路径

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449728&siteId=291194637