崛起于Springboot2.X+Jpa(61)

编程

《SpringBoot2.X心法总纲》

项目Git地址:https://gitee.com/mdxl/blog.git

1、pom依赖

      勾选lombok、web、jpa、jdbc、mysql依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</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-web</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

2、application.properties

server.port=8082

# Mysql

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false

spring.datasource.username=root

spring.datasource.password=root

# none, update, create, or create-drop

spring.jpa.hibernate.ddl-auto=update

# sql日志上输出到文件中

spring.jpa.show-sql=true

# 设置方言+引擎

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

# 命名策略

spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

3、实体类

一定要有注解@Entity和@Table

import javax.persistence.*;

@Table(name = "osc_user")

@Data

@ToString

@Entity

public class UserEntity {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private int id;

private String name;

private int age;

private String address;

private String hobby;

}

4、Mapper

public interface UserMapper extends JpaRepository<UserEntity,Integer> {

}

5、Controller

@RestController

@RequestMapping(value = "/jpa")

@Slf4j

public class UserController {

@Autowired

UserMapper userMapper;

@GetMapping(value = "/getUser")

public void getUser(int id){

UserEntity userEntity = userMapper.getOne(id);

log.info(userEntity.toString());

}

}

6、表结构

CREATE TABLE `osc_user` (

`id` int(11) NOT NULL AUTO_INCREMENT COMMENT "主键",

`name` varchar(32) NOT NULL COMMENT "姓名",

`age` int(10) NOT NULL COMMENT "年龄",

`address` varchar(64) NOT NULL COMMENT "地址",

`hobby` varchar(32) NOT NULL COMMENT "爱好",

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT="用户描述表"

7、启动

启动成功截图:

调用接口,如图:

成功。

以上是 崛起于Springboot2.X+Jpa(61) 的全部内容, 来源链接: utcz.com/z/511405.html

回到顶部