springboot 02 ------springboot整合mybatis

SpringBoot整合Mybatis

1. 添加mybatis的起步依赖

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

2. 添加数据库驱动坐标

       
            mysql
            mysql-connector-java
            5.1.6
        

3. 添加数据库连接信息

在application.properties中添加数据量的连接信息
 

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

4. 创建user表

在traveller数据库中创建user表

-- ----------------------------- Table structure for `user` -- ---------------------------DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT, 
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL, 
`name` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
 
-- ----------------------------- Records of user -- --------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三'); 
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

5. 创建实体Bean

package com.itheima.domain;



public class User {


    private Long id;
    private String username;
    private String password;
    private String name;
    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 getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

6. 编写Mapper

@Mapper 
public interface UserMapper {   
      public List queryUserList(); 
}

@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中 

7. 配置Mapper的映射文件

在src\main\resources\mapper路径下加入UserMapper.xml配置文件"
 




    

8. 在application.properties中添加mybatis的信息


mybatis.type-aliases-package=com.itheima.domain

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

9. 编写测试Controller

package com.itheima.controller;

import com.itheima.mapper.UserMapper;
import com.itheima.domain.User;
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;

import java.util.List;

@Controller
public class MapperController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/queryUser")
    @ResponseBody
    public List queryUser(){
        List users = userMapper.queryUserList();
        return users;
    }
}

10. 测试

http://localhost:8080/queryUser

[{"id":1,"username":"zhangsan","password":"123","name":"张三"},{"id":2,"username":"lisi","password":"123","name":"李四"}]

你可能感兴趣的:(mybatis,SpringBoot)