SpringBoot整合Mybatis

1、项目结构

 

2、pom.xml依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 开发环境配置
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
<br>#Mybatis配置
mybatis:
  mapper-locations: classpath:mappers/*.xml
  configuration:
    map-underscore-to-camel-case: true<br>

  

4、UserController类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 接口

1
2
3
4
5
6
7
8
9
package com.feng.service;<br>
import com.feng.pojo.Users;
import java.util.List;
 
public interface UserService {
 
    public List<Users> getUserList();
 
}

  

6、UserServiceImpl 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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接口

1
2
3
4
5
6
7
8
9
10
11
12
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文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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、创建表

1
2
3
4
5
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 @   Amy清风  阅读(85)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示