springboot之简单jap整合

  1. 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);
            }
        
        }
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值