HI,大家好,今天给大家分享一个懒人必备神技,不写一句 Service层和dao(数据库操作层)的代码而实现增删改查。
不写一句Service层和dao(数据库操作层)的代码而实现增删改查?
看到这里,你可能会说:老铁,不写重复的代码我知道,把重复的代码提取出来封装成类。这个我早就知道了,至于不写Service和dao的代码能直接操作数据库,难道直接写在Controllor层么?别开玩笑了。
别急,我说的不写重复的代码不是在一个项目中,而是在项目的前端API 和后台管理项目上,都没有重复的代码,至于不写Service和Dao层代码,不用卸载Controllor层,在保证原来MVC的架构上不写。下面我就来收一下整体思路。
不写重复代码,在一个项目中前台API和后台管理服务都可能对同一张表进行增删改查,这可能涉及到前端api写查询,后台管理也写到重复的查询,这样代码就冗余了一份查询的方法。那我的做法是把相同的部分提取出来,形成一个公共的依赖模块(Common),然后api和管理项目就分别依赖Common 就行,这样减少了前后两个项目中相同代码的出现。结构为,Server-api,Server-admin,Server-common三个模块。这样就可以把api和admin中同时出现的代码写到Server-common中,如数据库的链接,时间工具类、微服务调用工具、entity实体类、全局异常捕获类、全局常量等。
最后就是对Dao层和Service层的封装,把常用的增删该查代码封装为一个baseDao基类,一个Service层基类,一个Controllor层基类和一个entity基类。都使用泛型封装,代码如下:
Entity基类:
@Data
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = -8924489279354312949L;
private Long id;
private Long createTime = System.currentTimeMillis();
private Long updateTime;
}
Dao层基类:里面封装了常用的增删改查的方法
@NoRepositoryBean
public interface BaseDao extends JpaRepository, JpaSpecificationExecutor {
@Override
@Query(value = "update #{#entityName} set deleted=1 where id = ?1 ")
@Transactional
@Modifying
void deleteById(I id);
@Override
@Transactional
default void delete(T entity) {
deleteById((I) entity.getId());
}
@Transactional
default void delete(Iterable extends T> entities) {
entities.forEach(entitiy -> deleteById((I) entitiy.getId()));
}
@Override
@Query(value = "update #{#entityName} set deleted=1 where 1=1 ")
@Transactional
@Modifying
void deleteAll();
@Query(value = "update #{#entityName} set deleted=1 where id in ?1")
@Transactional
@Modifying
void deleteInBatch(List ids);
}
Server层代码:这里举例写了一些常用的方法。
@Slf4j
@Service
@Transactional
public class BaseService {
BaseDao dao;
@Autowired
public void setDao(BaseDao dao) {
this.dao = dao;
}
public Object addUser(I t) {
return dao.save(t);
}
public Object findByID(Long id) {
return dao.findById(id);
}
public void deleteById(Long id) {
dao.deleteById(id);
}
public void updateInfo(List t) {
dao.deleteInBatch(t);
}
public List findAll() {
return dao.findAll();
}
}
Controllor层代码:
@RestController
@Api(tags = "测试")
public class BaseControllerWithEntity {
BaseService service;
@Autowired
public void setService(BaseServiceNo1 service) {
this.service = service;
}
}
到这里,代码封装完成!你可能会问,说好的不写一句Dao层和Service层代码呢?别急,兄弟,请看我下文调用:
首先我需要写一个实体类继承BaseEntity,如下:
@Entity
@Table(name = "t_user")
public class User extends BaseEntity implements Serializable {
private static final long serialVersionUID = -2877490080979804529L;
private String nickName;
private Integer age;
private String email;
}
然后写一个Controllor实现对数据库的增删改查,如下:
@RestController
@RequestMapping("/test")
public class UserControllerWithEntity extends BaseControllerWithEntity {
@GetMapping("/user/add")
@ApiOperation("test")
public BaseResponseDto addUser() {
User user = new User();
user.setAge(25);
user.setEmail("1309095142@qq.com");
user.setNickName("test");
user = (User) service.addUser(user);
return success(user);
}
@GetMapping("/findall")
@ApiOperation("findall")
public BaseResponseDto> findall() {
return success(service.findAll());
}
}
可以看到,没有写一句DAO和Service层的代码,就可以实现对数据库的增删改查!