您的位置:首页 > 编程语言 > Java开发

SpringBoot项目 使用Sprin Data Jpa 实现数据库的 增 ,删, 改, 查操作

2018-04-03 11:20 1396 查看
1:首先建立查询接口类,该接口继承JpaRepository



2:在测试时最好是分三层进行测试,操作数据库放在Service



3:接下来就是具体的操作
/**
* 查询所有
*/
public List<User> queryList() {
List<UserEntity> listUser = userJpa.findAll();
List<User> list = new ArrayList<>( );
Iterator<UserEntity> iterator = listUser.iterator();
while (iterator.hasNext()) {
UserEntity userEntity = iterator.next();
User user = new User( );
BeanUtils.copyProperties( userEntity,user );
list.add( user );
}
return list;
}

/**
* 添加数据
* @return
*/
public String adduser() {
UserEntity userEntity = null;
Random random = new Random( );
for(int i = 0;i<100;++i) {
int aa = random.nextInt(100)+1;
userEntity = new UserEntity( );
userEntity.setName( "sd"+i );
userEntity.setAddress( "dgh"+i );
userEntity.setSex( aa );
UserEntity userEntity1 = userJpa.save( userEntity );
}
String s = "添加成功";
return s;
}

/**
* 删除
* @return
*/
public String deleteUser() {
userJpa.delete( 1L );
return "删除成功";
}

/**
* 更新
* @return
* @throws Exception
*/
public String updateUser() throws Exception{
UserEntity userEntity = userJpa.findOne( 2L );
userEntity.setAddress( "gghddddd" );
userJpa.save( userEntity );
return "成功";
}

4:建立controller层进行测试
package com.example.jpa.controller;
import com.example.jpa.entity.UserEntity;
import com.example.jpa.pojo.User;
import com.example.jpa.response.ResultClient;
import com.example.jpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* com.xinguangnet.tuchao.merchant.manage
*
* @Author : Wukn
* @Date : 2018/2/5
*/
@RestController
public class UserController {

@Autowired
private UserService userService;

/**
* 全查询
* @return
*/
@PostMapping(value = "/user/list")
public ResultClient queryList() {
List<User> list = userService.queryList();
return new ResultClient( list );
}

/**
* 添加
* @return
*/
@PostMapping(value = "/user/add")
public ResultClient addUs() {
String s = userService.adduser();
return new ResultClient( s );
}

/**
* 删除
* @return
*/
@PostMapping(value = "/user/delete")
public ResultClient deleteUser() {
return new ResultClient( userService.deleteUser() );

}

/**
* 更新
* @return
*/
@PostMapping(value = "/user/update")
public ResultClient updateUser() throws Exception{
String s = userService.updateUser();
return new ResultClient( s );
}

/**
* 更新
* @return
*/
@PostMapping(value = "/user/findsex")
public ResultClient findbysex() throws Exception{
List<UserEntity> userEntities = userService.findBySex();
return new ResultClient( userEntities );
}

}

5:用postman测试



:2Query新建语句查询

package com.example.jpa.repotory;
import com.example.jpa.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.lang.reflect.Modifier;
import java.util.List;

/**
* com.xinguangnet.tuchao.merchant.manage
*
* @Author : Wukn
* @Date : 2018/2/5
*/
public interface UserJpa extends JpaRepository<UserEntity,Long>{

@Query(value = "select c from UserEntity c where c.sex = :sex")
public List<UserEntity> findBySex(@Param( "sex" ) int sex);

/**
* 删除
* @param id
*/
@Modifying
@Query(value = "delete from t_user where id = :id",nativeQuery = true)
public void deleteUser(@Param( "id" ) int id);

/**
* 修改
* @param sex
* @param address
* @param id
*/
@Modifying
@Query(value = "update UserEntity c set c.sex = :sex, c.address = :address where c.id = :id" )
public void updateUser(@Param( "sex" ) Integer sex,@Param( "address" ) String address,@Param( "id" ) Long id);

}
1:nativeQuery = true
表示在使用语句查询时使用的是sql语法查询,sql语法面向的是数据库表,在书写语句时,一定要from  表名而不是对象名称



2:不写true默认是false说明语法是JPQL就是查询语言,具有与SQL 相类似的特征,JPQL是完全面向对象的,具备继承、多态和关联等特性,和hibernate HQL很相似。
@Modifying
@Query(value = "update Category c set c.isParent = :isParent where c.categoryId = :categoryId")
int updateIsParent(@Param("categoryId") Long categoryId,@Param("isParent") boolean isParent);

@Query("select c from Category c where c.parentId=:parentId and c.delFlag=:delFlag")
List<Category> findByParentIdAndDelFlagAndSort(@Param("parentId")Long parentId,@Param("delFlag")boolean delFlag, Sort sort);

@Modifying
@Query(value = "update Category c set c.delFlag=:delFlag where c.categoryId = :categoryId and c.platformId = :platformId")
int updateCategoryById(@Param("categoryId") Long categoryId,@Param("platformId") Integer platformId,
@Param("delFlag") boolean delFlag);

Category findByCategoryNameAndDelFlag(String categoryName,boolean delFlag);

Category findByCategoryIdAndDelFlag(Long categoryId, boolean delFlag);

@Query("select c from Category c where c.platformId=:platformId and c.parentId=:parentId and c.delFlag=:delFlag")
List<Category> findByPlatformIdAndSort(@Param("platformId") Integer platformId,
@Param("parentId") long parentId,@Param("delFlag") boolean delFlag,Sort sort);
}
3:在进行插入或者删除时需要事务处理
这是在Service



这是在Controller



也可以直接在语句出添加(个人不建议这么干)



4:@Modifying可以通过自定义的 JPQL 完成 UPDATE 和 DELETE 操作。 注意: JPQL 不支持使用 INSERT; 
在 @Query 注解中编写 JPQL 语句, 但必须使用 @Modifying 进行修饰. 以通知 SpringData, 这是一个 UPDATE 或 DELETE 操作 
UPDATE 或 DELETE 操作需要使用事务,此时需要定义 Service 层,在 Service 层的方法上添加事务操作; )默认情况下, SpringData 的每个方法上有事务, 但都是一个只读事务。 他们不能完成修改操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: