Spring boot 入门篇 整合mybatis (XML版)

yang-pig| 阅读:587 发表时间:2018-07-28 14:26:13 Java

Spring Boot中整合MyBatis,最近学习使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤。搜了一下Spring Boot整合MyBatis的文章,查了一下文档。下面就来详细介绍如何在Spring Boot中整合MyBatis。

1 添加相关maven文件

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

完整的pom包这里就不贴了

2、application.properties 添加相关配置

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password =
mybatis.type-aliases-package=com.yang.demo.entity
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了。

在启动类中添加对dao包扫描@MapperScan

package com.yang.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yang.demo.dao")
public class Application {

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

3、开发bean

package com.yang.demo.entity;

public class User {
    private Long id;
    private String userName;
    private String password;
    private String nickName;

    public User() {

    }

    public User(String userName, String password, String nickName) {
        this.userName = userName;
        this.password = password;
        this.nickName = nickName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

4、开发dao

package com.yang.demo.dao;

import com.yang.demo.entity.User;

import java.util.List;

public interface UserDao {

    List<User> getAll();

    User getOne(Long id);

    void insert(User user);

    void update(User user);

    void delete(Long id);
}

为了更接近生产我特地将nick_name属性在数据库加了下划线和实体类属性名不一致

5、mybatis基础配置文件和实体类映射文件配置

mybatis-config.xml 配置

```<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">


User的映射文件:

<?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">

SELECT id, userName, password, nick_name FROM users FROM users WHERE id = #{id} INSERT INTO users (userName,password,nick_name) VALUES (#{userName}, #{password}, #{nickName}) UPDATE users SET userName = #{userName}, password = #{password}, nick_name = #{nickName} WHERE id = #{id} DELETE FROM users WHERE id =#{id}


### 6、使用

上面基本完成了相关开发,使用的时候当作普通的类注入进入就可以了

package com.yang.demo.dao;

import com.yang.demo.entity.User;import org.junit.Assert;import org.junit.Ignore;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)@SpringBootTestpublic class UserDaoTest {

@Autowired
private UserDao userDao;
@Ignore
@Test
public void testInsert() throws Exception {
    userDao.insert(new User("aa", "a123456","测试1"));
    userDao.insert(new User("aa", "a123456","测试1"));
    userDao.insert(new User("aa", "a123456","测试1"));

    Assert.assertEquals(3, userDao.getAll().size());
}

@Test
public void testQuery() throws Exception {
    List<User> users = userDao.getAll();
    System.out.println(users.toString());
}
@Ignore
@Test
public void testUpdate() throws Exception {
    User user = userDao.getOne(3l);
    System.out.println(user.toString());
    user.setNickName("neo");
    userDao.update(user);
    Assert.assertTrue(("neo".equals(userDao.getOne(3l).getNickName())));
}

}

```

7、结果

企业微信截图_15327603783718.png