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
select * from serverlog where id = #{id}
在启动类里加上注解用于给出需要扫描的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
测试成功