首页 > 编程知识 正文

基于springboot的项目,springboot的优点

时间:2023-05-04 07:12:35 阅读:19581 作者:1105

背景

好久没用java了。 最近在项目中用于java开发的后台。 将框架设置为SpringBoot并操作数据库使用JPA确实很有用。 像我这样久违的不用java的初学者也可以使用。 我觉得入门确实很简单,但现实中并不是一个项目就能一直通过简单的表格查询或简单的表格关联完成。 为了满足各种相关查询的需要,必须部署mybatis进行辅助

一个主表(案件表)和来源、类型、分区、文档来源等大约五个左右的表必须与主表相关联。 要检查每个表中的字段,当然可以使用JPA的检查方法。 也就是说,也可以通过导入并关联对应于各表的实体来完成这个功能,但总觉得这个操作有缺点。 与前辈商量后,建议使用mybatis处理多个桌子,然后果断使用。一、需求

1、引入依赖

ependencygroupidorg.my batis.spring.boot/groupidartifactidmybatis-spring-boot-starter/artifactidversion1.3. 3

##############mybatis打开驼峰# # # # # # # # # my batis.configuration.map-under scap-under scap

package cn.enilu.flash.api; import cn.enilu.flash.Dao.baserepositoryfactorybean; importorg.my batis.spring.annotation.mappers can; importorg.spring framework.boot.spring应用程序; importorg.spring framework.boot.auto configure.springbootapplication; importorg.spring帧. boot.auto configure.domain.entity scan; importorg.spring framework.boot.builder.springapplicationbuilder; importorg.spring framework.boot.web.servlet.support.springbootservletinitializer; importorg.spring帧. cache.annotation.enable caching; importorg.spring帧. context.annotation.components can; importorg.spring framework.data.JPA.repository.config.enablejpaauditing; importorg.spring framework.data.JPA.repository.config.enablejparepositories;/* * * API应用程序* * @ Authorenilu * @版本2018/9/110011 */@ enable caching @ springbootapplication @ components @ entity scan (base packages=' cn.enilu.flash.bean.entity ' ) enen ablejparepositories ) ) base packag repositoryfactorybeanclass=baserepositoryfactorybean.class ) @ enablejpaauditing @ mappers can (base packages=' cn.enilling publicclassapiapplicationextendspringbootservletinitializer nbuilder configure (springapplication builder (spring app pure ((springa ication (return application.sources ) API aplication ) } publicstaticvoidmain (string [ ] args ) spring application.run (API application.class ); }

}

三、编码
创建实体对象,用于返回关联后的实体属性。

package cn.enilu.flash.bean.vo;import cn.enilu.flash.bean.entity.BaseEntity;import lombok.Data;import org.hibernate.annotations.Table;import org.springframework.data.jpa.domain.support.AuditingEntityListener;import javax.persistence.Entity;import javax.persistence.EntityListeners;import javax.persistence.GeneratedValue;import javax.persistence.Id;import java.util.Date;/** * @ClassName EventInfosVo * @Description TODO * @Author ZhouZhiqiang * @Date 2021/1/16 23:34 * @Version 1.0 **/@Entity(name = "view_event_infos")@Table(appliesTo = "view_event_infos")@Data@EntityListeners(AuditingEntityListener.class)public class ViewEventInfosVo extends BaseEntity{ private String taskNumber; private String eventSourceId; private String eventStateId; private Date eventTime; private String sponsor; private String coPerson; private String lawProcedure; private String litigantType; private String litigantId; private String legalPerson; private String litigantName; private String litigantAge; private Integer litigantSex; private String litigantTelephone; private String litigantAddress; private String eventAddress; private String districtId; private String eventType; private String eventDescription; private String causeAction; private String illegalBasis; private String illegalContent; private String punishBasis; private String punishContent; private Integer lawId; private Integer handler; private String x; private String y; private String img; private String existingDocuments; private String eventSourceName; private String eventTypeName; private String districtName; private String sponsorName; private String sponsorNumber; private String coPersonName; private String coPersonNumber; private String causeActionTitle; private String illegalBasisText; private String illegalContentText; private String punishBasisText; private String punishContentText;}

2、编写Provider,该类的主要功能是实现多表查询语句,各种查询条件直接进行sql拼接即可,需要注意的是多个参数需要用Map,因此传递参数的时候用@param注解

package cn.enilu.flash.provider;import java.util.Map;/** * @ClassName EventProvider * @Description TODO * @Author ZhouZhiqiang * @Date 2021/1/17 19:29 * @Version 1.0 **/public class EventProvider { public String getEventVoSql(Map<String, Object> para){ String sql=" select * from view_event_infos where 1=1"; if(para.get("eventStateId")!=null){ sql+=" and event_state_id="+para.get("eventStateId"); } if(para.get("handler")!=null){ sql+=" and handler="+para.get("handler"); } if(para.get("id")!=null){ sql+=" and id="+para.get("id"); } return sql; }}

3、dao层中添加Mapper,该类是实现调用Provider提供的查询语句,本案例中EventProvider就是要调用的对象,method是指sql返回的方法。

package cn.enilu.flash.dao;import cn.enilu.flash.bean.vo.ViewEventInfosVo;import cn.enilu.flash.provider.EventProvider;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.SelectProvider;import java.util.List;public interface EventMapper { @SelectProvider(type = EventProvider.class, method = "getEventVoSql") public List<ViewEventInfosVo> getEventViewList(@Param("id") long id, @Param("handler") long handler, @Param("eventStateId") String eventStateId);}

4、service层进行调用Mapper

package cn.enilu.flash.service.event;import cn.enilu.flash.bean.entity.event.EventInfos;import cn.enilu.flash.bean.entity.event.ViewEventInfos;import cn.enilu.flash.bean.vo.ViewEventInfosVo;import cn.enilu.flash.dao.EventMapper;import cn.enilu.flash.dao.event.EventInfosRepository;import cn.enilu.flash.service.BaseService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class EventInfosService extends BaseService<EventInfos,Long,EventInfosRepository> { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private EventInfosRepository eventInfosRepository; @Autowired private EventMapper eventMapper; //获取视图返回的数据 public List<ViewEventInfosVo> getEventInfosList(long id,long handler,String eventStateId){ List<ViewEventInfosVo> list=eventMapper.getEventViewList(id,handler,eventStateId); return list; }}

5、controller调用,返回给前端

@RequestMapping(value = "/list/vo",method = RequestMethod.GET)@ApiOperation("案件视图信息列表")//@RequiresPermissions(value = "/eventInfos")public Object listVo(@RequestParam(required = false) Long id,@RequestParam(required = false) String stateId,@RequestParam(required = false) Long handler) {Page<EventInfos> page = new PageFactory<EventInfos>().defaultPage();//page.addFilter("id",id);//page.addFilter("handler",handler);//过滤转办对象//page.addFilter("eventStateId",stateId);//过滤案件状态//page = eventInfosService.queryPage(page);List list = eventInfosService.getEventInfosList(id,handler,stateId);page.setRecords(list);return Rets.success(page);}

6、易错点总结
(1)注意开启mybatis的驼峰命名命令,否则返回的对象可能都为null.
(2)注意启动程序中增加扫描Mapper注解

@MapperScan(basePackages = "cn.enilu.flash.dao")

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。