一、pom.xml 加载包(该mybatis-plus集成了mybatis,还需要集成分页)
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
二、mybatis及分页参数配置
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" /> <!-- 全局映射器启用缓存 -->
<setting name="useGeneratedKeys" value="true" /> <!-- 允许 JDBC 支持自动生成主键 -->
<setting name="defaultExecutorType" value="REUSE" /> <!-- 配置默认的执行器 -->
<setting name="logImpl" value="SLF4J" /> <!-- 指定 MyBatis 所用日志的具体实现 -->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> 驼峰式命名 -->
</settings>
</configuration>
bootstrap.yml 或 application.xml
mybatis-plus:
configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # sql日志
cache-enabled: false #缓存
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
map-underscore-to-camel-case: true
global-config:
banner: false # 关闭控制LOGO图案
mapper-locations: classpath*:mapper/**/*Mapper.xml
typeAliasesPackage: com.test#实体扫描,多个package用逗号或者分号分隔
三、总体文件配置
1.domain.java 实体类
2.mapper.java 操作数据库orm层
3.mapper.xml 操作数据库层映射配置文件
4.service.java 业务层
5.serviceImpl.java 业务实现层
6.controller.java 控制层
domain.java
package com.test.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import com.test.common.core.domain.BaseEntity;
/**
* <p>
* APP版本迭代
* </p>
*
*@author test
*@date 2020-12-05 23:39:00
*/
@Data
@Accessors(chain = true)
@TableName("c_app_version")
@EqualsAndHashCode(callSuper = false)
public class CAppVersion extends BaseEntity {
/** 版本ID */
@TableId(type = IdType.AUTO)
private String versionId;
/** 主版本号 */
private String majorVersion;
/** 次版本号 */
private String minorVersion;
/** 内部版本号 */
private String bulidNumber;
/** 修订号 */
private String revisionNumber;
/** APP编码 */
private String appEncoded;
/** 内容 */
private String content;
/** 更新包路径id */
private String packageFileId;
/** 更新文档路径id */
private String docFileId;
/** 是否有效 */
private String delFlag;
}
mapper.java
package com.test.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.domain.CAppVersion;
/**
* <p>
* APP版本迭代 BaseMapper
* </p>
*
* @author test
* @date 2020-12-05 23:39:00
*/
public interface CAppVersionMapper extends BaseMapper<CAppVersion> {
}
mapper.xml
<?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="com.test.mapper.CAppVersionMapper">
<resultMap type="CAppVersion" id="CAppVersionResult">
<result property="versionId" column="version_id"/>
<result property="majorVersion" column="major_version"/>
<result property="minorVersion" column="minor_version"/>
<result property="bulidNumber" column="bulid_number"/>
<result property="revisionNumber" column="revision_number"/>
<result property="appEncoded" column="app_encoded"/>
<result property="content" column="content"/>
<result property="packageFileId" column="package_file_id"/>
<result property="docFileId" column="doc_file_id"/>
<result property="remark" column="remark"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
<result property="delFlag" column="del_flag"/>
</resultMap>
<sql id="selectCAppVersionVO">
select version_id, major_version, minor_version, bulid_number, revision_number, app_encoded, content, package_file_id, doc_file_id, remark, create_by, create_time, update_by, update_time, del_flag from c_app_version
</sql>
</mapper>
service.java
package com.test.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.test.domain.CAppVersion;
/**
* <p>
* APP版本迭代 IService
* </p>
*
* @author test
* @date 2020-12-05 23:39:00
*/
public interface ICAppVersionService extends IService<CAppVersion> {
}
serviceImpl.java
package com.test.service.impl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.mapper.CAppVersionMapper;
import com.test.domain.CAppVersion;
import com.test.service.ICAppVersionService;
/**
* <p>
* APP版本迭代Service业务层处理
* </p>
*
* @author test
* @date 2020-12-05 23:39:00
*/
@Service
public class CAppVersionServiceImpl extends ServiceImpl<CAppVersionMapper, CAppVersion> implements ICAppVersionService {
}
controller.java
package com.test.controller;
import com.test.common.core.controller.BaseController;
import com.test.domain.CAppVersion;
import com.test.service.ICAppVersionService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
/**
* <p>
* APP版本迭代 前端控制器
* </p>
*
* @author test
* @date 2020-12-05 23:39:00
*/
@RestController
@RequestMapping("/test/version")
public class CAppVersionController extends BaseController {
@Resource
private ICAppVersionService cAppVersionService;
/**
* 根据versionId获取APP版本迭代
*
* @param versionId APP版本迭代ID
* @return APP版本迭代
*/
@GetMapping("/get/{versionId}")
public CAppVersion selectCAppVersionById(@PathVariable("versionId") String versionId) {
return cAppVersionService.getById(versionId);
}
/**
* 查询APP版本迭代列表
*
* @param cAppVersion APP版本迭代
* @return APP版本迭代集合
*/
@GetMapping("/list")
public List<CAppVersion> selectCAppVersionList(CAppVersion cAppVersion) {
return cAppVersionService.list();
}
/**
* 新增保存APP版本迭代
*
* @param cAppVersion APP版本迭代
* @return 结果
*/
@PostMapping("/add")
public boolean insertCAppVersion(@RequestBody CAppVersion cAppVersion) {
return cAppVersionService.save(cAppVersion);
}
/**
* 修改保存APP版本迭代
*
* @param cAppVersion APP版本迭代
* @return 结果
*/
@PostMapping("/edit")
public boolean updateCAppVersionById(@RequestBody CAppVersion cAppVersion) {
return cAppVersionService.updateById(cAppVersion);
}
/**
* 批量删除APP版本迭代
*
* @param ids 需要删除的数据ID
* @return 结果
*/
@PostMapping("/remove")
public boolean removeCAppVersionByIds(String ids) {
return cAppVersionService.removeByIds(Collections.singletonList(StringUtils.commaDelimitedListToStringArray(ids)));
}
}
四、这套代码网上有专门的代码生成器可生成