十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1、windows 7 企业版
10年积累的成都网站制作、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有吉安免费网站建设让你可以放心的选择与我们合作。
2、IDEA 14
3、JDK 1.8
4、Maven 3.5.2
5、MariaDB
6、SQLYog
Maven目录下的conf目录下的settings.xml做如下内容的添加:
1、使用阿里云的仓库,比官网访问速度快很多
nexus-aliyun central Nexus aliyun http://maven.aliyun.com/nexus/content/groups/public
2、全局JDK配置
jdk18 true 1.8 1.8 1.8 1.8
1、Maven设置:选择Maven目录,同时配置文件和本地仓库
2、字符编码设置
选择Enable Auto-Import,创建好的工程目录如下图:
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0 cn.temptation studySpringBoot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-starter-data-jpa
2、resources目录下新建application.properties(当然喜欢用yaml的可以用yaml)
# 数据库连接 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password=sa spring.datasource.driver-class-name=com.mysql.jdbc.Driver # JPA配置 spring.jpa.properties.hibernate.hbm2ddl.auto=update
3、创建SpringBoot程序启动类SpringbootApplication.java
package cn.temptation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { // SpringBoot项目启动 SpringApplication.run(SpringbootApplication.class, args); } }
4、创建实体类Category.java
package cn.temptation.model; import javax.persistence.*; // 建库建表 //DROP TABLE category; // //CREATE TABLE category //( // categoryid INT AUTO_INCREMENT PRIMARY KEY, // categoryname VARCHAR(10) NOT NULL //); // //INSERT INTO category VALUES(NULL, '手机'), (NULL, '图书'), (NULL, '服装'), (NULL, '鞋帽'); // //SELECT * FROM category; @Entity @Table(name = "category") public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "categoryid") private Integer categoryid; @Column(name = "categoryname") private String categoryname; public Integer getCategoryid() { return categoryid; } public void setCategoryid(Integer categoryid) { this.categoryid = categoryid; } public String getCategoryname() { return categoryname; } public void setCategoryname(String categoryname) { this.categoryname = categoryname; } }
5、创建DAO接口CategoryDao.java
package cn.temptation.dao; import cn.temptation.model.Category; import org.springframework.data.jpa.repository.JpaRepository; public interface CategoryDao extends JpaRepository{ }
6、创建控制器类CategoryController.java
package cn.temptation.web; import cn.temptation.dao.CategoryDao; import cn.temptation.model.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class CategoryController { @Autowired private CategoryDao categoryDao; /** * 不分页查询 * * @return */ // @RequestMapping("/categorylist") // public ModelAndView categorylist() { // Listlist = categoryDao.findAll(); // // ModelAndView mav = new ModelAndView("categorylist"); // mav.addObject("list", list); // return mav; // } /** * 分页查询 * * @return */ @RequestMapping("/categorylist") public ModelAndView categorylist(@RequestParam(value = "start", defaultValue = "0") Integer start, @RequestParam(value = "limit", defaultValue = "2") Integer limit) { start = start < 0 ? 0 : start; Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "categoryid"); Pageable pageable = new PageRequest(start, limit, sort); Page page = categoryDao.findAll(pageable); // System.out.println(page.getNumber()); // System.out.println(page.getNumberOfElements()); // System.out.println(page.getSize()); // System.out.println(page.getTotalElements()); // System.out.println(page.getTotalPages()); // System.out.println(page.isFirst()); // System.out.println(page.isLast()); ModelAndView mav = new ModelAndView("categorylist"); mav.addObject("page", page); return mav; } /** * 类别新增视图 * @return */ @RequestMapping("/categoryinit") public String categoryinit() { return "categoryinit"; } /** * 类别新增操作 * @param model * @return */ @RequestMapping("/categoryinsert") public String categoryinsert(Category model) { categoryDao.save(model); return "redirect:categorylist"; } /** * 类别删除操作 * @param categoryid * @return */ @RequestMapping("/categorydelete") public String categorydelete(Integer categoryid) { categoryDao.deleteById(categoryid); return "redirect:categorylist"; } /** * 类别编辑视图 * @param categoryid * @return */ @RequestMapping("/categoryedit") public ModelAndView categoryedit(Integer categoryid) { Category model = categoryDao.getOne(categoryid); ModelAndView mav = new ModelAndView("categoryedit"); mav.addObject("category", model); return mav; } /** * 类别编辑操作 * @param model * @return */ @RequestMapping("/categoryupdate") public String categoryupdate(Category model) { categoryDao.save(model); return "redirect:categorylist"; } }
7、resources目录下新建templates目录,创建表现层:类别列表页面(categorylist.html)、类别新增页面(categoryinit.html)、类别编辑页面(categoryedit.html)
类别列表页面(categorylist.html)
类别新增页面(categoryinit.html)
类别新增
类别编辑页面(categoryedit.html)
类别编辑
总结
以上所述是小编给大家介绍的IDEA+maven+SpringBoot+JPA+Thymeleaf实现Crud及分页,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!