美文网首页
Spring-MyBatis

Spring-MyBatis

作者: 華華公子 | 来源:发表于2020-02-20 10:24 被阅读0次

    配置文件

    • pom.xml

    springboot

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    spring-mybatis

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    mysql

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>
    
    • application.yaml
    mybatis:
      type-aliases-package: org.wyv.monitor.mapper #mapper接口文件位置
      configuration:
        map-underscore-to-camel-case: true #驼峰声明方式
        default-statement-timeout: 30 #SQL运行超时时间
      mapper-locations: classpath:mappers/*.xml #SQL代码文件存放位置
    

    代码

    • java接口文件
    @Mapper
    @Component("XXXDao") //不用这个会出现报错的情况
    public interface XXXMapper{
    
        List<SoaServView> querySOAMessageLogOfHourToday(HashMap map);
    
    }
    
    • Mapper文件
    <?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="org.wyv.monitor.mapper.XXXMapper">
        <select id="querySOAMessageLogOfHourToday"
                resultType="org.wyv.monitor.model.vo.SoaServView">
           SELECT service_id,
           service_name,
           count(service_id) as cnt,
           sum(if(stat = 'true',1,0)) as cnt_true,
           sum(if(stat = 'false',1,0)) as cnt_false,
           sum(if(stat = 'true',respond_time,0))/sum(if(stat = 'true',1,0)) as avg_time,
           left(reg_time,10) as serv_time
            FROM m_service_message
            where
            reg_time &gt;= concat(date_format(current_date(),'%Y%m%d'),'0000000')
            and reg_time &lt;= concat(date_format(current_date(),'%Y%m%d'),'5959999')
            <if test="sid != null">
                and service_id = #{sid}
            </if>
            <if test="sname != null">
                and service_name like #{sname}
            </if>
            group by service_id,left(reg_time,10),service_name
            order by service_id,left(reg_time,10)
        </select>
    </mapper>
    
    

    添加springboot配置

    application.yml

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/db_cqt?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: 12345678
        driver-class-name: com.mysql.cj.jdbc.Driver
      aop:
        proxy-target-class: true
    mybatis:
      type-aliases-package: org.yiva.cqtserver.mapper
      configuration:
        map-underscore-to-camel-case: true
      mapper-locations: classpath:mappers/*.xml
    server:
      port: 9001
    
    

    pom.xml

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>org.yiva</groupId>
        <artifactId>cqtserver</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!--<packaging>war</packaging>-->
        <packaging>jar</packaging>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--<dependency>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
                <!--<scope>provided</scope>-->
            <!--</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-aop</artifactId>
            </dependency>
    
            <!--mybatis插件-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!-- MySQL 连接驱动依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.12</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.49</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    目录结构

    目录结构

    相关文章

      网友评论

          本文标题:Spring-MyBatis

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