• 《Java Spring框架》Idea+gradle 整合Springboot和Mybatis


    准备工作:

    安装JDK,安装MySql,安装Idea; 安装Gradle4.7版本, 安装插件:Spring Assistant

    1. 新建项目:直接通过IDEA构建

    第二步:取名

    第三步:选择web。

    第四步:点击完成即可

    第五步:设置gradle本地,也可以使用默认。

    界面会一致下载jar包,过程会中断,大家耐心点,一般30分钟以内。

    加载好了之后目录结构:

    2.  官网构建,导入IDEA。

    官网网址:https://start.spring.io/

    点击绿色按钮,生成zip包,将生成的包,导入IDEA即可。

    3. 整合SpringBoot和MyBatis

    数据库设置一张表:

    项目目录结构:

    各个文件如下:

    mytest

    import com.seventh.icecastle.db.Acco;
    import com.seventh.icecastle.service.AccoInterface;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/mytest")
    public class mytest {
    
        @Autowired
        AccoInterface accoInterface;
    
        @GetMapping(value = "/all")
        public List<Acco> getall(){
            return accoInterface.getAccoAll();
        }
    }

    Acco

    public class Acco {
        private int id;
        private String name;
        private Double money;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Double getMoney() {
            return money;
        }
    
        public void setMoney(Double money) {
            this.money = money;
        }
    }

    AccoDao

    import com.seventh.icecastle.db.Acco;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface AccoDao {
    
        List<Acco> getAll();
    }

    AccoInterfaceImpl

    import com.seventh.icecastle.db.Acco;
    import com.seventh.icecastle.mapper.AccoDao;
    import com.seventh.icecastle.service.AccoInterface;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class AccoInterfaceImpl implements AccoInterface {
    
        @Autowired
        AccoDao accoDao;
    
        @Override
        public List<Acco> getAccoAll() {
            return accoDao.getAll();
        }
    }

    AccInterface

    import com.seventh.icecastle.db.Acco;
    
    import java.util.List;
    
    public interface AccoInterface {
        List<Acco> getAccoAll();
    }

    AccountCashMapper.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.seventh.icecastle.mapper.AccoDao">
    
        <select id="getAll" resultType="com.seventh.icecastle.db.Acco">
            select a.*  from account a
        </select>
    
    </mapper>

    application.properties

    server.port= 8080
    
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/work_db?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=*********
    mybatis.configuration.database-id=mysql
    
    mybatis.mapperLocations=classpath:mapper/*.xml

    浏览器访问:

    ok, 初步框架搭完成。   后续还可以将其他框架加入进来。

    This moment will nap, you will have a dream; But this moment study,you will interpret a dream.
  • 相关阅读:
    链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的
    无法定位程序输入点 _glutCreateWindowWithExit于动态链接库glut32.dll上
    Error:“应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。”
    虚函数和纯虚函数的区别
    VS2010和matlab2010混合编程中char16_t重定义的问题
    笔记本电脑关闭小键盘(即打字按P出现星号键)
    WIN7系统下U盘安装Ubuntu双系统
    The Basics of 3D Printing in 2015
    3D建模与处理软件简介
    win7-32 系统 + VS2010 配置 glew
  • 原文地址:https://www.cnblogs.com/jssj/p/12153433.html
Copyright © 2020-2023  润新知