pom.xml里的依赖
<!-- springboot web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot tomcat 支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- 测试支持 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <!-- thymeleaf 的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- thymeleaf legacyhtml5 模式支持 --> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> <!-- 热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> </dependency>
resources/配置文件application.properties
#database spring.datasource.url=jdbc:mysql://localhost:3306/tmall_ssm?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.jdbc.Driver #thymeleaf spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false spring.thymeleaf.servlet.content-type=text/html server.servlet.context-path=/thymeleaf #访问路径是http://127.0.0.1:8080/thymeleaf/xxx
com.java/启动类Application.java
@SpringBootApplication @EnableCaching//redis缓存 public class Application { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(Application.class, args); } }
com.java.config/分页配置PageHelperConfig类
@Configuration public class PageHelperConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } }
com.java.exception/异常处理GlobalExceptionHandler类
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName("errorPage"); return mav; } }
com.java.pojo/实体类xx类
getter\setter\toString
com.java.mapper/持久层Mapper类
@Mapper//表示这是一个Mybatis Mapper接口 public interface CategoryMapper { @Select("select * from category")//调用findAll方***去执行对应的sql语句 List<Category> findAll(); @Insert("insert into category(name) values(#{name})") public int save(Category category); @Delete("delete from category where id=#{id}") public void delete(int id); @Select("select * from category where id=#{id}") public Category get(int id); @Update("update category set name=#{name} where id=#{id} ") public int update(Category category); }
com.java.service/业务层Service接口
public interface CategoryService { int add(Category hero); void delete(int id); Category get(int id); int update(Category hero); List<Category> list(); }
com.java.service.impl/业务层ServiceImpl类
@Service public class CategoryServiceImpl implements CategoryService{ @Autowired CategoryMapper categoryMapper; @Override public int add(Category hero) { // TODO Auto-generated method stub categoryMapper.save(hero); return 0; } @Override public void delete(int id) { // TODO Auto-generated method stub categoryMapper.delete(id); } @Override public Category get(int id) { // TODO Auto-generated method stub return categoryMapper.get(id); } @Override public int update(Category hero) { // TODO Auto-generated method stub return categoryMapper.update(hero); } @Override public List<Category> list() { // TODO Auto-generated method stub return categoryMapper.findAll(); } }
com.java.controller/控制层Controller类
@RestController public class CategoryController { @Autowired CategoryService categoryService; /*restful 部分*/ @GetMapping("/category") public PageInfo<Category> listCategory(@RequestParam(value = "start", defaultValue = "1") int start,@RequestParam(value = "size", defaultValue = "5") int size){ PageHelper.startPage(start,size,"id desc"); List<Category> cs=categoryService.list(); PageInfo<Category> page = new PageInfo<>(cs,5); //5表示一个导航分页最多有5个 return page; } @PostMapping("/category") public String addCategory(Category c){ categoryService.add(c); return "success"; } @DeleteMapping("/category/{id}") public String delete(Category c){ categoryService.delete(c.getId()); return "success"; } @PutMapping("/category/{id}") public String update(Category c){ categoryService.update(c); return "success"; } @GetMapping("/category/{id}") public Category edit(@PathVariable("id") int id){ Category c=categoryService.get(id); return c; } /*页面跳转 部分*/ @RequestMapping(value="/listCategory", method=RequestMethod.GET) public ModelAndView listHero(){ ModelAndView mv = new ModelAndView("listCategory"); return mv; } @RequestMapping(value="/editCategory", method=RequestMethod.GET) public ModelAndView editHero(){ ModelAndView mv = new ModelAndView("editCategory"); return mv; } }