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

目录

 

创建一个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

返回结果:

 

 

 

 

 

 

快速搭建一个Spring Boot + MyBatis + Spring MVC的Web工程,可以按照以下步骤进行操作: 1. 创建一个新的Spring Boot项目。你可以使用Spring Initializr(https://start.spring.io/)或者在IDE中创建一个新的Spring Boot项目。 2. 在pom.xml文件中添加必要的依赖项,包括Spring Boot、MyBatis和Spring MVC: ```xml <!-- Spring Boot依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- MySQL驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 3. 创建数据库连接配置文件,比如application.properties或application.yml,配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 4. 创建数据库表对应的实体类和MyBatis的Mapper接口。 5. 编写MyBatis的Mapper XML文件,配置SQL语句和映射关系。 6. 创建Service和Controller层的类,用于处理业务逻辑和请求响应。 7. 运行项目,通过浏览器或其他工具访问API接口。 这样就完成了一个基本的Spring Boot + MyBatis + Spring MVC的Web工程的搭建。你可以根据实际需求进行扩展和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值