Spring boot整合Mybatis

本文介绍了如何将Spring Boot与Mybatis进行整合,包括添加pom依赖、配置jdbc和Mybatis、创建测试表及对应的实体类、Mapper、Service和Controller。通过这个过程,可以实现对数据库的简单操作。请确保在表中填充数据,并通过指定端口访问项目。对于初学者,这是一个很好的实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为web项目怎么能不连接数据库呢,下面将说说如何在Spring boot中整合Mybaits
  1. 添加pom依赖:
     <!--mysql驱动包  -->
        <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
        </dependency>
    <!-- mybatis依赖 -->
    <!--boot整合mybatis的关键包annotation  -->
            <dependency>
               <groupId>org.mybatis.spring.boot</groupId>
               <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>1.1.1</version>
    </dependency>

然后就是配置文件了:

jdbc连接配置:

spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.datasource.username: root
spring.datasource.password: root

Mybaits配置:

mybatis.configuration.map-underscore-to-camel-case: true
mybatis.configuration.log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
mybatis.configuration.call-setters-on-nulls: true
mybatis.configuration.default-fetch-size: 100
mybatis.configuration.default-statement-timeout: 30
mybatis.mapper-locations: classpath:mapper/**/**/*.xml
mybatis.type-handlers-package: org.apache.ibatis.type.LocalDateTypeHandler

下面测试配置是否正确:

在你连接是数据库下创建一张测试使用的表:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `sex` varchar(10) DEFAULT NULL,
  `age` int(2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

很简单的一张表
然后是创建这张表的对象:
UserDao

public class UserDao {

    private int id;
    private int age;
    private String name;
    private String sex;

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "UserDao{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

创建Controller:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping()
public class test {
    @RequestMapping("/userId")
    @ResponseBody
    public UserDao userId(int id){
        return service.selectId(id);
    }
}

创建UserService 以及UserServiceImpl

public interface UserService {
    public UserDao selectId(int id);
}



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public UserDao selectId(int id) {
        return userMapper.selectId(id);
    }
}

创建UserMapper

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserMapper {

    public UserDao selectId(int id);
}

最后是userMapper.xml:

 <select id="selectId" parameterType="int" resultType="com.example.demo.model.dao.UserDao">
      SELECT id,name,sex,age FROM user WHERE id = #{id}
    </select>

所有文件的结构

记得在表中填充数据!!!记得在表中填充数据!!!记得在表中填充数据!!!
然后就可以访问你的项目
因为我配置了端口号的,所以是9100,如果你们没有配置端口号就是默认的8080
这里写图片描述

我也只是个新人,有什么不对的地方希望大佬提出来,我好加以修正。
有兴趣的小伙伴也可以加QQ群,一起交流:606278528

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值