-
JPA整合
-
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.5.2</version> </dependency>
-
使用
-
配置
jpa: hibernate: ddl-auto: update naming: #关闭自动驼峰转换 physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl show-sql: true
-
实体类
package com.example.hand.springbootweb.domain.entity; import javax.persistence.*; @Entity @Table(name = "login_user") public class LoginJPA { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "username") private String userName; @Column(name = "password") private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "LoginVo{" + "userName='" + userName + '\'' + ", password='" + password + '\'' + '}'; } }
-
“mapper”接口
@Repository //标志这是一个操作数据库的类 public interface LoginUser extends JpaRepository<LoginJPA,Integer> { //原生sql语句,使用nativeQuery =true // @Query("from LoginJPA where userName= ?1 and password= ?2") // LoginJPA selectLoginUser(String username,String password); @Query("from LoginJPA where userName= :username and password= :password") LoginJPA selectLoginUser(@Param("username") String username,@Param("password") String password); @Query("from LoginJPA") List<LoginJPA> selectAll(); }
-
写Service类调用
-
扫描
package com.example.hand.springbootweb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @EnableJpaRepositories(basePackages ="com.example.hand.springbootweb.app.service" ) @EntityScan(basePackages ="com.example.hand.springbootweb.domain.entity" ) //@ComponentScan(basePackages = "com.example.hand.springbootweb.app.service") 如果添加,就扫不到其他组件了 controller404 @SpringBootApplication public class SpringbootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootWebApplication.class, args); } }
-
-
springboot之简单jap整合
最新推荐文章于 2023-07-07 09:54:22 发布