springboot整合Mybatis(黑马java笔记)

springboot整合Mybatis

    • 添加Mybatis的起步依赖
    • 创建User类
    • 编写UserMapper
    • 配置Mapper映射文件
    • 配置application.properties
    • 编写测试controller

添加Mybatis的起步依赖

pom.xml文件



  4.0.0
  springboot.mybatis
  springboot.mybatis.demo
  0.0.1-SNAPSHOT
  
  
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.1.1
		

		
		
			mysql
			mysql-connector-java
		

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

  

创建User类

package springboot.mybatis.demo;
public class User {

    private Integer id;
    private String username;
    private String birthday;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

编写UserMapper

package springboot.mybatis.demo;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
	public List queryUserList();
}

配置Mapper映射文件




    
    

配置application.properties

#
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=1234


mybatis.type-aliases-package=springboot.mybatis.demo
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

编写测试controller

package springboot.mybatis.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MybatisController {
	@Autowired
	private UserMapper userMapper;
	@RequestMapping("/query")
	@ResponseBody
	public List queryUserList(){
		List users = userMapper.queryUserList();
		return users;
	}
	
}

package springboot.mybatis.demo;

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

@SpringBootApplication
public class SpringbootMybatisApplication {

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


你可能感兴趣的:(springboot整合Mybatis(黑马java笔记))