数据库准备
先整个表先
CREATE TABLE `operator` (
`id` bigint(20) PRIMARY KEY AUTO_INCREMENT COMMENT '唯一标示',
`code` varchar(20) DEFAULT NULL COMMENT '编码',
`name` varchar(64) DEFAULT NULL COMMENT '名称',
`status` char(1) DEFAULT '1' COMMENT '状态 1启用 0 停用',
`gmt_create` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
新建module
创建module
填入坐标
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
编写一个与数据库对应的实体类
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* 操作员实体
*
*/
@Entity
@Data
@EntityListeners(AuditingEntityListener.class)
public class Operator implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3752294262021766827L;
/**
* 唯一标示
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* 编码
*/
private String code;
/**
* 名称
*/
private String name;
/**
* 创建时间
*/
@CreatedDate //自动创建
private Date gmtCreate;
/**
* 修改时间
*/
@LastModifiedDate //有修改时 会自动时间
private Date gmtModified;
}
编写控制层 测试
package cn.gd.cz.hong.springjdademo.controller;
import cn.gd.cz.hong.springjdademo.dao.OperatorDao;
import cn.gd.cz.hong.springjdademo.entity.Operator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* hello
*/
@Slf4j
@RestController
@RequestMapping("/operator")
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
@Autowired
OperatorDao operatorDao;
@Transactional
@PostMapping("/add")
public String addUser(Operator operator) {
log.info("新增用户:{}", operator);
operator = operatorDao.save(operator);
return "新增成功,返回用户id为:" + operator.getId();
}
@GetMapping("/find/{id}")
public Operator findUser(@PathVariable Long id) {
log.info("查找用户ID:{}", id);
return operatorDao.findById(id).get();
}
@PostMapping("/del/{id}")
public String delUser(Long id) {
log.info("删除用户ID:{}", id);
Operator operator = new Operator();
operator.setId(id);
operatorDao.delete(operator);
return "用户id为:" + id + ",已被删除!";
}
@GetMapping("/find/{code}/{name}")
public List<Operator> findUserByCodeAndName(@PathVariable("code") String code, @PathVariable("name")String name) {
log.info("命名规则方式,查找用户:编码:{},名称:{}", code, name);
return operatorDao.findByCodeAndName(code, name);
}
@GetMapping("/find/paging/{code}")
public Page<Operator> findUserByCodePagin(@PathVariable("code") String code){
log.info("分页模式,查找用户:编码:{}", code);
//这里注意 page是从0开始的
return operatorDao.findByCode(code, PageRequest.of(0,10));
}
@GetMapping("/find/sql/{code}")
public List<Operator> findUserByQuerySql(@PathVariable("code") String code){
log.info("自定义sql方式,查找用户:编码:{}", code);
return operatorDao.queryByCode(code);
}
}
验证
为了测试方便
可以整合swagger
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
swagger配置
package cn.gd.cz.hong.springjdademo.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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger2 配置
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("cn.gd.cz.hong.springjdademo.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")
.description("SpringBoot整合Swagger,详细信息......")
.version("9.0")
.contact(new Contact("hong", "blog.csdn.net", "aaa@gmail.com"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
}
新增一个用户
这里没有在controller加上@Transactional 的时候没有新增成功 这里需要加上
我们看到新增的时候
新增一个用户 新增的用户
通过ID查询操作员
查询
网友评论