Java教程

SpringBoot快速整合Mybatis&MybatisPlus,java编程入门自学

本文主要是介绍SpringBoot快速整合Mybatis&MybatisPlus,java编程入门自学,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import lombok.NoArgsConstructor;

import lombok.ToString;

import lombok.experimental.Accessors;

@Data // getter/setter

@ToString // toString

@AllArgsConstructor // 有参构造函数

@NoArgsConstructor // 无参构造函数

@TableName(“user”)

@Accessors(chain = true)

@ApiModel(description = “用户实体”)

public class User {

// 用户编号

@TableId(type = IdType.AUTO)

@ApiModelProperty(value = “用户编号”, required = true)

private Integer id;

// 用户昵称

@ApiModelProperty(value = “用户昵称”, required = true)

private String nickname;

// 用户密码

@ApiModelProperty(value = “用户密码”, required = true)

private String password;

// 用户年龄

@ApiModelProperty(value = “用户年龄”, required = true)

private Integer age;

// 用户性别 0 女 1 男 2 保密

@ApiModelProperty(value = “用户性别”, required = true)

private Integer male;

}

2.2.5 新建mapper

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.example.entity.User;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:16

  • @Description:

*/

public interface UserMapper extends BaseMapper {

}

2.2.6 mybatis和spring融合

在启动类上增加@MapperScan("com.kuangstudy.mapper")

package com.example;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan(“com.example.mapper”)

public class StudyBootsMybatisApplication {

public static void main(String[] args) {

SpringApplication.run(StudyBootsMybatisApplication.class, args);

}

}

2.2.7 定义户Service接口和实现类

package com.example.service;

import com.baomidou.mybatisplus.extension.service.IService;

import com.example.entity.User;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:22

  • @Description:

*/

public interface UserService extends IService {

}

package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.entity.User;

import com.example.mapper.UserMapper;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:23

  • @Description:

*/

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

@Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}

2.2.8 测试用例

package com.example;

import com.example.entity.User;

import com.example.service.UserService;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest

class StudyBootsMybatisApplicationTests {

@Autowired

private UserService userService;

@Test

public void saveUser(){

User user = new User();

user.setId(1);

user.setNickname(“yykkxxxx”);

user.setPassword(“1121455”);

user.setAge(34);

user.setMale(1);

userService.saveOrUpdate(user);

}

}

3、基于xml的原生态的mybatis的支持

3.1 在application.yml增加支持xml的方式

##mybatis的原生态支持

mybatis-plus:

mapper-locations: classpath*:/mapper/*.xml

type-aliases-package: com.kuangstudy.entity

3.2 在UserMapper定义方法

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.example.entity.User;

import org.apache.ibatis.annotations.Param;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:16

  • @Description:

*/

public interface UserMapper extends BaseMapper {

User getByUserId(@Param(“id”) Integer id);

}

3.3 新建mapper.xml文件

在resources/mapper/UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

SELECT id,

nickname,

password,

age,

male

FROM kss_user

WHERE id = #{id}

3.4 UserService接口新建自定义的方法

package com.example.service;

import com.baomidou.mybatisplus.extension.service.IService;

import com.example.entity.User;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:22

  • @Description:

*/

public interface UserService extends IService {

/**

  • 根据用户id查询用户信息

  • @param id

  • @return

*/

User getByUserId(Integer id);

}

package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.entity.User;

import com.example.mapper.UserMapper;

import org.springframework.stereotype.Service;

/**

  • @Auther: 长颈鹿

  • @Date: 2021/07/23/10:23

  • @Description:

*/

@Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

@Override

public User getByUserId(Integer id) {

return this.baseMapper.getByUserId(id);

}

}

this.baseMapper:是UserMapper,可以不用注入了

这篇关于SpringBoot快速整合Mybatis&MybatisPlus,java编程入门自学的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!