虽然说在实际开发中,很多时候不会让你从零开发。但哪天要是让你自己做一个子模块,那得也得搞是不是?
做一个子模块(通常是springboot项目),大致遵从下面6步:
1.创建子模块(maven项目或springboot项目)
2.改pom.xml(引入依赖)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
3.配置yml或properties(配置端口号、服务名、数据源等)
server: port: 8001 spring: application: name: cloud-payment-service # zipkin: # base-url: http://localhost:9411 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://192.168.113.6:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root # sleuth: # sampler: # probability: 1 #采样率 1为全部采集 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: cn.aib.springcloud.entities
4.写主程序类(如果是maven项目不会自带这个主程序类的)
@SpringBootApplication public class PaymentApplication { public static void main(String[] args) { SpringApplication.run(PaymentApplication.class,args); } }
5.写业务:现在就来写代码了。一般在写代码最好遵循下面的步骤,记住有的步骤可能不需要写了,但请你还是回顾一下
1)设计表,创建表
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
2)创建表对应的实体类,如果可以的话还可以创建返回给前端的统一实体类
@Data @AllArgsConstructor //这个只会全参的构造,不会生成部分属性的构造方法 @NoArgsConstructor //空参构造 public class Payment { //支付id private Long id; //支付流水号 private String serial; }
@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult<T> { //状态码 private Integer code; //消息 private String message; //数据 private T data; public CommonResult(Integer code,String message){ this.code = code; this.message = message; } }
3)Dao
》dao
@Mapper public interface PaymentDao { /** * 保存支付 * 方法名:create * 参数:Payment payment * 返回值:int。做添加的操作其实也可以返回void或者boolean,但为了规范我们还是用int */ public int create(Payment payment); /** * 读取支付 * 方法名:selectPayment * 参数:Integer id * 返回值:Payment。 */ public Payment selectPayment(@Param("id") Long id); }
在进行添加的操作,返回值最好是int,表示影响的条数
》mapper.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="cn.aib.springcloud.dao.PaymentDao"> <resultMap id="PaymentResultMap" type="cn.aib.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"></id> <result column="serial" property="serial" jdbcType="VARCHAR"></result> </resultMap> <!-- 添加支付 --> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert to payment(serial) values (#{serial}) </insert> <!-- 读取支付 --> <select id="selectPayment" parameterType="Long" resultMap="PaymentResultMap"> select * from payment where id=#{id} </select> </mapper>
在mapper.xml进行添加操作时,尽量得定义userGeneratedKeys和keyProperyty,给实体类返回ID值
在实际开发中,有时候实体类和表的名字不一定会对应的,建议写resultMap,即使属性名和字段名一样,也尽量写resultMap
4)Service
@Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; @Override public int create(Payment payment) { int i = paymentDao.create(payment); return i; } @Override public Payment selectPayment(Long id) { Payment payment = paymentDao.selectPayment(id); return payment; } }
5)Controller
@RestController @Slf4j @CrossOrigin public class PaymentController { @Autowired private PaymentService paymentService; @PostMapping("/payment/create") public CommonResult create(Payment payment){ int i = paymentService.create(payment); log.info("插入条数:"+i); if(i>0) { return new CommonResult<Integer>(200, "插入成功",i); }else { return new CommonResult(400, "插入失败"); } } @GetMapping("/payment/get/{id}") public CommonResult selectPayment(@PathVariable("id") Long id){ Payment payment = paymentService.selectPayment(id); log.info("查询结果:"+payment); if(payment != null) { return new CommonResult<Payment>(200, "查询成功",payment); }else { return new CommonResult(400, "查询失败"); } } }
在实际开发中,请使用日志来打印信息把,不要再用System.out.println了,没有使用springboot就把log4j的jar包和日志配置文件加进来来用,如果使用的是springboot,那直接在你想要记录的类上加上@SL4J,这样就可以使用日志的API了
尽量写Restful风格的Mapping,少写RequestMapping
6.测试:现在测试主流的有两种,swagger和postman,根据自己喜好来吧,如果是在开发中的话,swagger会更多一点