数据库增删改查练习
Swagger2Config文件配置
配置Swagger2Config文件
package com
.example
.demo
.config
;
import org
.springframework
.context
.annotation
.Bean
;
import org
.springframework
.context
.annotation
.Configuration
;
import springfox
.documentation
.builders
.ApiInfoBuilder
;
import springfox
.documentation
.builders
.PathSelectors
;
import springfox
.documentation
.builders
.RequestHandlerSelectors
;
import springfox
.documentation
.service
.ApiInfo
;
import springfox
.documentation
.spi
.DocumentationType
;
import springfox
.documentation
.spring
.web
.plugins
.Docket
;
import springfox
.documentation
.swagger2
.annotations
.EnableSwagger2
;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket
createRestApi(){
return new Docket(DocumentationType
.SWAGGER_2
)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.example.demo.controller"))
.paths(PathSelectors
.any())
.build();
}
private ApiInfo
apiInfo() {
return new ApiInfoBuilder()
.title("SwaggerUI演示")
.description("内蒙古电子大数据工坊")
.contact("xhd")
.version("1.0")
.build();
}
}
UserController文件配置
配置UserController文件
package com
.example
.demo
.controller
;
import com
.example
.demo
.entity
.User
;
import com
.example
.demo
.service
.RedisService
;
import com
.example
.demo
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.web
.bind
.annotation
.*
;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private RedisService redisService
;
@Autowired
private UserService userService
;
@GetMapping("/queryByid")
public String
queryByid(int id
){
String userStr
= redisService
.get(id
+ "");
return userService
.queryById(id
).toString();
}
@DeleteMapping("/delete")
public Boolean
deletUserById(int id
){
userService
.deletUserById(id
);
return true;
}
@PostMapping("/saveOrUpdateUser")
public User
saveOrUpdateUser(User user
){
return userService
.save(user
);
}
}
UserDao文件配置
配置UserDao文件
package com
.example
.demo
.dao
;
import com
.example
.demo
.entity
.User
;
import org
.springframework
.data
.jpa
.repository
.JpaRepository
;
import java
.util
.List
;
public interface UserDao extends JpaRepository <User, Integer>{
User
findById(int id
);
}
User文件配置
配置User文件
package com
.example
.demo
.entity
;
import lombok
.Data
;
import javax
.persistence
.*
;
import java
.math
.BigDecimal
;
import java
.util
.Date
;
@Table(name
= "company_monitor_sensor")
@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy
= GenerationType
.IDENTITY
)
private int id
;
private String company_code
;
private Integer monitor_type
;
private String sys_name
;
private int equipment_type_id
;
private String equipment_type_name
;
private int sensor_type_id
;
private String sensor_type_name
;
private int technology_type_id
;
private String technology_type_name
;
private String department
;
private String sensor_code
;
private String location
;
private String storage_material
;
private BigDecimal level_alarm_high
;
private BigDecimal secondary_alarm_high
;
private BigDecimal level_alarm_low
;
private BigDecimal secondary_alarm_low
;
private int operate_id
;
private String operate_username
;
private Date create_time
;
private Date update_time
;
public User(){
}
}
RedisServiceImpl文件配置
配置RedisServiceimpl文件
package com
.example
.demo
.service
.impl
;
import com
.example
.demo
.service
.RedisService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.StringRedisTemplate
;
import org
.springframework
.stereotype
.Service
;
@Service
public class RedisServiceimpl implements RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate
;
@Override
public void set(String key
, String value
) {
stringRedisTemplate
.opsForValue().set(key
, value
);
}
@Override
public String
get(String key
) {
return stringRedisTemplate
.opsForValue().get(key
);
}
@Override
public boolean expire(String key
, long expire
) {
return false;
}
@Override
public void remove(String key
) {
}
@Override
public long increment(String key
, long delta
) {
return 0;
}
}
UserServiceImpl文件配置
配置UserServiceImpl文件
package com
.example
.demo
.service
.impl
;
import com
.example
.demo
.dao
.UserDao
;
import com
.example
.demo
.entity
.User
;
import com
.example
.demo
.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 User
save(User user
) {
return userDao
.save(user
);
}
@Override
public void deletUserById(int id
) {
userDao
.deleteById(id
);
}
@Override
public User
queryById(int id
) {
return userDao
.findById(id
);
}
@Override
public List
<User> queryByAge(int age
) {
return null
;
}
}
RedisService文件配置
配置RedisService文件
package com
.example
.demo
.service
;
public interface RedisService {
void set(String key
, String value
);
String
get(String key
);
boolean expire(String key
, long expire
);
void remove(String key
);
long increment(String key
, long delta
);
}
UserService文件配置
配置UserService文件
package com
.example
.demo
.service
;
import com
.example
.demo
.entity
.User
;
import java
.util
.List
;
public interface UserService {
User
save(User user
);
void deletUserById(int id
);
User
queryById(int id
);
List
<User> queryByAge(int age
);
}
DemoApplication文件配置
配置DemoaApplication文件
package com
.example
.demo
;
import org
.springframework
.boot
.SpringApplication
;
import org
.springframework
.boot
.autoconfigure
.SpringBootApplication
;
import org
.springframework
.boot
.SpringApplication
;
import org
.springframework
.boot
.autoconfigure
.SpringBootApplication
;
@SpringBootApplication
public class DemoApplication {
public static void main(String
[] args
) {
SpringApplication
.run(DemoApplication
.class, args
);
}
}
application文件配置
配置application文件
spring
:
datasource
:
type
: com
.alibaba
.druid
.pool
.DruidDataSource
driver
-class-name
: com
.mysql
.cj
.jdbc
.Driver
url
: jdbc
:mysql
://localhost
:3306/lx
?useUnicode
=true&characterEncoding
=UTF
-8&autoReconnect
=true&useSSL
=false&serverTimezone
=UTC
username
: root
password
: 123456
initialSize
: 20
minIdle
: 50
maxActive
: 500
jpa
:
hibernate
:
ddl
-auto
: update
show
-sql
: true
redis
:
host
: localhost # Redis服务器地址
database
: 0 # Redis数据库索引(默认为
0)
port
: 6379 # Redis服务器连接端口
password
: 123456 # Redis服务器连接密码(默认为空)
jedis
:
pool
:
max
-active
: 8 # 连接池最大连接数(使用负值表示没有限制)
max
-wait
: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max
-idle
: 8 # 连接池中的最大空闲连接
min
-idle
: 0 # 连接池中的最小空闲连接
timeout
: 3000ms # 连接超时时间(毫秒)
DemoApplicationTests文件配置
配置DemoApplicationTests文件
package com
.example
.demo
;
import org
.junit
.jupiter
.api
.Test
;
import org
.springframework
.boot
.test
.context
.SpringBootTest
;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
pom文件配置
配置pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion
>
<parent>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-parent
</artifactId
>
<version>2.3.3.RELEASE
</version
>
<relativePath
/> <!-- lookup parent from repository
-->
</parent
>
<groupId>com
.example
</groupId
>
<artifactId>demo
</artifactId
>
<version>0.0.1-SNAPSHOT
</version
>
<name>demo
</name
>
<description>Demo project
for Spring Boot
</description
>
<properties>
<java.version>1.8</java
.version
>
</properties
>
<dependencies>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-jdbc
</artifactId
>
</dependency
>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-data
-jdbc
</artifactId
>
</dependency
>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-data
-jpa
</artifactId
>
</dependency
>
<dependency>
<groupId>mysql
</groupId
>
<artifactId>mysql
-connector
-java
</artifactId
>
<scope>runtime
</scope
>
</dependency
>
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>druid
</artifactId
>
<version>1.1.21</version
>
</dependency
>
<dependency>
<groupId>org
.projectlombok
</groupId
>
<artifactId>lombok
</artifactId
>
<optional>true</optional
>
</dependency
>
<!--rides依赖
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-data
-redis
</artifactId
>
</dependency
>
<!--swagger依赖
-->
<dependency>
<groupId>io
.springfox
</groupId
>
<artifactId>springfox
-swagger
-ui
</artifactId
>
<version>2.7.0</version
>
</dependency
>
<dependency>
<groupId>io
.springfox
</groupId
>
<artifactId>springfox
-swagger2
</artifactId
>
<version>2.7.0</version
>
</dependency
>
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-test
</artifactId
>
<scope>test
</scope
>
<exclusions>
<exclusion>
<groupId>org
.junit
.vintage
</groupId
>
<artifactId>junit
-vintage
-engine
</artifactId
>
</exclusion
>
</exclusions
>
</dependency
>
</dependencies
>
<build>
<plugins>
<plugin>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-maven
-plugin
</artifactId
>
</plugin
>
</plugins
>
</build
>
</project
>
结果图
数据增加修改 数据查询 数据删除