SpringBoot+Jpa整合

一.创建SpringBoot项目 导入依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.17</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

二.配置SpringBoot的配置文件

#Mysql数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=UTC&characterEncoding=utf-8 
spring.datasource.username=root
spring.datasource.password=root

#JPA相关配置
#项目启动生成数据库
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

三.创建实体类

@Entity
@Table(name = "user")
@Data
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "username")
    private String username;
    @Column(name = "pwd")
    private String pwd;
}

四.创建repository

@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    /**
     * 条件查询
     * @param username   用户名
     * @param pwd   密码
     * @return
     * @throws Exception
     */
    User findByUsernameAndPwd(String username, String pwd) throws Exception;


    /**
     * HQL 查询,说白了就是根据实体进行查询
     * @param username 姓名
     * @param pwd 密码
     * @return 单个用户或者null
     */
    @Query(value = "select u from User u where username =?1 and pwd=?2")
    User findQueryHql(String username,String pwd);

    /**
     * nativeQuery =true  原生sql语句进行查询   将query注解后的sql语句放入数据库可直接使用
     * @param username   用户名
     * @param pwd         密码
     * @return
     */
    @Query(value = "select * from user  where username =?1 and pwd=?2",nativeQuery =true)
    User findByQuery(String username, String pwd);

    /**
     * 复合查询
     * @param spec   拼接的条件语句
     * @param pageable   分页加排序 Pageable已封完毕
     * @return  page  分页的用户列表
     */
    Page<User> findAll(Specification<User> spec, Pageable pageable);

}

五.创建service

@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    /**
     * 条件查询
     * @param username   用户名
     * @param pwd   密码
     * @return
     * @throws Exception
     */
    User findByUsernameAndPwd(String username, String pwd) throws Exception;


    /**
     * HQL 查询,说白了就是根据实体进行查询
     * @param username 姓名
     * @param pwd 密码
     * @return 单个用户或者null
     */
    @Query(value = "select u from User u where username =?1 and pwd=?2")
    User findQueryHql(String username,String pwd);

    /**
     * nativeQuery =true  原生sql语句进行查询   将query注解后的sql语句放入数据库可直接使用
     * @param username   用户名
     * @param pwd         密码
     * @return
     */
    @Query(value = "select * from user  where username =?1 and pwd=?2",nativeQuery =true)
    User findByQuery(String username, String pwd);

    /**
     * 复合查询
     * @param spec   拼接的条件语句
     * @param pageable   分页加排序 Pageable已封完毕
     * @return  page  分页的用户列表
     */
    Page<User> findAll(Specification<User> spec, Pageable pageable);

}

六.创建service 实现类

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public User findByUsernameAndPwd(String username, String pwd) throws Exception {
        return userRepository.findByUsernameAndPwd(username, pwd);
    }

    @Override
    public User findQueryHql(String username, String pwd) {
        return userRepository.findQueryHql(username, pwd);
    }

    @Override
    public Page<User> findAll(int page, int size, String all) {
        Pageable pageable =new PageRequest(page,size,new Sort(Sort.Direction.ASC,"username"));
        return userRepository.findAll((Specification<User>) (root, query, cb) -> {
            Predicate p1 = cb.like(root.get("username").as(String.class),"%"+all+"%");
            return cb.and(p1);
        },pageable);
    }

    @Override
    public List<User> queryAll() {
        return userRepository.findAll();
    }
}

七.Controller层

public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/findByNameAndPassword")
    public User findByNameAndPassword(String name,String password) throws Exception {
        return userService.findByUsernameAndPwd(name,password);
    }
    @GetMapping("/user/findQueryHql")
    public User findQueryHql(String name,String password){
        return userService.findQueryHql(name,password);
    }
    /**
     *
     * @param page 由于后端page是从0开始的,但是前端传过来的是从1 开始的  所以要减一
     * @param size
     * @param all
     * @return
     */
    @GetMapping("/user/findAll")
    public Page<User> findAll(int page, int size, String all){
        page =page -1;
        return userService.findAll(page,size,all);
    }
    /**
     * 调用jpa封装好的 查询所有方法
     * @return
     */
    @GetMapping("/user/all")
    public List<User> findJpaAll(){
        return userService.queryAll();
    }
}

8.基本单表的crud 都是jparepository封装好了,,调用他的方法,直接使用就好

发布了31 篇原创文章 · 获赞 23 · 访问量 3805

猜你喜欢

转载自blog.csdn.net/leilei1366615/article/details/100584646