SpringBoot整合Mybatis

1、项目结构

 

2、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>

    <groupId>org.example</groupId>
    <artifactId>feng</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.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>
        <swagger.version>3.0.0</swagger.version>
    </properties>


    <dependencies>
        <!-- SpringBoot的依赖配置-->
        <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>
            <scope>test</scope>
        </dependency>

        <!--mybatis的springboot依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <!-- SQL_SERVER驱动包 -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>9.2.1.jre8</version>
        </dependency>

        <!-- Swagger3依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>

    </dependencies>


    <!-- 这个插件,可以将应用打成一个可以执行的jar包 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  

3、application.yml文件

# 开发环境配置
server:
  # 服务器的HTTP端口,默认为8089
  port: 8089
  servlet:
    # 应用的访问路径
    context-path: /
  tomcat:
    # tomcat的URI编码
    uri-encoding: UTF-8
    # 连接数满后的排队数,默认为100
    accept-count: 1000
    threads:
      # tomcat最大线程数,默认为200
      max: 800
      # Tomcat启动初始化的线程数,默认值10
      min-spare: 100


# Spring配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/feng?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    username: root
    password: 123456

# Swagger配置
swagger:
  # 是否开启swagger
  enabled: true
  # 请求前缀
  pathMapping: /dev-api

#Mybatis配置 mybatis: mapper-locations: classpath:mappers/*.xml configuration: map-underscore-to-camel-case: true

  

4、UserController类

package com.feng.controller;

import com.feng.pojo.Users;
import com.feng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get")
    public List<Users> getList(){
        return userService.getUserList();
    }

}

 

5、UserService 接口

package com.feng.service;
import com.feng.pojo.Users; import java.util.List; public interface UserService { public List<Users> getUserList(); }

  

6、UserServiceImpl 类

package com.feng.service.impl;

import com.feng.mapper.UsersMapper;
import com.feng.pojo.Users;
import com.feng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    public List<Users> getUserList() {
        return usersMapper.findUserList();
    }
}

  

7、UsersMapper接口

package com.feng.mapper;

import com.feng.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface UsersMapper{
    
    public List<Users> findUserList();
    
}

  

8、UsersMapper.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.feng.mapper.UsersMapper" >
  <resultMap id="BaseResultMap" type="com.feng.pojo.Users" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>

  <select id="findUserList" resultMap="BaseResultMap">
    select * from users
  </select>

</mapper>

  

9、FengApplication 启动类

package com.feng;

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

@SpringBootApplication
public class FengApplication {

    public static void main(String[] args){
        SpringApplication.run(FengApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
        System.out.println("(♥◠‿◠)ノ゙  风启动成功   ლ(´ڡ`ლ)゙ ");
    }
}

  

10、创建表

CREATE TABLE `users` (
  `id` varchar(100) DEFAULT NULL,
  `username` varchar(100) DEFAULT NULL,
  `password` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

  

11、测试  http://localhost:8089/user/get

 

posted @ 2024-10-28 14:36  Amy清风  阅读(101)  评论(0)    收藏  举报