spring boot 整合mybatis 通过官方mybatis-spring-boot-starter

spring boot 专栏收录该内容
2 篇文章 0 订阅

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cjwdemo.demoBySB</groupId>
    <artifactId>demoBySB</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoBySB Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot整合mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--mysql jdbc驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>


    </dependencies>
    <build>
        <finalName>demoBySB</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

实体类

package com.cjwdemo.demoBySB.entity;

public class Userinfo {

    private Integer id;
    private String username;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

dao接口

package com.cjwdemo.demoBySB.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.cjwdemo.demoBySB.entity.Userinfo;

@Mapper
public interface UserinfoDao {

    @Select("SELECT id, username FROM userinfo WHERE username = #{username}")
    Userinfo findByUsername(@Param(value = "username") String username);

}

通过控制器调用

package com.cjwdemo.demoBySB.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjwdemo.demoBySB.dao.UserinfoDao;

@Controller
public class DemoController {

    @Autowired
    private UserinfoDao userinfoDao;

    // 测试路由器 http://localhost:9999/ 默认是8080,但我把他配置为9999了
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String test() {
        return "hello spring boot";
    }

    // 测试路由器 http://localhost:9999/test2 默认是8080,但我把他配置为9999了
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    @ResponseBody
    public String test2() {
        return userinfoDao.findByUsername("test").getUsername();
    }

}

测试准备的sql脚本

CREATE DATABASE test CHARSET utf8mb4;
USE test;

CREATE TABLE userinfo (
id INT(11) AUTO_INCREMENT NOT NULL COMMENT '主键',
username VARCHAR(20) NOT NULL COMMENT '用户名',
PRIMARY KEY (id)
)ENGINE=INNODB DEFAULT CHARSET utf8mb4 COMMENT '测试表';


INSERT INTO userinfo (username) VALUES('test');

官网教程

https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start
  • 1
    点赞
  • 1
    评论
  • 5
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

cjwdemo

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值