美文网首页
springboot之JPA初识

springboot之JPA初识

作者: adced06edef5 | 来源:发表于2019-04-03 10:31 被阅读0次

    说明:此处JPA之spring data jpa,springboot版本为2.1.3.RELEASE,springdata jpa版本为2.1.5.RELEASE
    1、首先创建一个基本的springboot项目,添加mysql和JPA依赖,具体的pom.xml,如下:

        <dependencies>
            <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>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

    2、添加配置,此处使用application.yml为配置文件,配置数据源及JPA信息如下:

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/jpa?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
        username: root
        password: 1234
      jpa:
        hibernate:
          ddl-auto: update
        show-sql: true
        properties:
          hibernate:
            format_sql: true
    

    此处需要特别注意的是mysql驱动类为com.mysql.cj.jdbc.Driver
    3、创建实体类

    @Entity
    @Table(name="d_demo")
    public class Demo implements Serializable{
    
        private static final long serialVersionUID = 4676210938649473427L;
        
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        
        private String name;
        
        private Boolean flag;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Boolean getFlag() {
            return flag;
        }
    
        public void setFlag(Boolean flag) {
            this.flag = flag;
        }
    
        @Override
        public String toString() {
            return "Demo [id=" + id + ", name=" + name + ", flag=" + flag + "]";
        }
    }
    

    简单说明:@Entity标注为实体,@Table标注表名,@Id主键,@GeneratedValue主键生成策略;如果字段名与实体名不一致,需要使用@Column。
    数据表:


    image.png

    4、创建DAO

    public interface DemoRepository extends JpaRepository<Demo, Integer>{
    
    }
    

    此处不需要使用@Repository
    5、测试
    此处为简单测试不再创建service
    新建

        @Autowired
        private DemoRepository demoRepository;
        
        @Test
        public void testInsert() {
            Demo demo = new Demo();
            demo.setName("AA");
            demo.setFlag(true);
            demo = demoRepository.save(demo);
            System.out.println(demo);
        }
    

    执行结果


    image.png

    查询

        @Test
        public void testSelect() {
            Demo demo = demoRepository.findById(26).get();
            System.out.println(demo);
        }
    

    执行结果


    image.png

    findById返回对象为Optional,Optional为1.8新特性,需要再执行get()返回实体。
    更新

        @Test
        public void testUpdate() {
            Demo demo = demoRepository.findById(26).get();
            demo.setName("AA-New");
            demoRepository.save(demo);
            System.out.println(demo);
        }
    

    执行结果


    image.png

    删除

        @Test
        public void testDelete() {
            demoRepository.deleteById(26);
        }
    

    执行结果


    image.png

    相关文章

      网友评论

          本文标题:springboot之JPA初识

          本文链接:https://www.haomeiwen.com/subject/uvcabqtx.html