SpringBoot整合Mybatis框架过程

简单记录一下springboot+mybatis搭建过程

1、使用IDEA创建maven工程

在这里插入图片描述
在这里插入图片描述

1.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>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/>
  </parent>

  <groupId>org.example</groupId>
  <artifactId>springbootMybatisDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>springboot-mybatis</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.1.3.RELEASE</version>
    </dependency>

    <!--springboot整合mybatis相关依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.12</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <!-- alibaba的druid数据库连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.8</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
    </dependency>

  </dependencies>

</project>

3、创建application.yml

server:
  port: 9527

#数据库配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/ssmdemo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
  #模板引擎配置
  thymeleaf:
    encoding: UTF-8
    #suffix: .html  默认后缀
    #prefix: classpath:/templates/  默认前缀

mybatis:
  mapperLocations: classpath:mappers/*.xml
  typeAliasesPackage: com.mybatis.pojo

4、SpringBootMapper.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:命名空间 -->
<mapper namespace="com.mybatis.dao.UserSpringBootMapper">
    <select id="selectAll" resultType="user">
        select * from tb_user;
    </select>
</mapper>

5、springboot服务启动文件

package com.mybatis;

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

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/7
 */
@SpringBootApplication
@MapperScan("com.mybatis.dao")
public class SpringBootMybatisDemoApplication {

    static Logger log = LoggerFactory.getLogger(SpringBootMybatisDemoApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisDemoApplication.class,args);
        log.info("---------启动成功---------");
    }
}

6、controller层

package com.mybatis.controller;


import com.mybatis.pojo.User;
import com.mybatis.service.UserSpringBootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/7
 */
@RestController
public class UserController {

    @Autowired
    private UserSpringBootService userSpringBootService;

    @GetMapping(value = "/str")
    public String str(){
        return "springboot启动成功。。。";
    }

    @GetMapping(value = "/selectAll")
    public List<User> selectAll(){
        return userSpringBootService.selectAll();
    }
}

7、service层

7.1、 UserSpringBootService.java

package com.mybatis.service;

import com.mybatis.dao.UserSpringBootMapper;
import com.mybatis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/7
 */

public interface UserSpringBootService {

    public List<User> selectAll();
}

7.2、UserSpringBootServiceImpl.java

package com.mybatis.service.impl;

import com.mybatis.dao.UserSpringBootMapper;
import com.mybatis.pojo.User;
import com.mybatis.service.UserSpringBootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/9
 */
@Service(value = "userSpringBootService")
public class UserSpringBootServiceImpl implements UserSpringBootService {

    @Autowired
    private UserSpringBootMapper userSpringBootMapper;

    public List<User> selectAll(){
        List<User> users =  this.userSpringBootMapper.selectAll();
        for (User user : users) {
            System.out.println(user);
        }
        return users;
    }
}

在这里插入图片描述
这里会飘红,但是没关系,不会报错的

8、dao层

package com.mybatis.dao;

import com.mybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/7
 */
public interface UserSpringBootMapper {

    public List<User> selectAll();
}

9、pojo层

package com.mybatis.dao;

import com.mybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author:LIUZHENSHENG247
 * Description:
 * CreateDate:2020/6/7
 */
public interface UserSpringBootMapper {

    public List<User> selectAll();
}

10、启动springboot

效果如下:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值