springcloud微服务学习笔记目录:
springcloud微服务学习笔记
1.建表
2.entities
使用了lombok,可以在idea中安装lombok插件
/**
* 实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
/**
* 返回数据
* @param <T>
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code,String message){
this(code,message,null);
}
}
这里idea要安装lombok插件
安装方法
安装完后要重启idea才起作用
3.dao
3.1 mapper映射文件
<mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values(#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"></id>
<id column="serial" property="serial" jdbcType="VARCHAR"></id>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
</select>
</mapper>
3.2 dao方法
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id")Long id);
}
4.service
4.1 service接口
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id")Long id);
}
4.2service实现类
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
5.controller
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/payment/creat")
public CommonResult<Payment> create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("******插入结果" + result);
if (result > 0) {
return new CommonResult(200, "success", result);
} else {
return new CommonResult(400, "fail", null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("******查询结果" + payment);
if (payment != null) {
return new CommonResult(200, "success,serverPort:" , payment);
} else {
return new CommonResult(400, "fail", null);
}
}
}
完成到这,可以启动主启动类测试一下
忽略这个错