SSM项目做得的多了,你就会发现大部分代码千篇一律,我一般都是通过工具生成这些CRUD代码,然后再编写业务代码,下面分享一下我开发的代码生成工具:
实现效果:
数据库设计好表结构或者修改表结构之后,通过此工具生成ORM层代码,service层代码,以及controller层代码。其中service 和 controller 层代码,只生成一次,避免自己写业务逻辑被覆盖。ORM层代码会覆盖。因此,ORM层的代码我分为自动生成的文件和手写的文件。
生成的代码如下:
Controller 层:
package com.jjp.api;
import java.lang.Integer;import java.lang.Object;import java.lang.String;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import com.jjp.enums.ErrorTypeEnum;
import com.jjp.enums.NormalStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import com.jjp.bean.PagingProperties;
import com.jjp.model.Org;
import com.jjp.model.OrgExample;
import com.jjp.service.OrgService;
import com.jjp.api.validator.OrgValidator;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/api/v1/org")
public class OrgController extends BaseController {
@Autowired
private OrgService orgService;
@InitBinder(value = "org")
public void initBinder(WebDataBinder binder) {
binder.setValidator(new OrgValidator());
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> insert(@Valid @RequestBody Org org, BindingResult result) {
if (result.hasErrors()) {
return failedResult(ErrorTypeEnum.VALIDATE_ERROR, result.getAllErrors().get(0).getDefaultMessage());
}
org.setCreateDate(new Date());
org.setStatus(NormalStatusEnum.NORMAL.getByte());
orgService.insert(org);
return successResult(org);
}
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
@ResponseBody
public Map<String, Object> delete(@PathVariable("id") Integer id) {
Org delete = new Org();
delete.setId(id);
// delete.setStatus(NormalStatusEnum.DELETED.getValue());
orgService.updateByPrimaryKeySelective(delete);
return successResult("Ok");
}
@RequestMapping(value = "list", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> list(PagingProperties paging) {
OrgExample exam = new OrgExample();
if(paging.getNeedPaging()) {
paging.setTotal(orgService.countByExample(exam));
exam.setOrderByClause(" id desc " + paging.build());
}
List<Org> list = orgService.selectByExample(exam);
return successPageList(paging,list);
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> get(@PathVariable("id") Integer id) {
return successResult(orgService.selectByPrimaryKey(id));
}
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
@ResponseBody
public Map<String, Object> update(@PathVariable("id") Integer id, @Valid @RequestBody Org org, BindingResult result) {
if (result.hasErrors()) {
return failedResult(ErrorTypeEnum.VALIDATE_ERROR, result.getAllErrors().get(0).getDefaultMessage());
}
orgService.updateByPrimaryKeySelective(org);
return successResult("ok");
}
}
Service 层
import com.jjp.model.Org;
import com.jjp.model.OrgExample;
public interface OrgService extends BaseService<Org, OrgExample, Integer>{
}
impl
package com.jjp.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jjp.dao.OrgMapper;
import com.jjp.model.Org;
import com.jjp.model.OrgExample;
import com.jjp.service.OrgService;
@Service
public class OrgServiceImpl extends BaseServiceImpl<Org, OrgExample, Integer> implements OrgService {
@Autowired
public void setBaseMapper(OrgMapper mapper) {
this.baseMapper = mapper;
}
}
所有的增删查改代码我都通过泛型写到父类里面了,避免到处都是重复代码
比如:
package com.jjp.service.impl;
import com.jjp.dao.BaseMapper;
import com.jjp.service.BaseService;
import java.util.List;
public abstract class BaseServiceImpl<T, E, K> implements BaseService<T, E, K> {
protected BaseMapper<T,E,K> baseMapper;
@Override
public int countByExample(E e){
return baseMapper.countByExample(e);
}
@Override
public void insert(T t) {
baseMapper.insert(t);
}
@Override
public void insertSelective(T t) {
baseMapper.insertSelective(t);
}
// @Override
// public void deleteByPrimaryKey(K k) {
// baseMapper.deleteByPrimaryKey(k);
//
// }
@Override
public void deleteByExample(E e) {
baseMapper.deleteByExample(e);
}
@Override
public T selectByPrimaryKey(K k) {
return baseMapper.selectByPrimaryKey(k);
}
@Override
public List<T> selectByExample(E e) {
return baseMapper.selectByExample(e);
}
@Override
public void updateByExampleSelective(T t, E e){
baseMapper.updateByExampleSelective(t, e);
}
@Override
public void updateByExample(T t, E e) {
baseMapper.updateByExample(t, e);
}
@Override
public void updateByPrimaryKeySelective(T t) {
baseMapper.updateByPrimaryKeySelective(t);
}
// @Override
// public void updateByPrimaryKey(T t) {
// baseMapper.updateByPrimaryKey(t);
//
// }
public void setBaseMapper(BaseMapper<T, E, K> baseMapper) {
this.baseMapper = baseMapper;
}
}
ORM 层也是一样
package com.jjp.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper<T,E,K> {
int countByExample(E e);
int deleteByExample(E e);
int deleteByPrimaryKey(K k);
int insert(T t);
int insertSelective(T t);
List<T> selectByExample(E e);
T selectByPrimaryKey(K k);
int updateByExampleSelective(@Param("record") T t, @Param("example") E e);
int updateByExample(@Param("record") T t, @Param("example") E e);
int updateByPrimaryKeySelective(T t);
int updateByPrimaryKey(T t);
}
最后看下工具界面
gitee 地址: https://gitee.com/petercao/ssm-generator
感兴趣的话可以加微信聊聊:mrxiaocao