快速创建一个项目springboot+mybatis+mysql

本文详细介绍了如何使用SpringBoot框架结合MyBatis进行数据库操作的全过程,从项目搭建到代码实现,再到数据库配置及最终的数据调用,提供了一个完整的实战案例。

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

目录

 

创建一个springboot项目

一.创建项目

二.配置文件

三.代码

四.数据库

五:调取


创建一个springboot项目

一.创建项目

二.配置文件

application.properties

#端口号,不配置,默认为8080
server.port=8080

#数据源必填项
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/yydemo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.username = root
spring.datasource.password = 123456

#加载Mybatis配置文件
mybatis.mapper-locations = classpath:mapper/*Mapper.xml

#springboot集成mybatis,实现在控制台打印出执行的sql语句。#其中com.yy.springbootdemo.dao写自己的Dao文件夹所在路径
logging.level.com.yy.springbootdemo.dao=debug

三.代码

1.结构

2.代码

domain层:Student
package com.yy.springbootdemo.domain;

import lombok.Data;

@Data
public class Student {
    private Long id;
    private String username;
    private Integer age;
}

dao层:StudentDao

package com.yy.springbootdemo.dao;

import com.yy.springbootdemo.domain.Student;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface StudentDao {
    List<Student> selectAll();
}

service层:StudentService

package com.yy.springbootdemo.service;

import com.yy.springbootdemo.domain.Student;
import java.util.List;

public interface StudentService {
    List<Student> selectAll();
}
service.impl层:StudentServiceImpl
package com.yy.springbootdemo.service.impl;

import com.yy.springbootdemo.dao.StudentDao;
import com.yy.springbootdemo.domain.Student;
import com.yy.springbootdemo.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;

    @Override
    public List<Student> selectAll() {
        List<Student> list = studentDao.selectAll();
        return list;
    }
}
controller层:StudentController
package com.yy.springbootdemo.controller;

import com.yy.springbootdemo.domain.Student;
import com.yy.springbootdemo.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @RequestMapping("selectAll")
    public List<Student> selectAll(){
        List<Student> list = studentService.selectAll();
        return list;
    }
}
控制:SpringBootApplication
package com.yy.springbootdemo;

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

@SpringBootApplication
@MapperScan(basePackages = {"com.yy.springbootdemo.dao"})
@ComponentScan(basePackages = {"com.yy.springbootdemo.domain","com.yy.springbootdemo.dao","com.yy.springbootdemo.service","com.yy.springbootdemo.controller"})
public class SpringBootDemoApplication {

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

}

mapper:StudentMapper.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.yy.springbootdemo.dao.StudentDao">

<select id="selectAll"  resultType="com.yy.springbootdemo.domain.Student">
        select * from student
    </select>

</mapper>

四.数据库

五:调取

http://localhost:8080/student/selectAll

返回结果:

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值