使用IDEA搭建SpringBoot项目

1打开IDEA,点击File→New→Project...,如图所示,选择Spring Initializr,点击NEXT即可。

 

2、修改group和artifact字段,artifact必须为小写,然后点击Next进行下一步。

 

3、选择左侧的Web、SQL。然后在中间部分选择我们需要的选项,最终选择结果如最右侧所示。然后点击NEXT,进行下一步。

 

4、在这里我们可以修改项目名称和项目保存的位置。确认自己输入的内容,点击Finish即可完成项目的创建。

 

5、生成的项目结构如图所示,其中***Application.java为项目的启动类,程序入口。

pom.xml为项目生成的pom文件。

resources为资源目录,其中application.properties为配置文件。

 

6、新建包com.example.demo.controller,然后再其下新建类HelloController,这个类就是Spring MVC里的一个普通控制器。

@RestController 是Spring4里的新注解,是@ResponseBody和@Controller的缩写。

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}

运行DemoApplication.java,然后访问地址

http://localhost:8080/hello

可以看到测试效果

 

整合Mybatis

  • 修改配置文件

将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件

application.yml文件内容:

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/bhyc-omgmt?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapping.xml
  type-aliases-package: com.example.demo.entity
  • 创建实体类

创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。

CSLog.java

package com.example.demo.entity;

public class CSLog {
    private Integer id;
    private String OPERATOR_ID;
    private String LOG_TYPE;

    public Integer getId() {
        return id;
    }

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

    public String getOperator_ID() {
        return OPERATOR_ID;
    }

    public void setOperator_ID(String OPERATOR_ID) {
        this.OPERATOR_ID = OPERATOR_ID;
    }

    public String getLog_Type() {
        return LOG_TYPE;
    }

    public void setLog_Type(String LOG_TYPE) {
        this.LOG_TYPE = LOG_TYPE;
    }

    @Override
    public String toString() {
        return "{" +
                "\"id\":" + this.id +
                ", \"OPERATOR_ID\":\"" + this.OPERATOR_ID + "\"" +
                ", \"LOG_TYPE\":\"" + this.LOG_TYPE + "\"}";
    }
}
CSLogController.java
package com.example.demo.controller;

import com.example.demo.service.CSLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class CSLogController {

    @Autowired
    private CSLogService logService;

    @RequestMapping("getlog/{id}")
    public String GetLog(@PathVariable int id){
        return logService.Sel(id).toString();
    }
}

CSLogService.java

package com.example.demo.service;

import com.example.demo.entity.CSLog;
import com.example.demo.mapper.CSLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CSLogService {
    @Autowired
    CSLogMapper logMapper;
    public CSLog Sel(int id){
        return logMapper.Sel(id);
    }
}

CSLogMapper.java

package com.example.demo.mapper;

import com.example.demo.entity.CSLog;
import org.springframework.stereotype.Repository;

@Repository
public interface CSLogMapper {

    CSLog Sel(int id);
}

CSLogMapping.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.example.demo.mapper.CSLogMapper">

    <resultMap id="BaseResultMap" type="com.example.demo.entity.CSLog">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="OPERATOR_ID" jdbcType="VARCHAR" property="OPERATOR_ID" />
        <result column="LOG_TYPE" jdbcType="VARCHAR" property="LOG_TYPE" />
    </resultMap>

    <select id="Sel" resultType="com.example.demo.entity.CSLog">
        select * from serverlog where id = #{id}
    </select>
</mapper>

在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("com.example.demo.mapper") 

package com.example.demo;

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

@MapperScan("com.example.demo.mapper") //扫描的mapper

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

重新启动,在浏览器中输入http://localhost:8080/test/getlog/17

测试成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值