最后
终极手撕架构师的学习笔记:分布式+微服务+开源框架+性能优化
-
**@TableId :**指定type = IdType.AUTO 表示数据库id为自增型。
-
**@TableField(value = “name”) :**指定数据库字段名。
-
添加get set方法即可。
package com.example.demo.Entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; /** 用户基本信息实体 */ @TableName(“user”) public class UserEntity {
- @TableId(value = “id”, type = IdType.AUTO) //表示该id为自增,新增时候不需要手动设置id。
private Integer id;
@TableField(value = “name”)
private String name;
@TableField(value = “age”)
private Integer age;
@TableField(value = “sex”)
private String sex;
@TableField(value = “address”)
private String address;
public Integer getId() {
return id;
} public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
**
然后再多添加几条数据吧!方便后续查询接口浏览查询。
UserService
编写两个接口!分别为不带参数与带一个参数,作为演示:具体代码如下
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.Entity.UserEntity;
import java.util.List;
/**
- 用户管理业务层接口
*/
public interface UserService extends IService {
/**
- 不分页查询所有用户信息
*/
List getUsers();
/**
-
根据性别查询所有用户
-
@param sex 性别
*/
List getUsersBySex(String sex);
}
UserServiceImpl
继承接口实现上方两方法。
由于是使用了该mybatis-plus的查询方式,顾语法有些不一样。这个后续给大家详细讲讲。
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Entity.UserEntity;
import com.example.demo.dao.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
- 用户管理业务层
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
/**
- 不分页查询所有用户信息
*/
@Override
public List getUsers() {
//直接可使用IService 封装好的一些方法,这个自行点进去看。
return this.list();
}
/**
-
根据性别查询所有用户
-
@param sex 性别
*/
@Override
public List getUsersBySex(String sex) {
//条件构造器
QueryWrapper wrapper = new QueryWrapper<>();
//eq 代表“ = ”;例如 eq(“sex”, “男”) —> sex = ‘男’;等同于拼接在sql语句后边的where条件。
wrapper.eq(“sex”,sex);
//将条件带入返回
List list = this.list(wrapper);
//返回数据
return list;
}
}
UserMapper
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.Entity.UserEntity;
import org.springframework.stereotype.Component;
/**
- 用户管理持久层
*/
@Component
public interface UserMapper extends BaseMapper {
}
以上 添加就大功告成了;
最后是controller类,调用刚才我们写的那两接口进行访问,看看结果如何
分发器代码如下:
package com.example.demo.controller;
import com.example.demo.Entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
- 用户管理分发器
*/
@RestController@RequestMapping(“/user”)
public class UserController {
@Autowired
private UserService userService;
/**
- 不分页查询所有用户信息
*/
@GetMapping(“/get-users”)
public List getUserList() {
return userService.getUsers();
}
/**
-
根据性别查询所有用户
-
@param sex 性别
*/
@GetMapping(“/get-users-by-sex”)
public List getUsersBySex(@RequestParam(name = “sex”) String sex) {
return userService.getUsersBySex(sex);
}
}
接下来,不用我说大家也知道了吧!接口访问,一切从简,那就直接打开浏览器访问咯
奥对了在访问之前,咱们再做一件事,添加一下控制台sql打印输出,方便我们查看到底执行了什么?对吧。做法只需在application-dev.yaml 中配置如下即可。
日志设置
logging:
level:
持久层日志级别
com.example.demo.dao: debug
ok~咱们先来访问第一个接口;浏览器直接访问。
http://localhost:8080/user/get-users
ps:如果中途报错,找不到basemapper
那你请在启动类 DemoApplication 中添加如下这行,表示指定扫描 dao。
最后总结我的面试经验
2021年的金三银四一眨眼就到了,对于很多人来说是跳槽的好机会,大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。
另外,面试中遇到不会的问题不妨尝试讲讲自己的思路,因为有些问题不是考察我们的编程能力,而是逻辑思维表达能力;最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。
BAT面试经验
实战系列:Spring全家桶+Redis等
其他相关的电子书:源码+调优
面试真题:
图片转存中…(img-NKO4hPGq-1715644083635)]
其他相关的电子书:源码+调优
[外链图片转存中…(img-Vxd5284J-1715644083635)]
面试真题:
[外链图片转存中…(img-8VVvdZS6-1715644083636)]
[外链图片转存中…(img-MCXnrRXB-1715644083636)]