IDEA+Gradle创建Springboot项目整合mybatis

2023-11-02

1.之前用的maven管理项目,现在公司用gradle,所以自己学习创建一个,特此记录一下(前提是你得配置了gradle)

1.首先是build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.inspur'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    maven {
        url = 'http://repo.inspur.com/artifactory/ecbu-gradle-release-local'
        credentials {
            username 'bss'
            password 'USTdYtIj'
        }
    }
    mavenCentral()
    mavenLocal()
}

dependencies {

    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
    compile('org.projectlombok:lombok:1.18.2')
    runtime('mysql:mysql-connector-java')
    compile('mysql:mysql-connector-java')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('com.alibaba:fastjson:1.2.5')
    compile('com.github.pagehelper:pagehelper:5.1.2')

    implementation 'org.springframework.boot:spring-boot-starter-freemarker'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

2. 创建数据库表

CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `score` varchar(255) DEFAULT NULL,
  `class1` varchar(255) DEFAULT NULL,
  `school` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '熊大', '20', '100', '动物系', '北京大学');
INSERT INTO `student` VALUES ('2', '熊二', '21', '90', '动物系', '南京大学');
INSERT INTO `student` VALUES ('3', '光头强', '18', '100', '伐木系', '复旦大学');

3.创建项目结构

Controller

package com.inspur.demo.controller;

import com.inspur.demo.entity.Student;
import com.inspur.demo.service.StudentService;
import com.inspur.demo.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping(value = "/all")
    public List<Student> getall(){
        return studentService.findAll();
    }

}

Service

package com.inspur.demo.service;

import com.inspur.demo.entity.Student;
import com.inspur.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;

    public List<Student> findAll() {
        return studentMapper.queryAll();
    }

}

Mapper

package com.inspur.demo.mapper;

import com.inspur.demo.entity.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {
    List<Student> queryAll();

}

Entity

package com.inspur.demo.entity;

import lombok.Data;

@Data
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Integer score;
    private String class1;
    private String school;

    public Student(){}
    public Student(Integer id,String name,Integer age,Integer score,String class1,String school){
        this.id=id;
        this.name=name;
        this.age=age;
        this.score=score;
        this.class1=class1;
        this.school=school;
    }

}

4.resources目录下添加mybatis的配置文件(文件名是  mybatis-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="callSettersOnNulls" value="true"/>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="cacheEnabled" value="false"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration> 

日志配置文件( 文件名是 logback-spring.xml)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <appender name="LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Prudent>true</Prudent>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>log/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

            <maxHistory>30</maxHistory>
            <totalSizeCap>1GB</totalSizeCap>

        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern> %d{yyyy-MM-dd HH:mm:ss} -%msg%n</Pattern>
        </layout>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%15thread{15}] %-40logger{40} : %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org" level="INFO"/>
    <logger name="sun" level="INFO"/>
    <logger name="com.inspur" level="INFO" />
    <root level="INFO">
        <appender-ref ref="LOG_FILE" />
        <!--<appender-ref ref="FILE" />-->
    </root>
</configuration>

