提交 c894c9cf 编写于 作者: 一缕82年的清风's avatar 一缕82年的清风

[A] JPA代码提交

上级 cae6f57c
...@@ -45,6 +45,12 @@ ...@@ -45,6 +45,12 @@
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- MySQL连接 --> <!-- MySQL连接 -->
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
......
...@@ -2,6 +2,7 @@ package com.lsqingfeng.springboot; ...@@ -2,6 +2,7 @@ package com.lsqingfeng.springboot;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
/** /**
* @className: SpringBootLearningApplication * @className: SpringBootLearningApplication
...@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @date: 2022-01-10 14:36 * @date: 2022-01-10 14:36
*/ */
@SpringBootApplication @SpringBootApplication
@EnableJpaAuditing
public class SpringBootLearningApplication { public class SpringBootLearningApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootLearningApplication.class, args); SpringApplication.run(SpringBootLearningApplication.class, args);
......
...@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.*; ...@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.*;
* @date: 2022-01-17 13:58 * @date: 2022-01-17 13:58
*/ */
@RestController @RestController
@RequestMapping("jdbc") @RequestMapping("jpa")
public class JdbcController { public class JdbcController {
/** /**
...@@ -55,4 +55,9 @@ public class JdbcController { ...@@ -55,4 +55,9 @@ public class JdbcController {
userService.delete(id); userService.delete(id);
return Result.success(); return Result.success();
} }
@GetMapping("getByName")
public Result getByName(String name){
return Result.success(userService.getByName(name));
}
} }
...@@ -2,8 +2,18 @@ package com.lsqingfeng.springboot.dao; ...@@ -2,8 +2,18 @@ package com.lsqingfeng.springboot.dao;
import com.lsqingfeng.springboot.entity.User; import com.lsqingfeng.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.io.Serializable; import java.io.Serializable;
public interface UserDao extends JpaRepository<User,Integer>, Serializable { public interface UserDao extends JpaRepository<User,Integer>, Serializable {
/**
*
* @param name
* @return
*/
@Query("select u from User u where u.name = ?1")
User getByName(String name);
} }
package com.lsqingfeng.springboot.entity; package com.lsqingfeng.springboot.entity;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.*;
import java.util.Date; import java.util.Date;
/** /**
...@@ -11,8 +14,15 @@ import java.util.Date; ...@@ -11,8 +14,15 @@ import java.util.Date;
* @date: 2022-01-14 16:35 * @date: 2022-01-14 16:35
*/ */
@Data @Data
@Entity
@Table(name = "t_user")
public class User { public class User {
/**
* 主键生成策略: 自增
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; private Integer id;
private String name; private String name;
...@@ -21,8 +31,14 @@ public class User { ...@@ -21,8 +31,14 @@ public class User {
private String address; private String address;
@CreatedDate
@Column(name="create_time")
@Temporal(TemporalType.TIMESTAMP)
private Date createTime; private Date createTime;
@LastModifiedDate
@Column(name="update_time")
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime; private Date updateTime;
} }
...@@ -46,5 +46,12 @@ public interface UserService { ...@@ -46,5 +46,12 @@ public interface UserService {
*/ */
int delete(Integer id); int delete(Integer id);
/**
* 根据名字查询
* @param name
* @return
*/
User getByName(String name);
} }
...@@ -53,4 +53,10 @@ public class UserServiceImpl implements UserService { ...@@ -53,4 +53,10 @@ public class UserServiceImpl implements UserService {
userDao.delete(user); userDao.delete(user);
return id; return id;
} }
@Override
public User getByName(String name) {
return userDao.getByName(name);
}
} }
...@@ -10,15 +10,16 @@ spring: ...@@ -10,15 +10,16 @@ spring:
jpa: jpa:
show-sql: true # 默认false,在日志里显示执行的sql语句 show-sql: true # 默认false,在日志里显示执行的sql语句
database: mysql database: mysql
hibernate.ddl-auto: update #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会 新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建 database-platform: org.hibernate.dialect.MySQL5Dialect
properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect # 使用JPA创建表时,默认使用的存储引擎是MyISAM,通过指定数据库版本,可以使用InnoDB hibernate:
database-platform: org.hibernate.dialect.MySQL5Dialect ddl-auto: update #指定为update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会 新建,如果指定create,则每次启动项目都会清空数据并删除表,再新建
hibernate: naming:
naming: #指定jpa的自动表生成策略,驼峰自动映射为下划线格式7
#指定jpa的自动表生成策略,驼峰自动映射为下划线格式 implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl #physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl devtools:
restart:
enable: true
third: third:
weather: weather:
url: http://www.baidu.com url: http://www.baidu.com
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册