• 20200113 SpringBoot整合MyBatis


    环境信息

    • SpringBoot 2.2.2.RELEASE
    • Mybatis
    • Druid
    • MySQL
    • Lombok

    实现步骤

    1. 引入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 https://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.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>study.hwj</groupId>
        <artifactId>mybatis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>mybatis</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    1. 编写Mapper

    编写Mybatis的Mapper有多种形式,这里只选择其中一种

    @Mapper
    public interface UserMapper {
        @Select("select * from user")
        List<User> getAllUsers();
    }
    
    1. 测试
    @SpringBootApplication
    public class MybatisApplication {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext ctx = SpringApplication.run(MybatisApplication.class, args);
    
            UserMapper userMapper = ctx.getBean(UserMapper.class);
            System.out.println(userMapper.getAllUsers());
        }
    
    }
    
  • 相关阅读:
    CentOS7 设置软件镜像源
    让树莓派自动上报IP地址到邮箱,二代B
    给树莓派安装看门狗的两种方法,二代B
    树莓派(Raspberry Pi)USB无线网卡自动连接,二代B
    升级MAC OX上的Python到3.4
    http 返回码 405 解决方案之一
    CentOS 6.4 SSH 免密码登录
    SVN的Hooks功能--强制添加注释
    PHP开发中,让var_dump调试函数输出更美观 ^_^#
    CentOS 6.4 命令行 安装 VMware Tools
  • 原文地址:https://www.cnblogs.com/huangwenjie/p/12188764.html
Copyright © 2020-2023  润新知