• springboot 梳理1--简单整合mybatis


    1. pom.xml添加

            <!--mybatis起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.2</version>
            </dependency>
            <!--mysql连接驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>    

    2. application.yml

    #db Configuration:
    spring:
      #默认datasource hikaricp
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8&&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
    
    mybatis:
      mapper-locations: mapper/*.xml
      type-aliases-package: com.xinzhi.studyspringboot.entity

    3. entity层User

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int id;
        private String username;
        private String password;
    }

    3. dao层UserMapper

    @Mapper
    public interface UserMapper {
        /**
         * 查询用户
         * @return
         */
        public List<User> queryUserList();
    }

    4. resources/mapper/UserMapper.xml

    <?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.xinzhi.studyspringboot.dao.UserMapper">
        <select id="queryUserList" resultType="User">
            select * from user
        </select>
    </mapper>

    5. service层

    (1)IUserService = UserMapper

    (2)UserServiceImpl 实现IUserService,私有变量UserMapper,方法实现

    @Service
    public class UserServiceImpl implements IUserService {
        @Resource
        private UserMapper userMapper;
    
        @Override
        public List<User> queryUserList() {
            return userMapper.queryUserList();
        }
    }

    6. controller层UserController实现,私有变量IUserService

    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Resource
        private IUserService userService;
    
        @GetMapping
        @ResponseBody
        public List<User> getUsers(){
            return userService.queryUserList();
        }
    }
  • 相关阅读:
    python函数篇
    字符编码和文件处理
    对话代码
    复习2
    [转]借闪光灯的东风 成就你完美的摄影作品
    色系
    Oracle的一些基本操作
    iebook line flash
    网站收录
    复习1
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14344423.html
Copyright © 2020-2023  润新知