栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Mybatis

SpringBoot整合Mybatis 前言

这是SpringBoot整合Mybatis的简单例子,拿去即用,有问题欢迎提出来,评论区交流

一,文件配置 1.pom文件

代码如下(示例):



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    com.example
    springboot-mybatis
    0.0.1-SNAPSHOT
    springboot-mybatis
    mybatis project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
        
            mysql
            mysql-connector-java
            8.0.19
        

        
            org.mybatis
            mybatis
            3.5.4
        

        
            org.mybatis
            mybatis-spring
            2.0.4
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.数据库配置(application.yml)

代码如下(示例):

spring:
  datasource:
    url: jdbc:mysql:///spring_boot?serverTimezone=GMT
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

server:
  port: 8081
3.启动类(SpringbootMybatisApplication)

代码如下(示例):

package com.example.springbootmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.springbootmybatis.dao")
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}
4.控制层(JdbcController)

代码如下(示例):

package com.example.springbootmybatis.controller;

import com.example.springbootmybatis.pojo.User;
import com.example.springbootmybatis.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class JdbcController {
    @Resource
    private UserService userService;
    @RequestMapping("/hello")
    public String hello(){
        return "Hello Spring-boot-mybatis";
    }

    @RequestMapping("/selectAll")
    public List selectAll(){
        return userService.selectAll();
    }
}
5.service层(UserService)

代码如下(示例):

package com.example.springbootmybatis.service;
import com.example.springbootmybatis.pojo.User;
import java.util.List;

public interface UserService {
    List selectAll();
}

6.service的实现层(UserServiceImpl)

注意要加@Service注解
代码如下(示例):

package com.example.springbootmybatis.service.Impl;

import com.example.springbootmybatis.dao.UserMapper;
import com.example.springbootmybatis.pojo.User;
import com.example.springbootmybatis.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List selectAll() {
        return userMapper.selectAll();
    }
}

6.dao层(UserServiceImpl)

我这里采取的注解写sql
代码如下(示例):

package com.example.springbootmybatis.dao;

import com.example.springbootmybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    @Select("select * from user")
    List selectAll();

}

7.实体类(User)

代码如下(示例):

package com.example.springbootmybatis.pojo;

public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "username='" + username + ''' +
                ", password='" + password + ''' +
                '}';
    }
}
二,测试

总结

以上就是今天要讲的内容,本文仅仅简单介绍了springboot去整合mybatis…

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962806.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号