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"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.guigu</groupId>
        <artifactId>guigu-springboot-mybatis-demo04</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
        </parent>
    
        <dependencies>
            <!--驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!--mybatis的 起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
            <!--spring web起步依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

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<User> findAllUser();
    }

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

   <?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">
    <mapper namespace="com.guigu.dao.UserMapper">
        <select id="findAllUser" resultType="com.guigu.pojo.User">
            SELECT * from user
        </select>
    </mapper>

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<User> 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<User> 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<User> findAll(){
            return userService.findAllUser();
        }
    }

10.测试

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

上一篇:vue table动态新增input行,修改值后无法输入


下一篇:自动备份一个TXT文件里面的SVN库