• SpringBoot整合Mybatis


    1、依赖在上一篇随笔中已经导入,这里不再展示

    2、数据源信息也上一篇随笔中已经配置过

    3、编写pojo对象MUser

     1 package com.offcn.pojo;
     2 
     3 import lombok.AllArgsConstructor;
     4 import lombok.Data;
     5 import lombok.NoArgsConstructor;
     6 
     7 import javax.persistence.*;
     8 
     9 @Entity
    10 @Data
    11 @NoArgsConstructor
    12 @AllArgsConstructor
    13 @Table(name = "user")
    14 public class MUser {
    15 
    16    @Id
    17    @GeneratedValue(strategy = GenerationType.IDENTITY)
    18    private Integer uid;
    19    private String uname;
    20    private String pwd;
    21    private String gender;
    22    private String phone;
    23 }

    4MUserMapper接口

    1 package com.offcn.mapper;
    2 
    3 import com.offcn.pojo.MUser;
    4 
    5 import java.util.List;
    6 
    7 public interface MUserMapper {
    8    List<MUser> getUserList();
    9 }

    5、MUserMapper.xml映射文件

    1 <?xml version="1.0" encoding="UTF-8" ?>
    2 <!DOCTYPE mapper
    3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5 <mapper namespace="com.offcn.mapper.MUserMapper">
    6     <select id="getUserList" resultType="com.offcn.pojo.MUser">
    7     select * from user
    8   </select>
    9 </mapper>

    6、在主启动类上配置扫描mybatis的包

     1 package com.offcn;
     2 
     3 import org.mybatis.spring.annotation.MapperScan;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 
     7 /**
     8  * @author yy
     9  * @date 2019/11/5 9:56
    10  */
    11 @MapperScan("com.offcn.mapper")
    12 @SpringBootApplication
    13 public class HelloApplication {
    14    public static void main(String[] args) {
    15       SpringApplication.run(HelloApplication.class,args);
    16    }
    17 }

    如果启动错误可以将映射文件放在resources文件目录下试试

  • 相关阅读:
    git push提交出现Everything up-to-date提示问题
    启动Dubbo项目注册Zookeeper时提示zookeeper not connected异常原理解析
    linux环境搭建mysql5.7总结
    Hadoop学习笔记:运行wordcount对文件字符串进行统计案例
    kafka3.0创建topic出现zookeeper is not a recognized option
    sql_waf绕过
    win11环境映像劫持
    vulnhub靶场—devguru
    vulhub-Presidential靶场解题过程
    php命令执行无回显判断及利用方法
  • 原文地址:https://www.cnblogs.com/lifefamily/p/11801517.html
Copyright © 2020-2023  润新知