天天看点

SpringBoot新手入门一、工具二、初始化项目三、springboot整合mybatis

一、工具

建议使用IDEA

IDEA工具下载安装破解,请参考:IntelliJ IDEA 2020.3.1激活破解教程

二、初始化项目

问题1:初始化springboot项目时,出现Cannot resolve net.bytebuddy:byte-buddy:1.10.19

问题描述:

今天开始学习springboot知识,创建第一个项目,就出现 Cannot resolve net.bytebuddy:byte-buddy:1.10.19问题

SpringBoot新手入门一、工具二、初始化项目三、springboot整合mybatis
思路分析:

maven依赖包未导入成功

解决方案:

手动导入依赖即可

操作步骤:

SpringBoot新手入门一、工具二、初始化项目三、springboot整合mybatis

三、springboot整合mybatis

3.1 导入依赖

主要导入此依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
           

3.2 编写实体类

package com.chris.habby.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseUser {
    private Integer userid;
    private String username;
    private String password;
}

           

3.3 编写接口

package com.chris.habby.mapper;

import com.chris.habby.pojo.BaseUser;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

//这个注解表示这是一个mybatis的mapper类
@Mapper
@Repository
public interface UserMapper {
    List<BaseUser> queryUserList();

}

           

3.4 编写xml文件

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chris.habby.mapper">
    <select id="queryUserList" resultType="BaseUser">
        select * from baseuser
    </select>
</mapper>
           

3.5 配置别名和xml文件路径

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/dbtest1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

#整合mybatis
mybatis:
  type-aliases-package: com.chris.habby.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml
           

3.6 遇到的问题:Invalid bound statement (not found): com.chris.habby.mapper.UserMapper.adduser

原因分析:

绑定无效,是因为在xml中,只写了包名,没有写类名,导致绑定无效

SpringBoot新手入门一、工具二、初始化项目三、springboot整合mybatis

继续阅读