IDEA项目使用SpringBoot+MyBatis-Plus的方法

步骤如下:

1.打开IDEA

2.File—>new—> project

3.选择spring initializr—>Next

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第1张图片

4.填写Grouphe和Artifact,选择Java version: 8 ,点击next ,如图:

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第2张图片

5.选择对应的依赖,点击Next

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第3张图片

6.核对项目的名字是否一致,点击finish后就完成了工程的创建。

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第4张图片

7.接下来就是pom文件的依赖包引入了(很重要!!!)


      org.springframework.boot
      spring-boot-starter-jdbc
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      com.alibaba
      fastjson
      1.2.73
    
    
      org.springframework.boot
      spring-boot-starter-aop
    
    
      com.oracle
      ojdbc6
      11.2.0.4.0-atlassian-hosted
    
    
    
      cn.easyproject
      orai18n
      11.2.0.4
    
    
    
      com.baomidou
      mybatis-plus-boot-starter
      3.3.1.tmp
    
    
    
      com.baomidou
      mybatis-plus-generator
      3.3.0
    
    
    
      org.apache.velocity
      velocity-engine-core
      2.2
    
    
    
      io.springfox
      springfox-swagger2
      2.9.2
    
    
      io.springfox
      springfox-swagger-ui
      2.9.2
    
    
    
      org.projectlombok
      lombok
      1.18.12
    

8.在appliaction.propertiles配置文件中写入数据库参数

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=用户
spring.datasource.password=密码
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

9.在Java下创建对应的pojo和mapper包,并创建对应的类

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第5张图片

10.在pojo包中新建和数据库userinfo表映射的类

@Data
@KeySequence("SEQ_USER_INFO")
public class UserInfo {

  @TableId(value = "USER_ID",type = IdType.INPUT)//在自增主键的变量加上即可
  private Long userId;
  private String userName;
  @TableField(value = "USER_NINAME") //可以不写,但字段名要用小驼峰命名
  private String userNiName;
  private String userPwd;
  private Date userCtime;
  private Integer userState;
  private Integer userSex;
  private String userEdu;
  private String userPro;
  private String userEmail;
  private String userTel;
  private Long userScore;
}

11.在mapper包中创建mapper接口,并集成mybatisPlus的BaseMapper

public interface UserInfoMapper extends BaseMapper {
}

12.在DemoApplication的main方法中添加注解@MapperScan,使其能够扫描mapper类
,添加@SpringBootApplication注解

@MapperScan("com.mybatitsplus.demo")
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

13.在测试类中:
添加注解@RunWith(SpringRunner.class)
添加注解@Resource
最后打印输出

@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {

  @Resource
  private UserInfoMapper userInfoMapper;

  @Test
  public void select() {
    List userInfos = userInfoMapper.selectList(null);
    userInfos.forEach(System.out::println);
  }
}

14.右击运行测试类,就输出打印信息了。

IDEA项目使用SpringBoot+MyBatis-Plus的方法_第6张图片

常用注解
MyBatisPlus提供了一些注解供我们在实体类和表信息出现不对应的时候使用。通过使用注解完成逻辑上匹配。

注解名称 说明
@TableName 实体类的类名和数据库表名不一致
@TableId 实体类的主键名称和表中主键名称不一致
@TableField 实体类中的成员名称和表中字段名称不一致

总结

到此这篇关于IDEA项目使用SpringBoot+MyBatis-Plus的方法的文章就介绍到这了,更多相关idea使用SpringBoot+MyBatis-Plus内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(IDEA项目使用SpringBoot+MyBatis-Plus的方法)