idea 搭建 springboot 2.3.0 + mybatis + thymeleaf + mysql 项目 超详细

1.打开idea,选择file–>new–>project
在这里插入图片描述
2.点击next配置group和Artifact
在这里插入图片描述
3.点击next,给项目添加依赖
在这里插入图片描述
4.点击next,项目结构如图
在这里插入图片描述
5.选择file–>settings给项目设置maven配置
在这里插入图片描述
6.下载依赖包

打开pom.xml文件,鼠标右键选择maven–>reimport
在这里插入图片描述
7.如图下载成功
在这里插入图片描述
8.springboot自动配置优先加载application.properties,其次application.yml,由于书写风格选择使用application.yml。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
    username: root
    password: root

9.启动项目成功,说明sringboot项目搭建成功!
在这里插入图片描述
整合mybatis
10.yml中添加mybatis配置

mybatis:
  mapper-locations: classpath:mapper/*.xml  #classpath就是应用程序resources的路径
  type-aliases-package: com.iflytek.edu.model
  configuration:
    log-impl : org.apache.ibatis.logging.stdout.StdOutImpl #通过日志将sql再控制台打印出来
    mapUnderscoreToCamelCase: true #设为true表示开启驼峰转换

11.创建controller类

package com.iflytek.edu.controller;

import com.iflytek.edu.model.User;
import com.iflytek.edu.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @create 2020-07-10-23:50
 **/
@RestController
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping("/test")
    public List<User> getUsers(){

           return  testService.getUsers();
    }
}

12.创建service类

package com.iflytek.edu.service;

import com.iflytek.edu.dao.UserDao;
import com.iflytek.edu.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @create 2020-07-10-23:50
 **/
@Service
public class TestService {

    @Autowired
    private UserDao userDao;

    public List<User> getUsers() {
      return   userDao.getUsers();
    }
}

13.创建接口

package com.iflytek.edu.dao;

import com.iflytek.edu.model.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by 14512 on 2020/7/10.
 */
 //这块的mapper注解相当于生成一个类的代理类去找到xml中的对应方法
@Mapper
public interface UserDao {
    //这块需要注意一点方法中的参数如果是多个最好加上@Param注解,不然不同版本的开发工具可能会报参数找不到
    List<User> getUsers();
}

14.创建实体类

package com.iflytek.edu.model;

/**
 * @create 2020-07-10-23:44
 **/
public class User {
    private Integer id;
    private String name;
    private Integer sex;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getSex() {
        return sex;
    }

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

    public User(Integer id, String name, Integer sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public User() {
    }
}

15.创建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.iflytek.edu.dao.UserDao">
    //这块为什么可以直接写user是因为yml中配置了限定包别名,只要这个包下有这个类型就可以这么写,不然要指定全部限定名
    <select id="getUsers" resultType="User">
        select * from `user`
    </select>
</mapper>

16.在启动类上加上MapperScan注解

package com.iflytek.edu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//这个注解就是让springboot去扫描指定包下的所有带有mapper注解的接口实现类,生成动态代理类
@MapperScan("com.iflytek.edu.dao")
public class HnezzhxyApplication {

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

}

17.大致配完如图
在这里插入图片描述
18.postman测试
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40974235/article/details/107153246
今日推荐