5.添加mapper.xml文件(名字是  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.inspur.demo.mapper.StudentMapper">

    <select id="queryAll" resultType="com.inspur.demo.entity.Student">
        select * from student
    </select>
</mapper>

6.添加application.yml文件

server:
  port: 8081
spring:
  application:
    name: demo
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

mybatis:
    mapper-locations: classpath:mapper/**/*.xml
    config-location: classpath:mybatis-config.xml

6.项目启动类

7.启动测试

8.大功告成

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IDEA+Gradle创建Springboot项目整合mybatis 的相关文章

随机推荐

  • TensorRT 7 动态输入和输出

    这几天折腾了使用TensorRT加速一个BERT模型 在动态输入和输出这一块费了不少的劲 现在把这一块记录下来 算是自己的一点经验吧 我的环境 torch 1 5 0 tensorrt 7 0 0 11 我的思路是 1 Torch模型转on
  • Ansible之playbook

    playbook 1 实施playbook 1 1 Ansible Playbook与临时命令 1 2 格式化Ansible Playbook YAML语言 1 3 运行playbook 1 4 提高输出的详细程度 1 5 语法验证 1 6
  • Element之input输入框

    注 input不支持 v model 修饰符 一 基础用法
  • VMware 双网卡静态IP配置之后,主机ping不虚通拟机。

    我说说我的问题 毕竟每个人的环境不一样 1 首先保证这里都是连接启状态 2 在虚拟机里连个ip都是ping痛的 3 虚拟机的防火墙和windows防火强都是关闭状态 4 ping虚拟机网关是可以ping通的 解决方案 两次遇到到了这样的问题
  • K-Means算法

    K Means算法 K means算法是将样本聚类成k个簇 cluster 具体算法描述如下 1 随机选取k个聚类质心点 cluster centroids 为 2 重复下面过程直到收敛 对于每一个样例i 计算其应该属于的类 对于每一个类j
  • [leetcode]20.20. 有效的括号

    给定一个只包括 的字符串 s 判断字符串是否有效 有效字符串需满足 左括号必须用相同类型的右括号闭合 左括号必须以正确的顺序闭合 示例 1 输入 s 输出 true 示例 2 输入 s 输出 true 这道题就是栈的应用 char chan
  • git commit 提交规范及配置

    开发过程中 希望commit提交更为规范 找到了这样一个插件 分享一下 使用后的效果 这样多人开发的时候 提交代码更为清晰了 接下来是安装流程 1 安装commitizen和cz customizable npm i g commitize
  • 分段处理_三菱PLC简单工程的分段编程,像结构化编程一样,一目了然

    三菱的PLC编程分为简单工程和结构化工程 在结构化工程中可以分段处理程序 可以为每一段的程序命名 在编辑的时候能在不同的页面进行 如下图 结构化编程 这样我们无论是编程 监控 维护等方面都很方便找到 而不用再一个页面里上下拉去看程序 尤其实
  • 【LSTM分类】基于白鲸算法优化双向长短期记忆神经网络的数据分类预测附matlab代码 BWO-BiLSTM...

    作者简介 热爱科研的Matlab仿真开发者 修心和技术同步精进 matlab项目合作可私信 个人主页 Matlab科研工作室 个人信条 格物致知 内容介绍 白鲸优化算法 Beluga whale optimization BWO 由Chan
  • 图像处理Matlab(二)——生成随机点

    摘要 生成40个数据对 并作图 程序如下 figure set gcf Position 400 300 200 200 a1 40 set axes Color 128 255 128 255 128 255 hold on C rand
  • CentOS-7,网络ping不通详解

    官网下载地址 https www centos org download 问题1 就是题目说的ifconfig命令不起效果了 而且在sbin目录中没有ifconfig文件 原因 这是因为centos7已经不适用ifconfig命令了 已经用
  • mmsegmentaion安装记录

    mmsegmentaion安装记录 安装anaconda环境 conda create name Hopenmmlab python 3 8 y conda activate Hopenmmlab 安装GPU torch pip3 inst
  • 静态单赋值(二)—gcc中的SSA化算法

    版权声明 本文为CSDN博主 ashimida 的原创文章 遵循CC 4 0 BY SA版权协议 转载请附上原文出处链接及本声明 原文链接 https blog csdn net lidan113lidan article details
  • 分布式高频量化交易系统架构讲解(企业版,期货ctp,股票xtp,数字货币,附全部源码)(值得收藏)

    目录 1 量化交易系统简介 1 1行情数据 1 2交易策略 1 3交易 2 分布式高频量化交易系统 2 1 架构图 2 1 1量化交易系统教程地址 2 1 2量化交易系统教程中讲解的期货ctp知识点 2 2 交易系统功能介绍 2 3 账户系
  • UE4--材质地形篇——地形材质的运用(混合材质)

    引言 我们所做的的地形材质都是多种多样的 有的地形是山地 其中可能有植被与岩石 有的地形是荒漠 它就包括了泥土与沙子 总之 其中材质远不止一种 那么我们如何创建多个材质组成的混合体呢 我们创建材质的时候通常只能创建一种材质 比如透明材质 水
  • easy-monitor3.0 nodejs性能监控和分析工具

    easy monitor性能监控和分析工具 Easy Monitor 3 0 https blog csdn net qq 36791889 article details 115420116 git地址 https github com
  • qbxt国庆刷题班 游记$总结

    今天是 2019 10 4 距离 csp 也就是 AFO 不远了 鬼知道我为什么拖到今天才写这次清北学堂的游记 准确的说鬼知道我为啥要写游记 而且到现在才写 也许是给未来留点回忆吧 Day4 早上懒床拖到 7 26 赶紧吃了昨天买的牛肉粉方
  • Java学习笔记7——类和对象

    类和对象 类的特点 类的定义 定义样例 对象的使用 对象在内存中的位置 类 类是对现实生活种一类具有 类的特点 类是对象得数据类型 类是具有相同属性和行为得一组对象得集合 属性指的是对象具有的各种特征 每个对象的每个属性都有特定的值 行为指
  • 什么是透明、匿名、高匿代理?详解!

    随着大数据的应用越来越广泛 应用的行业也越来越多 我们每日都可以看到大数据的一些新颖的应用 从而帮助人们从中获取到真正有用的价值 随着很多工作的开展 我们需要大量的IP操作 这时为了避免IP被封 使用代理IP是个很好的选择 而IP代理按匿名
  • IDEA+Gradle创建Springboot项目整合mybatis

    1 之前用的maven管理项目 现在公司用gradle 所以自己学习创建一个 特此记录一下 前提是你得配置了gradle 1 首先是build gradle文件 buildscript ext springBootVersion 2 1 2