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;
}
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 {
}
在启动类上增加@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);
}
}
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 {
}
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);
}
}
##mybatis的原生态支持
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
type-aliases-package: com.kuangstudy.entity
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);
}
在resources/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>SELECT id,
nickname,
password,
age,
male
FROM kss_user
WHERE id = #{id}
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,可以不用注入了