0
点赞
收藏
分享

微信扫一扫

springboot jpa


1.创建demo表添加数据,便于测试

CREATE TABLE `demo` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

INSERT INTO demo(NAME) VALUES('tom');
INSERT INTO demo(NAME) VALUES('link');
INSERT INTO demo(NAME) VALUES('jink');
COMMIT;

2.创建Demo实体类

package springboot.data.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Demo {

@Id
private int id;
@Column
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

3.UserRepository接口

package springboot.data.jpa.repository;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import springboot.data.jpa.entity.Demo;

public interface UserRepository extends CrudRepository<Demo, Long> {

//select
@Query("select u from Demo u where u.id= :id")//使用Query查询
Demo findById(@Param("id") int id);
//update
@Transactional
@Modifying
@Query("update Demo set name=?1 where id=?2")//使用Query查询
int updateById(@Param("name") String name,@Param("id") int id);
//delete
@Transactional
@Modifying
@Query("delete from Demo where id=?1")//使用Query查询
int deleteById(@Param("id") int id);

}

4.UserControl类

package springboot.data.jpa.control;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springboot.data.jpa.entity.Demo;
import springboot.data.jpa.repository.UserRepository;

@RestController
@RequestMapping(value = "/user")
public class UserControl {

@Autowired
UserRepository userRepository;


@RequestMapping(value = "/seleteUser" , method = RequestMethod.GET)
public Demo seleteUser(int id){
Demo user=userRepository.findById(id);
return user;
}

@RequestMapping(value = "/updateUser" , method = RequestMethod.GET)
public int seleteUser(int id,String name){
int rs=userRepository.updateById(name, id);
return rs;
}

@RequestMapping(value = "/deleteUser" , method = RequestMethod.GET)
public int deleteUser(int id){
int rs=userRepository.deleteById(id);
return rs;
}

}

5.springboot启动类

package springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {

public static void main(String[] args) {
SpringApplication app=new SpringApplication(SpringbootApplication.class);
app.run(args);

}
}

6.访问方式:

查询:http://localhost:8080/user/selectUser?id=1

修改:http://localhost:8080/user/updateUser?id=1&name=aa


删除:http://localhost:8080/user/deleteUser?id=1



举报

相关推荐

0 条评论