springboot mybatis使用xml方式配置方式

项目地址:github地址

  • 创建项目
  • 配置pom.xml
  • mybatis配置
  • main函数创建 application.yml文件创建
  • 单元测试
1.创建一个maven项目

文件目录格式:


创建
pom.xml


    4.0.0

    com.hiro
    springbootDemo
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
            org.springframework.boot
            spring-boot-starter-test
            1.3.1.RELEASE
        
        
            com.alibaba
            fastjson
            1.2.41
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

如果对应依赖在当前工程还是不能正确引用,右击pom.xml-》Maven-》Reimport

mybatis引入

mybatis可通过注解和xml引入,这里主要介绍xml配置。
创建UserMapper.java

package com.hiro.demo.db.dao;

import com.hiro.demo.db.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * Project: springbootDemo
 *
 * @author : hirolin
 * @date : 2019/3/25 22:14
 */
@Mapper
public interface UserMapper {

    User getUser(@Param("id") int id);
}

创建User.java

package com.hiro.demo.db.entity;

/**
 * Project: springbootDemo
 *
 * @author : hirolin
 * @date : 2019/3/25 22:14
 */
public class User {

    private int id;

    private String name;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

创建xml文件:





    
        
        
    

    


main函数创建 application.yml文件创建
package com.hiro.demo;

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

/**
 * Project: springbootDemo
 *
 * @author : hirolin
 * @date : 2019/3/25 22:13
 */
@SpringBootApplication
@MapperScan("com.hiro.demo.db")
public class DemoApplication {
    public static void main(String [] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/demo
    username: root
    password: xxxxx #自己的数据库配置
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*xml
单元测试
package com.hiro.demo;

import com.hiro.demo.db.dao.UserMapper;
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.SpringJUnit4ClassRunner;

/**
 * Project: springbootDemo
 *
 * @author : hirolin
 * @date : 2019/3/25 22:15
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoApplicationTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void Test(){
        System.out.printf(userMapper.getUser(1).getName());
    }

}

完整目录结构:


在这里插入图片描述

你可能感兴趣的:(springboot mybatis使用xml方式配置方式)