14、通过jpa往数据库插数据
这是接着上一篇写的,在上一篇的基础上添加
Controller
@RestController public class HelloController { @Resource private HelloService helloService; @RequestMapping("/hello") public String hello(){ return "hello"; } @RequestMapping("/saveUser") public String user(){ User u = new User(); u.setPassword("1"); u.setUser_name("123"); u.setAge(1); helloService.saveUser(u); return "success"; } }
Service
import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Created by Administrator on 2017/5/27 0027. */ @Service public class HelloService { @Resource private HelloDao helloDao; public void saveUser(User u) { helloDao.save(u); } }
Dao(接口,实现了CrudRepository<User,Long>)
public interface HelloDao extends CrudRepository<User,Long>{ }
然后运行项目,输入:http://localhost:8080/saveUser
表中会多了
一条记录