springboot集成mybatis
2020-07-01 本文已影响0人
Geroge1226
1、介绍
首先建立一个springboot web项目,在此基础上集成mybatis
2、使用
- pom引用
<!--mybatis引用-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- jdbc引用 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
-
mybatis-generator生成了映射文件
mybatis-generator使用方法参考:https://www.jianshu.com/writer#/notebooks/38432218/notes/68331326
目录如下:
image.png
- contoller层
@Api(value = "健身助手服务")
@RequestMapping("/api/fitness")
@RestController
public class FitnessHelperController extends BaseController {
@Autowired
private FitnessHelperService service;
/**
* @name: signIn
* @description: 签到送券码
* @return: com.service.common.UniformResultTemplate<java.lang.Integer>
* @date: 2020-06-28 10:51
* @auther: xxx
*
*/
@ApiOperation(value = "2499会员卡签到")
@RequestMapping(value = "/signin",method = RequestMethod.POST)
public UniformResultTemplate<Integer> signIn(@RequestBody @Valid FitnessSigninReqVO reqVO, BindingResult result){
if(result.hasErrors()){
return super.fail(result.getFieldError().getDefaultMessage());
}
return success(service.addFitnessSignData(reqVO));
}
- service层
public interface FitnessHelperService {
Integer addFitnessSignData(FitnessSigninReqVO vo);
}
- 实现层
@Service
@Slf4j
public class FitnessHelperServiceImpl implements FitnessHelperService {
@Autowired
private SportFintessSignTotalMapper totalMapper;
@Autowired
private SportFitnessSignLogMapper logMapper;
@Autowired
private EsbTerminalFeign esbTerminalFeign;
private String couponId="1234";
// 新增签到数据
@Override
public Integer addFitnessSignData(FitnessSigninReqVO vo) {
// 1、新增数据记录
SportFitnessSignLog record = new SportFitnessSignLog();
BeanUtils.copyProperties(vo,record);
int a = logMapper.insertSelective(record);
if(a <= 0){
throw new CustomException(Code.FAIL.getCode(),"新增失败");
}
// 2、赠券
CouponSendReqDTO dto = new CouponSendReqDTO();
dto.setMemberId(Long.parseLong(vo.getMemberId()));
dto.setCouponId(Long.parseLong(couponId));
esbTerminalFeign.sendCoupon(dto);
return record.getId();
}
}
- 数据库连接配置yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/pousheng_source?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
username: root
password: lsy641
- ** mapper bind配置信息 yml
mybatis:
mapper-locations: classpath*:mappers/*.xml
以上不指定扫码文件会报:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pousheng.fitnesshelper.dao.SportFitnessSignLogMapper.insertSelective
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:107)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:94)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
at com.sun.proxy.$Proxy94.insertSelective(Unknown Source)
at com.pousheng.fitnesshelper.service.impl.FitnessHelperServiceImpl.addFitnessSignData(FitnessHelperServiceImpl.java:52)
at com.pousheng.fitnesshelper.controller.FitnessHelperController.signIn(FitnessHelperController.java:53)
原因:springboot项目启动扫码文件