spring家族的springboot就像航天界冒出的SpaceX,降本增效提质。
hello world 即将成为 hello space。
1.1 快速搭建springboot项目工程(就类似vue的vue-cli脚手架),next据需选配。
1.2 开箱即用
1.3 springboot go !
1.4 profile 环境切换
或者:
1.5 logback日志(会先于springboot配置文件的加载)
logback
%d{HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %msg%n
INFO
ACCEPT
DENY
%d{HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %msg%n
UTF-8
/data/log/demo/logs/demo.%d{yyyyMMddHH}.log
ERROR
%d{HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %msg%n
UTF-8
/data/log/demo/logs/demo.error.%d{yyyyMMddHH}.log
2.1 maven pom引入mybatis依赖
UTF-8
UTF-8
1.8
1.1.22
3.5.0
2.0.0
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
com.alibaba
druid
${druid-version}
com.alibaba
druid-spring-boot-starter
1.1.10
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.projectlombok
lombok
true
2.2 yml 文件配置:
#server
server:
port: 8100
servlet:
context-path: /demo
#spring
spring:
application:
name: demo
#datasource
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://192.168.2.9:3306/demo? characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: demo
password: demo123
druid:
initial-size: 2
min-idle: 2
max-active: 20
max-wait: 60000
remove-abandoned: true
remove-abandoned-timeout: 60
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
validation-query: select 1
test-on-return: true
test-while-idle: true
test-on-borrow: true
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据 http://localhost:8100/demo/druid/index.html
# admin/admin
useGlobalDataSourceStat: true
#mybatis
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.example.demo.entity.domain
#pagehelper
pagehelper:
helper-dialect: mysql
reasonable: false
support-methods-arguments: true
params: count=countSql
2.3扫描mapper映射为DAO
2.4数据库建表,使用mybatis-generator反向工程自动生成
domain,mapping, mapper (查看官网教程或私信索要工具类)
创建Domain基类
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.Date;
@Slf4j
@Data
public class Domain implements Serializable {
protected Integer id;
protected Date createTime;
protected Date updateTime;
protected Byte delFlag;
}
修改自动生成的User 集成自Domain基类
public class User extends Domain {
private String account;
private String password;
}
创建BaseMapper
import com.example.demo.domain.Domain;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper {
String PO_KEY = "po";
int deleteByPrimaryKey(Integer id);
int insert(T record);
int insertSelective(T record);
T selectByPrimaryKey(Integer id);
int updateByPrimaryKey(T record);
int updateByPrimaryKeySelective(T record);
Page queryByExample(@Param(PO_KEY) T record);
}
修改UserMapper 集成BaseMapper
import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper extends BaseMapper {
User queryUserByAccount(@Param("account") String account);
}
追加扩展UserMapping功能
2.5service层
BaseService
import com.example.demo.po.PagerRequest;
import com.github.pagehelper.Page;
import java.util.List;
public interface BaseService {
int insert(T record);
int insertSelective(T record);
int deleteByPrimaryKey(Integer id);
int updateByPrimaryKey(T record);
int updateByPrimaryKeySelective(T record);
T selectByPrimaryKey(Integer id);
Page selectByPage(PagerRequest pagerRequest);
List queryByExample(T record);
}
BaseServiceImpl
import com.example.demo.domain.Domain;
import com.example.demo.mapper.BaseMapper;
import com.example.demo.po.PagerRequest;
import com.example.demo.service.BaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.ParameterizedType;
import java.util.List;
@Slf4j
public abstract class BaseServiceImpl implements BaseService {
@Autowired
private BaseMapper baseMapper;
private Class domain;
public BaseServiceImpl() {
ParameterizedType parameterizedType = ((ParameterizedType) getClass().getGenericSuperclass());
domain = (Class) parameterizedType.getActualTypeArguments()[0];
}
@Transactional(rollbackFor = Exception.class)
@Override
public int insert(T record){
return this.baseMapper.insert(record);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int insertSelective(T record){
return this.baseMapper.insertSelective(record);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int deleteByPrimaryKey(Integer id){
return this.baseMapper.deleteByPrimaryKey(id);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int updateByPrimaryKey(T record){
return this.baseMapper.updateByPrimaryKey(record);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int updateByPrimaryKeySelective(T record){
return this.baseMapper.updateByPrimaryKeySelective(record);
}
@Override
public T selectByPrimaryKey(Integer id){
return this.baseMapper.selectByPrimaryKey(id);
}
@Override
public Page selectByPage(PagerRequest pagerRequest){
try {
PageHelper.startPage(pagerRequest.getPageNum(), pagerRequest.getPageSize());
PageHelper.orderBy(pagerRequest.getOrderBy());
T example = domain.newInstance();
// 查询参数封装转换 (如分页查询User, 创建UserRequest extends PagerRequest,
// UserRequest中的查询参数与User对应的属性保持一致)
BeanUtils.copyProperties(pagerRequest, example);
// 在对应的mapping中覆写queryByExample方法
return this.baseMapper.queryByExample(example);
} catch (Exception e) {
log.error("异常信息:{}", e.getMessage());
throw new RuntimeException("查询参数异常", e);
}
}
@Override
public List queryByExample(T record){
return this.baseMapper.queryByExample(record);
}
}
分页查询参数PagerRequest
@Data
public class PagerRequest {
private int pageNum = 1; // mybatis pagerHelper从0开始
private int pageSize = 10; // 默认10条记录
private String orderBy = "id desc"; // 默认按id降序
}
@EqualsAndHashCode(callSuper = true)
@Data
public class UserRequest extends PagerRequest {
private String account;
}
UserService
import com.example.demo.domain.User;
public interface UserService extends BaseService {
User queryUserByAccount(String account);
}
UserServiceImpl
@Service
public class UserServiceImpl extends BaseServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User queryUserByAccount(String account) {
return userMapper.queryUserByAccount(account);
}
}
BaseService中方法不满足时,可在UserService中扩展实现方法
原本想整理springboot + ORM,mybatis及 JPA一起搞了的,发现篇幅太长了不宜读,JPA且待下回分享。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