第四节--子工程服务提供者支付Module模块

第一步:创建子工程 cloud-provider-payment8001:操作步骤csdn上:https://blog.csdn.net/hexu_blog/article/details/104971451
 
  新知识:如何开启Run Dashbard运行项目?
 
 
第二步:pom.xml文件
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <!-- actuator监控信息完善 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>


    <!--热部署  -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

第三步:applicaiton.yml文件

server:
  port: 8001


spring:
  application:
    name: cloud-payment-service #eureka对外暴露的微服务名字
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 30000


mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations:
    - classpath:mybatis/mapper/user.xml

第四步:创建表

create table payment(  id BIGINT(20) not null auto_increment comment 'ID',  serial varchar(200) default '',  primary key(id))ENGINE=INNODB auto_increment=1 default CHARSET=utf8;insert into payment(serial) values('abbbaa01');

第五步:payment.xml和mybatis.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.hx.dao.PaymentDao">


    <select id="findPaymentById" resultType="payment">
     select*from payment where id=#{id}
  </select>


    <select id="findPaymentList" resultType="payment">
    select*from payment
  </select>
    <insert id="addPayment" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO payment(serial) VALUES(#{serial});
  </insert>
    <update id="updatePayment">
     update payment set serial=#{serial}  WHERE id=#{id}
  </update>
    <delete id="delPayment">
     delete from user payment id=#{id}
  </delete>

</mapper>
<?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>
    <typeAliases>
      <!-- <typeAlias type="com.hx.entity.User" alias="user"/> -->
       <package name="com.hx.entity"/>
      <!-- 在批量起别名中,可以使用@Alias("user")为某个类起别名 如:hx.entity中有User类,entity子包下hx.entity.children还有一个User,如果只使用<package
         name="com.hx.entity"/> 运行会报错,因为识别不了是否指定那个user,两个User类的别名都相同,所有可以使用@Alias("users")在某个类上从新起别名 -->
   </typeAliases>
</configuration>

第六步:dao层,service层, controller层

@Mapperpublic interface PaymentDao {    public List<Payment> findPaymentList();    public Payment findPaymentById(long id);    public int addPayment(Payment payment);    public boolean updatePayment(Payment payment);    public boolean delPayment(long id);}

public interface PaymentService {
    public List<Payment> findPaymentList();


    public Payment findPaymentById(long id);


    public int addPayment(Payment payment);


    public boolean updatePayment(Payment payment);


    public boolean delPayment(long id);
}




@Service
public class PaymentServiceImpl implements PaymentService {


    @Resource private PaymentDao paymentDao;
    @Override
    public List<Payment> findPaymentList() {
        return paymentDao.findPaymentList();
    }


    @Override
    public Payment findPaymentById(long id) {
        return paymentDao.findPaymentById(id);
    }


    @Override
    public int addPayment(Payment payment) {
        return paymentDao.addPayment(payment);
    }


    @Override
    public boolean updatePayment(Payment payment) {
        return paymentDao.updatePayment(payment);
    }


    @Override
    public boolean delPayment(long id) {
        return paymentDao.delPayment(id);
    }
}



@RestController
@Slf4j
public class PaymentController {


    @Autowired
    private PaymentService paymentService;


     @PostMapping("/admin/addPayment")
     public CommonResult addPayment(@RequestBody Payment payment){
       int count= paymentService.addPayment(payment);
       log.info("添加数据");
       if(count>0){
           return  new CommonResult(200,"添加成功");
       }
       return  new CommonResult(400,"添加失败");
     }
    @GetMapping("/admin/findPaymentById/{id}")
    public CommonResult findPaymentById(@PathVariable("id")Long id){
        Payment payment=paymentService.findPaymentById(id);
        log.info("查找数据");
        if(payment!=null){
            return  new CommonResult(200,"查找成功",payment);
        }
        return  new CommonResult(400,"没有对应的记录");
    }
}
项目目录结构
 
使用postman发送请求:
 
搭建环境完成
 
 
原文地址:https://www.cnblogs.com/hexublog/p/13680002.html