美文网首页
SpringBoot的第一应用

SpringBoot的第一应用

作者: 思记享 | 来源:发表于2018-07-24 15:14 被阅读0次

    今要开发一个数据同步接口,对外提供数据同步,打算用springboot快速开发试下。

    @SpringBootApplication
    @MapperScan("com.xxx.wechat.mapper")

    @SpringBootApplication
    @MapperScan("com.xxx.wechat.mapper")
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    API开发没有变化

    @RestController
    @EnableAutoConfiguration
    @RequestMapping("/datacenter")
    public class DataSyncToDataCenterApi {
        
        @Autowired
        private UserMapper userMapper;
        
        @Autowired
        private ProductCardStatMapper productCardStatMapper;
    
        @RequestMapping("getuser")
        public User getUser() {
            User user = new User();
            user.setName("test");
            return user;
        }
        
        @RequestMapping("getUserNames")
        public List<String> getUserName() {
            List<String> result = userMapper.getNickName();
            return result;
        }
    
    package com.tcl.wechat.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Result;
    import org.apache.ibatis.annotations.Results;
    import org.apache.ibatis.annotations.Select;
    
    import com.tcl.wechat.model.OrderStat;
    import com.tcl.wechat.model.ProductStat;
    
    public interface ProductCardStatMapper {
    
        @Select("SELECT p.product_name as productName,count(1) as productNum "
                + "FROM `product_card` pc, product p "
                + "WHERE pc.product_id = p.id and status=1 group by product_id limit #{start} ,#{pageNum}" )
        public List<ProductStat> getStock(int date,@Param("start") int start,@Param("pageNum") int pageNum);
        
        @Select("SELECT COUNT(1) from (SELECT p.product_name as productName,count(1) as productNum "
                + "FROM `product_card` pc, product p "
                + "WHERE pc.product_id = p.id and status=1 group by product_id) AS a_table")
        public int getProductCardRecordCount();
        
        @Select("SELECT COUNT(1) from `order` where DATE_FORMAT(update_time,'%Y%m%d')=#{date}")
        public int getOrderCount(int date);
        
        @Select("SELECT * from `order` where DATE_FORMAT(update_time,'%Y%m%d')=#{date} limit #{start} ,#{pageNum} ")
        @Results({
            @Result(id=true,property="id",column="id"),
            @Result(property="orderNo",column="order_no"),
            @Result(property="outTradeNo",column="out_trade_no"),
            @Result(property="payAmount",column="pay_amount"),
            @Result(property="orderType",column="order_type"),
            @Result(property="userId",column="user_id"),
            @Result(property="createTime",column="create_time"),
            @Result(property="createTime",column="create_time"),
            @Result(property="updateTime",column="update_time"),
            @Result(property="source",column="source"),
            @Result(property="businessType",column="business_type"),
            @Result(property="title",column="title"),
            @Result(property="goodsId",column="goods_id"),
            @Result(property="payTime",column="pay_time"),
            @Result(property="goodsId",column="goods_id"),
            @Result(property="productId",column="product_id")
        })
        public List<OrderStat> getOrders(@Param("date") int date,@Param("start") int start,@Param("pageNum") int pageNum);
        
        
    }
    
    

    maven配置文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.tcl.wechat</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    相关文章

      网友评论

          本文标题:SpringBoot的第一应用

          本文链接:https://www.haomeiwen.com/subject/zzaemftx.html