1、使用IDEA新建一个空的maven项目
2、整个工程目录如下图所示:
3、其中DemoApplocation.java为系统启动类,具体代码如下:
package com.cjs.example
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cjs.example.dao")
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}
4、HelloController.java代码如下:
package com.cjs.example.controller
import com.cjs.example.dto.UserDto;
import com.cjs.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class HelloController {
@Autowired
private UserService userService;
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello(){return "hello world!";}
@RequestMapping(value="/alluser", method=RequestMethod.GET)
public List<UserDto> getAllUser(){return userService.selectAllUser();}
}
5、UserService以及UserServiceImpl代码如下:
package com.cjs.example.service
import com.cjs.example.dto.UserDto;
import java.util.List;
public interface UserService{
List<UserDto> selectAllUser();
}
package com.cjs.example.service.impl;
import com.cjs.example.dao.UserDao;
import com.cjs.example.dto.UserDto;
import com.cjs.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public List<UserDto> selectAllUser(){
return userDao.selectAlluser();
}
}
6、UserDao代码如下:
package com.cjs.example.dao;
import com.cjs.example.dto.UserDto;
import org.springframework.stereotype.Repositort;
import java.util.List;
@Repository
public interface UserDao{
List<UserDto> selectAllUser();
}
7、UserMapper.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.cjs.example.dao.UserDao">
<resulrMap id="userList" type="com.cjs.example.dto.UserDto">
<result property="userId" column="userId"/>
<result property="name" column="name"/>
</resultMap>
<select id="selectAllUser" resultMap="userList">
select user_id as userId,name as name from tbl_user;
</select>
</mapper>
8、整个工程配置在application.properties中,配置如下
server.port:9999
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/systemdb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
management.security.enabled=false
9、以及pom.xml配置相关如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cjs</groupId>
<artifactId>springboottest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-complier-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<fork>false</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.cjs.example.DemoApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10、备注:actuator为监控与管理工具,存在以下链接可以访问:
应用配置类:
1、应用自动化配置报告:/autoconfig
2、上下文中创建的bean:/beans
3、应用中配置的属性信息报告:/configprops
4、应用所有可用的环境属性报告:/env
5、所有spring mvc的控制器映射关系报告:/mappings
6、返回应用自定义的信息:/info
度量指标类:
1、返回当前应用的各类重要度量指标,比如内存信息、线程信息、垃圾回收信息:/metrics