commit 1d6e2ed0b2ffa922bd61d96478348ac0ffb35437 Author: e Date: Fri May 17 20:50:26 2024 +0800 正常的和数据库交互获取数据 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1de4c10 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.idea +*.iml +# Created by .ignore support plugin (hsz.mobi) +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2875dce --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.3.12.RELEASE + + + + com.mashibing + read_write_lock + 1.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + mysql + mysql-connector-java + 5.1.47 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + org.projectlombok + lombok + + + + \ No newline at end of file diff --git a/src/main/java/com/mashibing/ReadWriteLockStarterApp.java b/src/main/java/com/mashibing/ReadWriteLockStarterApp.java new file mode 100644 index 0000000..eefd4e8 --- /dev/null +++ b/src/main/java/com/mashibing/ReadWriteLockStarterApp.java @@ -0,0 +1,18 @@ +package com.mashibing; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author zjw + * @description + */ +@SpringBootApplication +@MapperScan(basePackages = "com.mashibing.mapper") +public class ReadWriteLockStarterApp { + + public static void main(String[] args) { + SpringApplication.run(ReadWriteLockStarterApp.class,args); + } +} diff --git a/src/main/java/com/mashibing/controller/AirController.java b/src/main/java/com/mashibing/controller/AirController.java new file mode 100644 index 0000000..6b00f8d --- /dev/null +++ b/src/main/java/com/mashibing/controller/AirController.java @@ -0,0 +1,24 @@ +package com.mashibing.controller; + +import com.mashibing.entity.Air; +import com.mashibing.service.AirService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author zjw + * @description + */ +@RestController +public class AirController { + + @Autowired + private AirService airService; + + @GetMapping("/air/{id}") + public Air getById(@PathVariable Long id){ + return airService.getById(id); + } +} diff --git a/src/main/java/com/mashibing/entity/Air.java b/src/main/java/com/mashibing/entity/Air.java new file mode 100644 index 0000000..6163dfe --- /dev/null +++ b/src/main/java/com/mashibing/entity/Air.java @@ -0,0 +1,17 @@ +package com.mashibing.entity; + +import lombok.Data; + +/** + * @author zjw + * @description + */ +@Data +public class Air { + + private Long id; + + private int pm25; + + private String monitoringStation; +} diff --git a/src/main/java/com/mashibing/mapper/AirMapper.java b/src/main/java/com/mashibing/mapper/AirMapper.java new file mode 100644 index 0000000..4a5b5de --- /dev/null +++ b/src/main/java/com/mashibing/mapper/AirMapper.java @@ -0,0 +1,15 @@ +package com.mashibing.mapper; + +import com.mashibing.entity.Air; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +/** + * @author zjw + * @description + */ +public interface AirMapper { + + @Select("select * from air where id = #{id}") + Air findById(@Param("id") Long id); +} diff --git a/src/main/java/com/mashibing/service/AirService.java b/src/main/java/com/mashibing/service/AirService.java new file mode 100644 index 0000000..9f10906 --- /dev/null +++ b/src/main/java/com/mashibing/service/AirService.java @@ -0,0 +1,11 @@ +package com.mashibing.service; + +import com.mashibing.entity.Air; + +/** + * @author zjw + * @description + */ +public interface AirService { + Air getById(Long id); +} diff --git a/src/main/java/com/mashibing/service/impl/AirServiceImpl.java b/src/main/java/com/mashibing/service/impl/AirServiceImpl.java new file mode 100644 index 0000000..0a4ed08 --- /dev/null +++ b/src/main/java/com/mashibing/service/impl/AirServiceImpl.java @@ -0,0 +1,24 @@ +package com.mashibing.service.impl; + +import com.mashibing.entity.Air; +import com.mashibing.mapper.AirMapper; +import com.mashibing.service.AirService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author zjw + * @description + */ +@Service +public class AirServiceImpl implements AirService { + + @Autowired + private AirMapper airMapper; + + @Override + public Air getById(Long id) { + Air air = airMapper.findById(id); + return air; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..2f3ea4b --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + datasource: + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/air + +mybatis: + configuration: + map-underscore-to-camel-case: true