SpringBoot练习

一、SpringBoot入门

1.首先创建一个Maven项目。SpringBoot就是一个简单的Maven项目

2.新建一个pom.xml,这个pom.xml就指定了当前项目需要用到的jar包。

3.创建 Application.java,其注解 @SpringBootApplication 表示这是一个SpringBoot应用,运行其主方法就会启动tomcat,默认端口是8080。

4.接着创建控制器类HelloController, 这个类就是Spring MVC里的一个普通的控制器。
@RestController 是spring4里的新注解,是@ResponseBody和@Controller的缩写。

 

 5.运行Application.java, 然后访问地址 http://127.0.0.1:8080/hello

运行结果如下

 

 二、SpringBoot Mybatis

1.首先,创建一个数据库

 2.修改pom.xml,增加对mysql和mybatis的支持。

3.增加一个包:com.how2java.springboot.pojo,然后创建实体类Category。

4.增加一个包:com.how2java.springboot.mapper,然后创建接口CategoryMapper。
使用注解@Mapper 表示这是一个Mybatis Mapper接口。
使用@Select注解表示调用findAll方法会去执行对应的sql语句。

5.增加一个包:com.how2java.springboot.web,然后创建CategoryController 类。其作用是:
1. 接受listCategory映射
2. 然后获取所有的分类数据
3. 接着放入Model中
4. 跳转到listCategory.jsp中 

6.新建一个jsp,用jstl遍历从CategoryController 传递过来的集合:cs.

运行结果如下:

 三、SpringBoot Mybatis-xml方式

1.修改CategoryMapper,去掉了sql 语句的注解。

2.在Mapper类旁边,新增加Category.xml文件,里面就是放的这个sql语句。

 3.修改application.properties, 指明从哪里去找xml配置文件,同时指定别名。

 最后运行,结果如下:

 四、Springboot中运用Mybatis增删改查CRUD和分页 功能

1.修改pom.xml,增加对PageHelper的支持。

2.新增加一个包 com.how2java.springboot.config, 然后添加一个类PageHelperConfig ,其中进行PageHelper相关配置。

offsetAsPageNum:设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用.

3.修改CategoryMapper,增加CRUD方法的支持。 其实就是调用不同的SQL语句。

4.为CategoryController添加: 增加、删除、获取、修改映射

增加操作:

@RequestMapping("/addCategory")

public String listCategory(Category c) throws Exception {

categoryMapper.save(c);

return "redirect:listCategory";

}

删除操作:

@RequestMapping("/deleteCategory")

public String deleteCategory(Category c) throws Exception {

categoryMapper.delete(c.getId());

return "redirect:listCategory";

}

获取操作:

@RequestMapping("/updateCategory")

public String updateCategory(Category c) throws Exception {

categoryMapper.update(c);

return "redirect:listCategory";

}

修改映射:

@RequestMapping("/editCategory")

public String listCategory(int id,Model m) throws Exception {

Category c= categoryMapper.get(id);

m.addAttribute("c", c);

return "editCategory";

}

5.修改listCategory.jsp,通过page.getList遍历当前页面的Category对象。
在分页的时候通过page.pageNum获取当前页面,page.pages获取总页面数。

6.修改editCategory.jsp,修改分类的页面。

 最后运行,结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值