源码地址:https://github.com/ostars/springboot-fast.git
摘要
文章主要介绍了如何搭建一个springboot项目,并封装常用的增删查改操作。本文主要包括一下部分:
1、springboot项目搭建步骤
2、通用mapper、model、service的封装
3、通用模块的使用
一、springboot搭建步骤
1.选择spring initializr
2.填写group id和artifact id
3.选择依赖
4.选择项目路径
5.搭建完成后的项目结构
二、springboot通用mapper、model、service的封装
Model.java
package org.ostars.springbootfast.common.model;
import java.util.Date;
/**
* @author isaac
* @date 2019/10/8
*/
public interface Model {
Long getId();
void setId(Long id);
Date getCreateTime();
void setCreateTime(Date createTime);
}
BaseModel.java
package org.ostars.springbootfast.common.model;
import java.util.Date;
/**
* common model
*
* @author isaac
* @date 2019/10/8
*/
public abstract class BaseModel implements Model {
public Long id;
public Date createTime;
}
BaseMapper.java
package org.ostars.springbootfast.common.mapper;
import java.util.List;
/**
* common mapper
*
* @author isaac
* @date 2019/10/8
*/
public interface BaseMapper<T> {
int deleteById(Long id);
int deleteByIds(List<Long> ids);
int insert(T model);
int insertList(List<T> models);
T selectById(Long id);
List<T> selectByModel(T model);
int updateByIdSelective(T model);
int updateById(T model);
}
BaseService.java
package org.ostars.springbootfast.common.service;
import org.ostars.springbootfast.common.mapper.BaseMapper;
import org.ostars.springbootfast.common.model.BaseModel;
import org.ostars.springbootfast.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
/**
* common service
*
* @author isaac
* @date 2019/10/8
*/
public class BaseService<E extends BaseModel> {
@Autowired
private BaseMapper<E> baseMapper;
public boolean deleteById(Long id) {
return id != null && this.baseMapper.deleteById(id) >= 0;
}
public boolean deleteByIds(List<Long> ids) {
return ids != null && (ids.isEmpty() || this.baseMapper.deleteByIds(ids) > 0);
}
public boolean insert(E model) {
return model != null && this.baseMapper.insert(model) == 1;
}
public boolean insertList(List<E> models) {
if (models == null) {
return false;
}
if (models.isEmpty()) {
return true;
}
if (models.size() > 100) {
for (List<E> list : ListUtil.getSubList(models, 100)) {
this.baseMapper.insertList(list);
}
} else {
return this.baseMapper.insertList(models) > 0;
}
return true;
}
public E selectById(Long id) {
return id == null ? null : this.baseMapper.selectById(id);
}
public List<E> selectByModel(E model) {
return model == null ? new ArrayList<>(0) : this.baseMapper.selectByModel(model);
}
public boolean updateByIdSelective(E model) {
return !(model == null || model.getId() == null) && this.baseMapper.updateByIdSelective(model) == 1;
}
public boolean updateById(E model) {
return !(model == null || model.getId() == null) && this.baseMapper.updateById(model) == 1;
}
public boolean deleteById(E model) {
return !(model == null || model.getId() == null) && this.baseMapper.deleteById(model.getId()) >= 0;
}
public boolean insertOrUpdate(E model) {
if (model == null) {
return false;
}
if (model.getId() == null) {
return this.baseMapper.insert(model) == 1;
}
return this.baseMapper.updateById(model) == 1;
}
}
三、通用模块的使用
application.yml
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springboot-fast?serverTimezone=UTC
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/**/*.xml
logging:
file: log/springboot-fast.log
SysLogModel.java
package org.ostars.springbootfast.model.base;
import lombok.Data;
import org.ostars.springbootfast.common.model.BaseModel;
import java.io.Serializable;
import java.util.Date;
/**
* log Model
*
* @author isaac
* @date 2019/10/08
*/
@Data
public class SysLogModel extends BaseModel implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Integer type;
private String path;
private String params;
private String ip;
private String browser;
private Long createUserId;
private String createUserName;
private Date createTime;
private String remark;
}
SysLogMapper.java
package org.ostars.springbootfast.mapper.base;
import org.ostars.springbootfast.common.mapper.BaseMapper;
import org.ostars.springbootfast.model.base.SysLogModel;
/**
* log mapper
*
* @author isaac
* @date 2019/10/08
*/
public interface SysLogMapper extends BaseMapper<SysLogModel> {
}
SysLogService.java
package org.ostars.springbootfast.service.base;
import org.ostars.springbootfast.common.service.BaseService;
import org.ostars.springbootfast.model.base.SysLogModel;
import org.springframework.stereotype.Service;
/**
* log Service
*
* @author isaac
* @date 2019/10/08
*/
@Service
public class SysLogService extends BaseService<SysLogModel> {
}
SysLogMapper.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="org.ostars.springbootfast.mapper.base.SysLogMapper">
<resultMap id="BaseResultMap" type="org.ostars.springbootfast.model.base.SysLogModel">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="type" jdbcType="INTEGER" property="type" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="params" jdbcType="VARCHAR" property="params" />
<result column="ip" jdbcType="VARCHAR" property="ip" />
<result column="browser" jdbcType="VARCHAR" property="browser" />
<result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
<result column="create_user_name" jdbcType="VARCHAR" property="createUserName" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="remark" jdbcType="VARCHAR" property="remark" />
</resultMap>
<sql id="Base_Column_List">
type, path, params, ip, browser, create_user_id, create_user_name, create_time, remark
</sql>
<select id="selectById" parameterType="java.lang.Long" resultMap="BaseResultMap">
select *
from sys_log
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteById" parameterType="java.lang.Long">
delete from sys_log
where id = #{id,jdbcType=BIGINT}
</delete>
<delete id="deleteByIds" parameterType="java.util.List">
delete from sys_log
where id in
<foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</delete>
<insert id="insert" keyProperty="id" parameterType="org.ostars.springbootfast.model.base.SysLogModel">
insert into sys_log (
<include refid="Base_Column_List" />
)
values (#{type,jdbcType=INTEGER},
#{path,jdbcType=VARCHAR},
#{params,jdbcType=VARCHAR},
#{ip,jdbcType=VARCHAR},
#{browser,jdbcType=VARCHAR},
#{createUserId,jdbcType=BIGINT},
#{createUserName,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP},
#{remark,jdbcType=VARCHAR}
)
</insert>
<insert id="insertList" parameterType="java.util.List">
insert into sys_log (
<include refid="Base_Column_List" />
) values
<foreach collection="list" index="index" item="item" separator=",">
(
#{item.type,jdbcType=INTEGER},
#{item.path,jdbcType=VARCHAR},
#{item.params,jdbcType=VARCHAR},
#{item.ip,jdbcType=VARCHAR},
#{item.browser,jdbcType=VARCHAR},
#{item.createUserId,jdbcType=BIGINT},
#{item.createUserName,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP},
#{item.remark,jdbcType=VARCHAR}
)
</foreach>
</insert>
<select id="selectByModel" parameterType="org.ostars.springbootfast.model.base.SysLogModel" resultMap="BaseResultMap">
select * from sys_log where 1 = 1
<if test="type != null">
and type = #{type,jdbcType=INTEGER}
</if>
<if test="path != null">
and path = #{path,jdbcType=VARCHAR}
</if>
<if test="params != null">
and params = #{params,jdbcType=VARCHAR}
</if>
<if test="ip != null">
and ip = #{ip,jdbcType=VARCHAR}
</if>
<if test="browser != null">
and browser = #{browser,jdbcType=VARCHAR}
</if>
<if test="createUserId != null">
and create_user_id = #{createUserId,jdbcType=BIGINT}
</if>
<if test="createUserName != null">
and create_user_name = #{createUserName,jdbcType=VARCHAR}
</if>
<if test="createTime != null">
and create_time = #{createTime,jdbcType=TIMESTAMP}
</if>
<if test="remark != null">
and remark = #{remark,jdbcType=VARCHAR}
</if>
</select>
<update id="updateByIdSelective" parameterType="org.ostars.springbootfast.model.base.SysLogModel">
update sys_log
<set>
<if test="type != null">
type = #{type,jdbcType=INTEGER},
</if>
<if test="path != null">
path = #{path,jdbcType=VARCHAR},
</if>
<if test="params != null">
params = #{params,jdbcType=VARCHAR},
</if>
<if test="ip != null">
ip = #{ip,jdbcType=VARCHAR},
</if>
<if test="browser != null">
browser = #{browser,jdbcType=VARCHAR},
</if>
<if test="createUserId != null">
create_user_id = #{createUserId,jdbcType=BIGINT},
</if>
<if test="createUserName != null">
create_user_name = #{createUserName,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="remark != null">
remark = #{remark,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateById" parameterType="org.ostars.springbootfast.model.base.SysLogModel">
update sys_log
set type = #{type,jdbcType=INTEGER},
path = #{path,jdbcType=VARCHAR},
params = #{params,jdbcType=VARCHAR},
ip = #{ip,jdbcType=VARCHAR},
browser = #{browser,jdbcType=VARCHAR},
create_user_id = #{createUserId,jdbcType=BIGINT},
create_user_name = #{createUserName,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
remark = #{remark,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>