同步操作将从 yswift/LearnJava 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package db.springdata;import db.model.College;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.stereotype.Component;import java.util.List;import java.util.Optional;@Componentpublic class TestSpringDataCollege {@AutowiredCollegeRepository repository;void list() {List<College> cs = repository.findAll();for (College c : cs) {System.out.println(c);}}void insert() {College[] cs = {new College("01", "计科"), new College("02", "通信"), new College("03", "电器"),};for (College c : cs) {repository.save(c);}}public void update(College c) {repository.save(c);}public void update2() {// 修改名称:计算机科学与技术System.out.println("计算机科学与技术");Optional<College> oc = repository.findById("01");College c = oc.get();c.setName("计算机科学与技术");repository.save(c);}public void delete(String id) {repository.deleteById(id);}void deleteAll() {repository.deleteAll();}void findById(String id) {System.out.println("find by id: " + id);Optional<College> c = repository.findById(id);System.out.println(c.get());}void findByName(String name) {System.out.println("find by name: " + name);College c = repository.findByName(name);System.out.println(c);}public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig4H2.class);TestSpringDataCollege t = ctx.getBean(TestSpringDataCollege.class);t.deleteAll();System.out.println("插入3个部门");t.insert();t.list();System.out.println("\n修改电器名称");College c = new College("03", "电气");t.update(c);t.list();System.out.println("\n删除机械");t.delete("02");t.list();System.out.println("查找计科");t.findByName("计科");System.out.println("查找机械");t.findByName("机械");t.update2();t.list();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。