欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot整合Mybatis环境搭建示例

程序员文章站 2022-05-11 17:30:12
...

一、Spring Boot整合Mybatis环境搭建

1. 创建maven工程spring-boot-mybatis,并在pom.xml文件中如下配置:

	<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>
	
	<!-- 热加载 -->
	<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. 在resources目录下创建application.properties,并添加如下配置:

 

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. 在resources目录下创建config目录,并创建UserMapper.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.tuozixuan.springbootmybatis.dao.UserDao">

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

</mapper>

 4. 编写业务及数据库操作示例代码

 

4.1 UserController代码

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代码

package com.tuozixuan.springbootmybatis.service;

public interface UserService {

	void updateName();
}

 

 

4.3 UserServiceImpl代码

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代码

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. 加入启动器代码,并运行

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. 导入测试数据

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扫描路径