Spring boot 集成Mybatis框架

 1.创建maven管理的项目,配置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>

    <groupId>org.example</groupId>
    <artifactId>pratice_account</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>
    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.yml</include>
                <include>**/*.xml</include>
            </includes>

            </resource>

        </resources>
    </build>


</project>

2.更改运行主类,使项目可以运行

package org.example;

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

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
@SpringBootApplication
@ComponentScan(basePackages = {"org.example.*"})
public class SpringbootApplication {
    public static void main(String[] args) {
       SpringApplication.run(SpringbootApplication.class,args);
       System.out.println("Spring boot 启动成功...");
    }
}

3.配置数据库

      在resource文件夹下,创建application.yml文件,主要配置数据库地址、用户名、密码、其中practice为新建的数据库名称。maybatis:mapper-locations配置扫描的存储sql语句的文件,注意扫描路径 名称的准确性

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/practice?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml

4.创建java类

依次在java包下创建如下文件夹:controller-service-dao-domain

在resource文件夹下创建mappers文件夹,存储*Mapper.xml文件,主要写sql语句

controller:接受http层请求,调用service接口

service:使用@Service将服务注入bean,方便controller层调用,同时调用dao层接口

dao:提供接口,与mappers文件夹对应

domain:提供数据类型,一般数据库表属性和domain中类属性一致,接受数据库返回的数据

domain层数据类型

package org.example.module.user.domain;

import lombok.Data;

import java.io.Serializable;
@Data
public class User implements Serializable {
        private int id;
        private String username;
        private String userpassword;
        private String userphone;
}
userMapper.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="org.example.module.user.dao.UserMapper">

  <select id="selectUser" resultType="org.example.module.user.domain.User">
      select * from practice.t_user;
  </select>
</mapper>
dao层的userMapper接口:

package org.example.module.user.dao;

import org.example.module.user.domain.User;

import java.util.List;

@Mapper
public interface UserMapper {

    public List<User> selectUser();
}
service 层接口:


package org.example.module.user.service;


import org.example.module.user.domain.User;

import java.util.List;

public interface UserService {
    List<User> selectUserList();
}
service层实现接口的类:
@Service注入Bean, @Resource使用Dao层的接口

package org.example.module.user.service;

import org.example.module.user.dao.UserMapper;
import org.example.module.user.domain.User;
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<User> selectUserList(){
        return userMapper.selectUser();
    }

}
controller层调用service:

package org.example.module.user.controller;

import org.example.module.user.dao.UserMapper;
import org.example.module.user.domain.User;
import org.example.module.user.service.UserService;
import org.example.module.user.service.UserServiceImpl;
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 javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService ;

    @GetMapping("/list")
    public List<User> getUserList(){
        return userService.selectUserList();
    }

}

5.访问接口

http://localhost:8080/user/list

会返回查询到的数据

6.注意的问题

pom.xml文件要配置<resource>,扫描资源

application.yml文件配置mybatis的路径名称要正确

*Mapper.xml文件中的namespace要对应上

写sql语句的.xml文件名和Dao层接口名要一致

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值