Java常见问题解决方案
2023-12-28 15:43:01
目录
数据对比
Mybaits
增删改查
Mybaits-plus
1. 增删改查
1.1 实体类
/**
* xx实体类
*/
@TableName("demo_table")
public class DemoTableEntity implements Serializable {
/**
* ID
*/
@TableId
private String demoId;
/**
* 名称
*/
private String demoName;
public String getDemoId() {
return demoId;
}
public void setDemoId(String demoId) {
this.demoId= demoId;
}
public String getDemoName() {
return demoName;
}
public void setDemoName(String demoName) {
this.demoName= demoName;
}
}
1.2 Controller
@RestController
@RequestMapping("/demoTable")
public class DemoTableController extends BaseController {
@Autowired
private DemoTableService demoTableService;
/**
* 新增
* @param demoTableEntity
* @return
*/
@PostMapping("/save")
public AjaxResult save(@RequestBody DemoTableEntity demoTableEntity){
return toAjax(demoTableService.save(demoTableEntity));
}
/**
* 修改
* @param demoTableEntity
* @return
*/
@PostMapping("/update")
public AjaxResult update(@RequestBody DemoTableEntity demoTableEntity){
return toAjax(demoTableService.update(demoTableEntity));
}
/**
* 查询列表
* @param demoTableEntity
* @return
*/
@GetMapping("/selectList")
public TableDataInfo selectList(DemoTableEntity demoTableEntity){
startPage();
List<DemoTableEntity> list = demoTableService.selectList(demoTableEntity);
return getDataTable(list);
}
/**
* 删除
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public AjaxResult deleteById(@PathVariable String id){
return toAjax(demoTableService.deleteById(id));
}
}
1.3 Service
public interface DemoTableService {
/**
* 新增服务实例
* @param demoTableEntity
* @return
*/
Integer save(DemoTableEntity demoTableEntity);
/**
* 修改服务实例
* @param demoTableEntity
* @return
*/
Integer update(DemoTableEntity demoTableEntity);
/**
* 查询服务实例列表
* @param demoTableEntity
* @return
*/
List<DemoTableEntity> selectList(DemoTableEntity demoTableEntity);
/**
* 根据id删除服务实例
* @param id
* @return
*/
Integer deleteById(String id);
}
1.4 ServiceImpl
@Service
public class DemoTableServiceImpl implements DemoTableService {
@Autowired
private DemoTableMapper demoTableMapper;
/**
* 新增服务实例
* @param demoTableEntity
* @return
*/
@Override
public Integer save(DemoTableEntity demoTableEntity){
return demoTableMapper.insert(demoTableEntity);
}
/**
* 修改
* @param demoTableEntity
* @return
*/
@Override
public Integer update(DemoTableEntity demoTableEntity){
UpdateWrapper<DemoTableEntity> wrapper = new UpdateWrapper<>();
return demoTableMapper.update(demoTableEntity, wrapper);
}
/**
* 查询列表
* @param demoTableEntity
* @return
*/
@Override
public List<DemoTableEntity> selectList(DemoTableEntity demoTableEntity){
QueryWrapper queryWrapper = new QueryWrapper();
return demoTableMapper.selectList(queryWrapper);
}
/**
* 根据id删除
* @param id
* @return
*/
@Override
public Integer deleteById(String id){
return demoTableMapper.deleteById(id);
}
}
1.5 Mapper
public interface DemoTableMapper extends BaseMapper<DemoTableEntity> {
}
注解
1. @MapperScan
bug场景: 访问 Controller 的接口返回404
// 解决方案: 启动类添加 @MapperScan注解
@MapperScan("com.gientech.armp.empower.mapper.*")
@MapperScan
注解指定了 Mapper 接口所在的包路径为 com.gientech.armp.empower.mapper
。这样,在应用启动时,Spring Boot 就会自动扫描该包下的所有 Mapper 接口,并将其实例化后交给 Spring 容器管理。
文章来源:https://blog.csdn.net/weixin_45137565/article/details/135267455
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!