jeecgBoot项目总结
MSCM架构
Controller
首先controller需要对JeecgController的继承,而且是Table是持久化的对象,Service是服务层。
@Api(tags="...")
@RestController
@RequestMapping("...")
@Slf4j
public class Controller extends JeecgController<Table, Service> {
/**
* 请求参数是@RequestBody,进行分页查询
*/
@AutoLog(value = "...")
@ApiOperation(value="...", notes="...")
@PostMapping(value = "/list")
public Result<Page> listPage(@RequestBody @Validated Object object) {
Page page = service.listPage(object);
return Result.OK(page);
}
/**
* 分页列表查询
*
* @param object
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@GetMapping(value = "/list")
public Result<?> queryPageList(Object object, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
QueryWrapper<Object> queryWrapper = QueryGenerator.initQueryWrapper(object, req.getParameterMap());
Page<Object> page = new Page<Object>(pageNo, pageSize);
//queryWrapper实体对象封装操作类, 这个page是mybatisplus封装好的。
IPage<SysMessage> pageList = sService.page(page, queryWrapper);
return Result.ok(pageList);
}
/**
* 导出excel
*
* @param
* @param
*/
@AutoLog(value = "...")
@ApiOperation(value = "...", notes = "...")
@PostMapping(value = "/...")
public ModelAndView export(HttpServletRequest request, Object object) {
return super.exportXls(request, object, Object.class, "");
}
/**
* 导出excel
*
* @param request http请求
* @param 对象
*/
@AutoLog(value = "导出所有的数据")
@ApiOperation(value = "导出所有的数据", notes = "导出所有的数据")
@GetMapping(value = "/exportAll")
public ModelAndView exportAll(HttpServletRequest request, Object object) {
// Step.1 组装查询条件
QueryWrapper<OfflineMetadataBusiness> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
// Step.2 AutoPoi 导出Excel
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
List<Obejct> list = sService.list(queryWrapper);
//获得登录用户
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
// 导出文件名称
mv.addObject(NormalExcelConstants.FILE_NAME, "");
mv.addObject(NormalExcelConstants.CLASS, OfflineMetadataBusiness.class);
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("数据", "导出人:" + sysUser.getRealname(), "导出信息"));
mv.addObject(NormalExcelConstants.DATA_LIST, list);
return mv;
}
}
Service
接口层继承了IService
,这个Table是持久层的对象public interface service extends IService<Table> {
//分页查询
Page listPage(Object object);
//
}
ServiceImpl
接口的实现层
从mapper查询的是List,直接使用page.setRecords(list);就可以实现分页的功能,
@Override
public Page listPage(Object object) {
Page page = new Page(vo.getPageNo(),vo.getPageSize());
List<Table> list = Mapper.listPage(page, object);
return page.setRecords(list);
}
mapper interface
mapper层的接口
特点:继承了BaseMapper,然后table是持久层的对象
public interface Mapper extends BaseMapper<Table> {
List<Table> listPage(Page page, Object object);
List<Object> listByParamPage(Page page, @Param("xml的sql需要用到的字段") String name);
}
XML
xml层的SQL
参考:
id: tips1
like conca :tips2
where 1 = 1 : tips3
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="这个填Mapper层接口对应的位置">
<select id="唯一的id"
resultType="对应需要返回的对象的位置的Table">
SELECT
*
FROM
数据库的table
WHERE
1=1
<if test="object.val != null and object.val != ''">
and table.val = #{object.val}
</if>
<if test="object.val != null and object.val != ''">
and table.val like concat('%',#{object.val},'%')
</if>
ORDER BY object.val DESC
</select>
DAO
持久化对象的特点:
@Data
@TableName("...") //...是数据库的表的名字
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="...", description="...")
public class Table implements Serializable {
private static final long serialVersionUID = 1L;
/**主键*/
@TableId(type = IdType.ASSIGN_ID) //只有当插入对象ID 为空,才自动填充,分配ID (主键类型为number或string), 默认实现类
@ApiModelProperty(value = "...")
private String id;
/**时间格式*/
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd") //把英文的时间转换成中文的格式
@ApiModelProperty(value = "...")
private Date time;
/**导出Excel格式*/
@Excel(name = "...", width = 15) //设置导出时的列名,与列的宽度
@ApiModelProperty(value = "...")
private String ...;
}
SpringData JPA架构
Controller层
@Api(tags = "...")
@RestController
@RequestMapping("/.../...")
@Slf4j
@EnableTransactionManagement
public class Controller extends JeecgController<Object, Service> {
/**
*
*
* @return List<Object>
*/
@AutoLog(value = "...")
@ApiOperation(value = "...", notes = "...")
@GetMapping(value = "/findFilter")
public Result<List<Object>> find() {
List<Object> list = =sService.findBy=Filter();
log.info("...");
return Result.OK(list);
}
/**
* 分页列表查询
*
*/
@AutoLog(value = "...")
@ApiOperation(value = "...", notes = "...")
@GetMapping(value = "/find")
public Result<Page<Object>> find(@RequestParam(name = "...", defaultValue = "") String Name,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
log.info("...");
Page<Object> all = sService.find(参数, pageNo, pageSize));
return Result.OK(all);
}
@PutMapping(value = "/...")
public Result<String> update(@RequestBody Object object) {
String res = sService.update(object);
log.info("...");
return Result.OK(res);
}
}
持久化对象:
@Entity
@Data
@Table(name = "数据库表的名字")
@ApiModel(value="...", description="...")
public class Entity implements Serializable {
private static final long serialVersionUID = 1L;
/**主键*/
@TableId(type = IdType.ASSIGN_ID) //这里可以设置自增,或者是
@ApiModelProperty(value = "主键")
@Id
private String id;
@Transient //这里是不参加数据库的映射,而我返回结果中有需要一个这样的集合
//这个Entity可以是本身
@ApiModelProperty(value = "...")
private List<Entity> children = new ArrayList<>()
@Transient
@ApiModelProperty(value = "...")
private String ...;
}
Repository层
public interface Repository extends PagingAndSortingRepository<Entity, String> {
/**
* ...
*
* @param
* @param pageable
* @return
*/
Page<Entity> findAll(String ..., Pageable pageable);
/**
*
*
* @param
* @param
* @param pageable
* @return
*/
Page<Entity> findAllBy...And...(String ..., String ..., Pageable pageable);
dByPid(String pid);
/**
*
*
* @param ...
* @param ...
* @return
*/
List<Entity> findBy...And...(String ..., String ...);
/**
*
*
* @param ...
* @param ...
* @return
*/
Entity findByT...And...(String ..., String ...);
/**
*
* @param
* @param
* @param pageable
* @return
*/
Page<Entity> findAll...And...(String ..., String ..., Pageable pageable);
}
properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver //如果是连接8.0 就需要加上一个cn
spring.jpa.show-sql=true
yml
server:
port: ...
spring:
application:
name: ...
总结:
Spring Data JPA 是更大的 Spring Data 家族的一部分,可以轻松实现基于 JPA 的存储库。该模块处理对基于 JPA 的数据访问层的增强支持。它使构建使用数据访问技术的 Spring 驱动的应用程序变得更加容易。
实施应用程序的数据访问层已经很麻烦了。必须编写太多样板代码来执行简单的查询以及执行分页和审计。Spring Data JPA 旨在通过减少实际需要的工作量来显着改进数据访问层的实现。作为开发人员,您编写存储库接口,包括自定义查找器方法,Spring 将自动提供实现。
特征
- 支持基于 Spring 和 JPA 构建存储库
- 支持Querydsl谓词,因此支持类型安全的 JPA 查询
- 域类的透明审计
- 分页支持、动态查询执行、集成自定义数据访问代码的能力
@Query
在引导时验证带注释的查询- 支持基于 XML 的实体映射
- 通过引入基于 JavaConfig 的存储库配置
@EnableJpaRepositories
。