栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot + mybatis环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot + mybatis环境搭建

第一: 先看目录结构

第二: 各个文件内容

com/springstudy/demo/controller/IndexController.java 内容如下:

package com.springstudy.demo.controller;

import com.springstudy.demo.entity.AppInfo;
import com.springstudy.demo.service.AppInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/index")
public class IndexController {
    @Resource
    private AppInfoService appInfoService;

    @RequestMapping(value = "/index")
    public String index() {
        return "index/index";
    }

    @RequestMapping(value = "/demo")
    public String demo(Model model) {
        model.addAttribute("currentValue", "测试...333666");
        try {
            List appInfoList = appInfoService.getAllAppInfo();
            model.addAttribute("appInfoList", appInfoList);
        } catch (Exception e) {
            model.addAttribute("appInfoList", "无数据!!!");
            e.printStackTrace();
        }
        return "index/index";
    }
}

com/springstudy/demo/entity/AppInfo.java 代码如下:

package com.springstudy.demo.entity;

import java.io.Serializable;
import java.util.Date;


public class AppInfo implements Serializable {
    private String id;
    private String host_name;
    private String app_id;
    private Date create_time;
    private String app_name;
    private Double cpu_per;
    private Double mem_per;
    private String app_type;
    private String state;

    public AppInfo() {
    }

    public AppInfo(String id, String host_name, String app_id, Date create_time, String app_name, Double cpu_per, Double mem_per, String app_type, String state) {
        this.id = id;
        this.host_name = host_name;
        this.app_id = app_id;
        this.create_time = create_time;
        this.app_name = app_name;
        this.cpu_per = cpu_per;
        this.mem_per = mem_per;
        this.app_type = app_type;
        this.state = state;
    }

    public String getId() {
        return id;
    }

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

    public String getHost_name() {
        return host_name;
    }

    public void setHost_name(String host_name) {
        this.host_name = host_name;
    }

    public String getApp_id() {
        return app_id;
    }

    public void setApp_id(String app_id) {
        this.app_id = app_id;
    }

    public Date getCreate_time() {
        return create_time;
    }

    public void setCreate_time(Date create_time) {
        this.create_time = create_time;
    }

    public String getApp_name() {
        return app_name;
    }

    public void setApp_name(String app_name) {
        this.app_name = app_name;
    }

    public Double getCpu_per() {
        return cpu_per;
    }

    public void setCpu_per(Double cpu_per) {
        this.cpu_per = cpu_per;
    }

    public Double getMem_per() {
        return mem_per;
    }

    public void setMem_per(Double mem_per) {
        this.mem_per = mem_per;
    }

    public String getApp_type() {
        return app_type;
    }

    public void setApp_type(String app_type) {
        this.app_type = app_type;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

com/springstudy/demo/mapper/AppInfoMapper.java 内容如下:

package com.springstudy.demo.mapper;

import com.springstudy.demo.entity.AppInfo;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface AppInfoMapper {
    //查询所有数据
    public List getAllAppInfo() throws Exception;
}

com/springstudy/demo/service/AppInfoService.java 的内容如下:

package com.springstudy.demo.service;

import com.springstudy.demo.entity.AppInfo;
import com.springstudy.demo.mapper.AppInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;

    public List getAllAppInfo() throws Exception {
        return appInfoMapper.getAllAppInfo();
    }
}

启动文件 com/springstudy/demo/DemoApplication.java 内容如下:

package com.springstudy.demo;

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

@SpringBootApplication
@MapperScan("com.springstudy.demo.mapper")
public class DemoApplication {

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

}

mybatis/mapper/AppInfoMapper.xml 内容如下:





    
    
        
        
        
        
        
        
        
        
        
    

    
        select * from APP_INFO
    

mybatis/mybatis-config.xml 内容如下:





    

    



templates/index/index.html 内容如下:




    
    Title



首页
[[${currentValue}]]

[[${appInfoList}]]


application.properties 内容如下:

#应用名称
spring.application.name=springboot-study
#访问端口
server.port=8080
#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test5
spring.datasource.username=root
spring.datasource.password=root
#关闭thymeleaf模板缓存
spring.thymeleaf.cache=false
#配置mybatis
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml

pom.xml 内容如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.springstudy
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
            2.5.5
        

        
            mysql
            mysql-connector-java
            5.1.46
        
        

        
        
            org.springframework.boot
            spring-boot-devtools
            
            true
        
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


浏览器访问,效果如图

 

采坑注意:

  1. 注解千万不要忘记写, 如@Resource , @AutoWired, @Service, @Repository
  2. 启动入口文件不要忘记写注解 @MapperScan("com.springstudy.demo.mapper")
  3. mybatis/mapper/AppInfoMapper.xml文件中字段映射千万要细心
  4. application.properties文件(不要忘记配置这两项)
    #配置mybatis
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    mybatis.config-location=classpath:mybatis/mybatis-config.xml 
  5. pom.xml文件的这两项
    
        org.springframework.boot
        spring-boot-starter-jdbc
        2.5.5
    
    
    
        mysql
        mysql-connector-java
        5.1.46
    
    
    
    
    
        org.springframework.boot
        spring-boot-devtools
        
        true
    
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号