springboot集成mybatis

本文详细介绍了如何在IntelliJ IDEA中创建一个SpringBoot项目,并整合MyBatis进行数据库操作,包括添加依赖、创建实体类、Mapper、Service及Controller,最后通过浏览器访问接口验证功能。

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

工具:idea

数据库:mysql

1、idea创建一个springboot项目

选择Spring Initializr

选择web,勾选Spring Web Starter ,next后finish

2、在pom.xml添加mybatis和mysql依赖

<?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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-mybatis-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--Mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建相应的文件,下面是完整项目结构

User.java

package com.example.springbootmybatisdemo.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private Long id;

    private String niceName;

    private String name;

    private Date createTime;

    private String idCar;

    private String sex;

    private String mobilePhone;

    private String email;

    private static final long serialVersionUID = 1L;

    public Long getId() {
        return id;
    }

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

    public String getNiceName() {
        return niceName;
    }

    public void setNiceName(String niceName) {
        this.niceName = niceName == null ? null : niceName.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getIdCar() {
        return idCar;
    }

    public void setIdCar(String idCar) {
        this.idCar = idCar == null ? null : idCar.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getMobilePhone() {
        return mobilePhone;
    }

    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone == null ? null : mobilePhone.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", niceName=").append(niceName);
        sb.append(", name=").append(name);
        sb.append(", createTime=").append(createTime);
        sb.append(", idCar=").append(idCar);
        sb.append(", sex=").append(sex);
        sb.append(", mobilePhone=").append(mobilePhone);
        sb.append(", email=").append(email);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

 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="com.example.springbootmybatisdemo.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.example.springbootmybatisdemo.entity.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="nice_name" jdbcType="VARCHAR" property="niceName" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="id_car" jdbcType="VARCHAR" property="idCar" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="mobile_phone" jdbcType="VARCHAR" property="mobilePhone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
  </resultMap>

  <select id="selectAll" resultMap="BaseResultMap">
    select id, nice_name, `name`, create_time, id_car, sex, mobile_phone, email
    from buss_user
  </select>
</mapper>

UserMapper.java

package com.example.springbootmybatisdemo.mapper;

import com.example.springbootmybatisdemo.entity.User;

import java.util.List;

public interface UserMapper {

    List<User> selectAll();
}

UserService.java

package com.example.springbootmybatisdemo.service;

import com.example.springbootmybatisdemo.entity.User;

import java.util.List;

public interface UserService {
    List<User> selectAll();
}

UserServiceImpl.java

package com.example.springbootmybatisdemo.service.impl;

import com.example.springbootmybatisdemo.entity.User;
import com.example.springbootmybatisdemo.mapper.UserMapper;
import com.example.springbootmybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }
}

UserController.java

package com.example.springbootmybatisdemo.ctrl;

import com.example.springbootmybatisdemo.entity.User;
import com.example.springbootmybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController// 添加该注解类中的方法都会以 json 的格式返回
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public List<User> findAll(){
        return userService.selectAll();
    }
}

 SpringbootMybatisDemoApplication.java

package com.example.springbootmybatisdemo;

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

@SpringBootApplication
@MapperScan("com.example.springbootmybatisdemo.mapper")// 指定mybatis接口扫描路径
public class SpringbootMybatisDemoApplication {

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

}

 application.properties


#数据库配置文件
spring.datasource.url=jdbc:mysql://192.168.50.131:3306/test?useUnicode=true?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=Root123456.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#映射文件路径
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#模型所在的路径,不用在xml文件写实体类的全路径
mybatis.type-aliases-package=com.example.springbootmybatisdemo.entity

 启动项目SpringbootMybatisDemoApplication.java

在浏览器输入 localhost:8080/user/findAll

 报错了

 报错是应为在mysql一类中没有指定版本,点进去看默认使用了最高版本

指定一下版本就可以了

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值