当前位置: 代码迷 >> 综合 >> springboot整合mybatis,druid,thymeleaf,简洁易懂
  详细解决方案

springboot整合mybatis,druid,thymeleaf,简洁易懂

热度:74   发布时间:2023-11-21 12:32:08.0

话不多说,效果如下

简单用了Semantic UI

mapper.xml:sql语句如下,没有太复杂,是简单入门

<mapper namespace="nuc.edu.springboot01.dao.UserDao"><select id="findAll" parameterType="nuc.edu.springboot01.pojo.Query" resultType="nuc.edu.springboot01.pojo.Book">select *from book.book</select><select id="findById" parameterType="integer" resultType="nuc.edu.springboot01.pojo.Book">select *from book.book where bookid =#{bookid}</select><select id="findByIdName" resultType="nuc.edu.springboot01.pojo.Book">select *from book.book where bookid =#{param1} and bookname= #{param2}</select><insert id="insertBook" parameterType="nuc.edu.springboot01.pojo.Book">Insertinto book.book(bookid,bookname,bookprice) values (#{bookid},#{bookname},#{bookprice});</insert><delete id="deleteById" parameterType="int">delete from  book.book where bookid = #{bookid}</delete>
</mapper>

表现层

@Controller
@Repository
public class UserController {@Autowiredprivate Services services;@GetMapping("/")public String finaAll(Model model,Query query){PageInfo<Book>userPageInfo = services.findAll(query);model.addAttribute("page",userPageInfo);return "index";}@PostMapping("/findById")public String findById(Model model,Integer bookid){Book book1 = services.findById(bookid);model.addAttribute("book1", book1);return "indexId";}@PostMapping("/findByIdName")public String findByIdName(Model model,Integer bookid,String bookname){Book book2 = services.findByIdName(bookid,bookname);model.addAttribute("book2", book2);return "indexIdName";}@PostMapping("/insert")public String insetUser(RedirectAttributes attributes, Book book){boolean a = services.insertUser(book);if(a) {attributes.addFlashAttribute("message","添加图书成功");return "redirect:/";}else {attributes.addFlashAttribute("message","添加图书失败");return "redirect:/";}}@GetMapping("/insert1")public String toUpdate(Model model){Book book = new Book();model.addAttribute("book",book);return "insertbook";}@GetMapping("/delete/{bookid}")public String deleteById(@PathVariable("bookid") Integer bookid, RedirectAttributes attributes){boolean a =services.deleteById(bookid);if(a){attributes.addFlashAttribute("message","删除图书成功!");return "redirect:/";}else {attributes.addFlashAttribute("message","删除图书成功!");return "redirect:/";}}
}

前端运用了thymeleaf模板,采用了Semantic UI

例:

<tr th:each="book:${page.list}"><td th:text="${book.bookid}"></td><td th:text="${book.bookname}"></td><td th:text="${book.bookprice}"></td><td><a class="ui button mini blue" th:href="@{/delete/{bookid}(bookid=${book.bookid})}">删除</a><a class="ui button mini blue">编辑</a></td></tr>

有问题欢迎私信,一起交流

 源码:springboot-01.rar-Java文档类资源-CSDN下载

  相关解决方案