springboot集成mybatis


springboot集成mybatis

实现步骤:

1.准备数据库创建表
2.添加起步依赖
3.创建POJO
4.创建mapper接口
5.创建映射文件
6.配置yml 指定映射文件位置
7.创建启动类,加入注解扫描
8.创建service controller 进行测试

1.创建数据库表


-- ----------------------------
-- 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=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

2.创建工程并添加依赖pom.xml如下参考


<?xml version="1.0" encoding="UTF-8"?>
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0
?
  com.guigu
  guigu-springboot-mybatis-demo04
  1.0-SNAPSHOT
?
 
      org.springframework.boot
      spring-boot-starter-parent
      2.1.4.RELEASE
 

?
 
     
     
          mysql
          mysql-connector-java
          runtime
     

     
     
          org.mybatis.spring.boot
          mybatis-spring-boot-starter
          2.0.1
     

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

?
 

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

     

 

?
?

3.创建pojo


public class User implements Serializable{
  private Integer id;
  private String username;//用户名
  private String password;//密码
  private String name;//姓名
  //getter setter...
  //toString
}

4.创建mapper接口


package com.guigu.dao;
?
import com.guigu.pojo.User;
?
import java.util.List;
?
/***
* mapper接口
* @author javaboy
* @packagename com.guigu.dao
* @version 1.0
* @date 2020/2/23
*/
public interface UserMapper {
  public List findAllUser();
}

5.创建UserMapper映射文件,如下图所示



?
<?xml version="1.0" encoding="UTF-8" ?>


 

xml文件放Resource中的mappers文件下

6.创建配置application.yml文件:


spring:
datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  username: root
  password: 123456
#配置mapper的映射文件的位置
mybatis:
mapper-locations: classpath:mappers/*Mapper.xml

7.创建启动类,加入mapper接口注解扫描



?
@SpringBootApplication
@MapperScan(basePackages = "com.guigu.dao")
//MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spriing容器
public class MybatisApplication {
  public static void main(String[] args) {
      SpringApplication.run(MybatisApplication.class,args);
  }
}

8.创建service 实现类和接口


package com.guigu.service.impl;
?
import com.guigu.dao.UserMapper;
import com.guigu.pojo.User;
import com.guigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
import java.util.List;
?
/***
* 描述
* @author javaboy
* @packagename com.guigu.service.impl
* @version 1.0
* @date 2020/2/23
*/
@Service
public class UserServiceImpl implements UserService {
?
  @Autowired
  private UserMapper userMapper;
?
  @Override
  public List findAllUser() {
      return userMapper.findAllUser();
  }
}

接口如下:


package com.guigu.service.impl;
?
import com.guigu.dao.UserMapper;
import com.guigu.pojo.User;
import com.guigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
?
import java.util.List;
?
/***
* 描述
* @author javaboy
* @packagename com.guigu.service.impl
* @version 1.0
* @date 2020/2/23
*/
@Service
public class UserServiceImpl implements UserService {
?
  @Autowired
  private UserMapper userMapper;
?
  @Override
  public List findAllUser() {
      return userMapper.findAllUser();
  }
}

9.创建controller


package com.guigu.controller;
?
import com.guigu.pojo.User;
import com.guigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
?
import java.util.List;
?
/***
* 描述
* @author javaboy
* @packagename com.guigu.controller
* @version 1.0
* @date 2020/2/23
*/
@RestController
@RequestMapping("/user")
public class UserController {
?
  @Autowired
  private UserService userService;
?
  @RequestMapping("/findAll")
  public List findAll(){
      return userService.findAllUser();
  }
}

10.测试

浏览器中发送请求:http://localhost:8080/user/findAll