Spring Boot整合Spirng-data-jpa及使用

Spring-data-jpa可根据实体类自动创建表结构,提供基本的增删改查方法,数据访问层非常简洁,只是一层接口。

1.pom引进依赖

<!--dataSource-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

2.properties配置

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456..
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#通过jpa自动建表 第一次create 后面用update
spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.show-sql=true

3.创建实体

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private Integer age;
    ......省略   注解来自javax.persistence
}

4.编写接口

//默认提供增删改查
public interface UserRepository  extends JpaRepository<User,Long> {

	//jpa自动根据方法名称转换查询语句
    User findByName(String name);
	//参数要使用@Param
    @Query("from User where name=:name and age=:age")
    User findByNameAndAgea(@Param("name") String name, @Param("age") Integer age);
}

5.实现分页查询

@Repository
public interface GirlReponsitory extends JpaRepository<Girl, Integer> {

    Page<Girl> findAll(Pageable pageable);
}

@RequestMapping("/index/{page}")
    public String index(@PathVariable int page, Model model) {

        model.addAttribute("model", "Hello,world");
        int size = 1;
        Pageable pageable = new PageRequest(page,size);
        Page<Girl> girls = girlReponsitory.findAll(pageable);
        //getContent得到List
        model.addAttribute("pages",girls.getContent());	
        return "index";
    }


前端再使用freemarker显示出来

6.自定义的JPQL语句

对于修改和删除根据需要增加注释

@Transactional
    @Modifying
    @Query("update Girl set age=:age where id=:id")
    void updateById(@Param("age") Integer age, @Param("id") Integer id);

    @Transactional
    @Modifying
    @Query("delete from Girl where id=:id")
    void deleteGirl(@Param("id") Integer id);

7.关联表查询

一对一映射

两个实体
@Entity
public class UserInfo implements Serializable {
    @Id
    @GeneratedValue
    private Long userId;
    private String name;
    private Integer age;
    private String email;

    //与 Address 关联
    private Long addressId;
    
    //省略 get set constructor
}

@Entity
public class Address {
    @Id
    @GeneratedValue
    private Long addressId;
    private String country;
    private String province;
    private String city;
    ....
    }
   重点 新建一个结果集类
   public class ViewInfo {
    private UserInfo userInfo;
    private Address address;

    public ViewInfo(UserInfo userInfo){
        this.userInfo = userInfo;
        this.address = new Address();
    }

    public ViewInfo(Address address){
        this.userInfo = new UserInfo();
        this.address = address;
    }

    public ViewInfo(UserInfo userInfo, Address address) {
        this.userInfo = userInfo;
        this.address = address;
    }
    //省略 get set
}
dao接口
@Repository
public interface UserInfoRepository extends JpaRepository<UserInfo,Long> {
    //需要用完整类名构造函数,结果集将映射进ViewInfo
    @Query(value = "select new com.ay.demoboot.model.ViewInfo(u,a) from UserInfo u,Address a where u.addressId = a.addressId")
    List<ViewInfo> findViewInfo();

}
另一个接口省略

多对多映射
参考多表查询

猜你喜欢

转载自blog.csdn.net/weixin_41768073/article/details/83420726