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 }
4、MUserMapper接口
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文件目录下试试