SpringBoot+Mybatis增删改查实战 (2)

路径:/service/PersonService.java

package com.ljsh.test.service; import com.ljsh.test.dao.PersonDao; import com.ljsh.test.model.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class PersonService { @Autowired PersonDao personDao; /* Service层介于controller和dao之间作为服务层进行一些逻辑处理, 这里逻辑太简单所以知识单纯调用dao所以不做注释 */ public List<Person> getAll(){ return personDao.getAll(); } public Person getPersonByID(int id){ return personDao.getPersonByID(id); } public void delete(int id){ personDao.delete(id); } public void update(Person p){ personDao.update(p); } public void newp(Person p){ personDao.newp(p); } } PersonController

路径:/controller/PersonController.java

package com.ljsh.test.controller; import com.ljsh.test.model.Person; import com.ljsh.test.service.PersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class PersonController { @Autowired PersonService personService; // 设置访问路由值为路径 @RequestMapping("http://www.likecs.com/") public ModelAndView index(){ // 顾名思义 实体和数据 同时返回页面模板和数据 ModelAndView mav = new ModelAndView("index"); List<Person> list = personService.getAll(); mav.addObject("list",list); return mav; } } 前端页面

路径:/templates/index.html

<!DOCTYPE html> <html lang="en"> <!-- --> <!-- 使用thymeleaf需引入 --> <html xmlns:th="http://www.thymeleaf.org"> <head> </head> <body> <div> <table> <caption>人员信息</caption> <tr> <th>Name</th> <th>Phone</th> </tr> <!-- 通过th命令使用一些操作 --> <!-- 通过${} 使用变量 --> <tr th:each="item: ${list}"> <td th:text="${{item.name}}">还没有任何人员信息哦</td> <td th:text="${{item.mobile}}">你是不是想独吞奖品</td> </tr> </table> </div> </div> </body> </html> 右上角运行

SpringBoot+Mybatis增删改查实战


要是没有这个可以右侧选择TestApplication右键Run,结果图如下
[结果图]

未完待续

熄灯睡觉了,写的有点慢,删改查还没来及写,如果需求留言,我会继续更新。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydzfg.html