美文网首页
SpringBoot-Mybatis整合

SpringBoot-Mybatis整合

作者: zxy_3197 | 来源:发表于2019-02-19 18:02 被阅读0次

    1.主程序
    package com.example.demo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    /**

    • @Description:
    • @author: YuanTong-ZXY
    • @Date: 2019/2/19 15:15
      */
      @SpringBootApplication
      public class DemoApplication {
      public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class,args);
      }
      }

    2.controller层
    package com.example.demo.controller;

    import com.example.demo.service.UserServiceImpl;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import javax.annotation.Resource;

    /**

    • @Description:

    • @author: YuanTong-ZXY

    • @Date: 2019/2/19 15:09
      */
      @RestController
      @RequestMapping("/testboot")
      public class TestBootController {
      @Resource
      private UserServiceImpl userService;

      @RequestMapping("getuser")
      public Integer getUser() {
      Integer userById = userService.getUserById();
      return userById;
      }
      }
      3、service层
      package com.example.demo.service;
      /**

    • @Description:

    • @author: YuanTong-ZXY

    • @Date: 2019/2/19 15:59
      /
      import com.example.demo.mapper.UserDao;
      import org.springframework.stereotype.Service;
      import javax.annotation.Resource;
      @Service("userService")
      public class UserServiceImpl{
      @Resource
      private UserDao userDao;
      public Integer getUserById() {
      return userDao.selectCount();
      }
      }
      4.mapper
      package com.example.demo.mapper;
      /
      *

    • @Description:

    • @author: YuanTong-ZXY

    • @Date: 2019/2/19 15:54
      /
      import org.apache.ibatis.annotations.Mapper;
      @Mapper
      public interface UserDao {
      Integer selectCount();
      }
      5.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="com.example.demo.mapper.UserDao" >
      <select id="selectCount" resultType="java.lang.Integer" >
      select count(
      ) from wx_user
      </select>
      </mapper>

    6.application.yml
    spring:
    profiles:
    active: dev
    datasource:
    url: jdbc:mysql://localhost:3306/saasboard
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    7.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.3.RELEASE</version>
    <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <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.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    </project>

    相关文章

      网友评论

          本文标题:SpringBoot-Mybatis整合

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