SpringBoot 作业

在eclipse中创建运行的起来的maven项目

(1)先运行,看到效果。

导入maven项目比较慢,导入的时候最好不要到处乱点,很容易导致eclipse出现不明原因卡顿,要等底下的进度条消失才能运行

运行结果:

 (2)创建项目并运行

改变pom.xml

 

创建Application.java

 

@SpringbootApplication表示这是一个SpringBoot应用,运行其主方法就会启动tomcat,默认端口是8080

创建HelloController.java

 (4)运行测试

SPRINGBOOT系列教材 (十四)- 持久层支持 - SPRINGBOOT中如何运用MYBATIS 简单例子

(1)准备数据

(2)先运行,看到效果

 

 

(3)Category类

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

常规set和get方法

 

(4)CategoryMapper

增加一个包:com.how2java.springboot.mapper,然后创建接口CategoryMapper。

运用mapper注解,mybatis框架,帮助sql语句的调用

使用注解@Mapper 表示这是一个Mybatis Mapper接口。

使用@Select注解表示调用findAll方法会去执行对应的sql语句

 @Insert(" insert into category_ ( name ) values (#{name}) ") 
    public int add(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);  
        
    @Select(" select * from category_ ") 
    public List<Category> list(); 

(5)CategoryController

增加一个包:com.how2java.springboot.web,然后创建CategoryController 类

控制各个页面的跳转以及不同包之间方法的调用

1. 接受listCategory映射

2. 然后获取所有的分类数据

3. 接着放入Model中

4. 跳转到listCategory.jsp中

@RequestMapping("/listCategory")
    public String listCategory(Model m) throws Exception {
        List<Category> cs=categoryMapper.findAll();
          
        m.addAttribute("cs", cs);
          
        return "listCategory";
    }

 

(6)listCategory.jsp

用jstl遍历从CategoryController 传递过来的集合:cs.

 (7)重启测试

 

SPRINGBOOT系列教材 (十五)- 持久层支持 - MYBATIS-XML方式

(1)去掉了CategoryMapper的注释

 (2)在CategoryMapper.java旁边新增Category.xml文件存放sql语句

 

(3)application.properties

指明去哪找配置文件

mybatis.mapper-locations=classpath:com/how2java/springboot/mapper/*.xml
mybatis.type-aliases-package=com.how2java.springboot.pojo

 (4)运行

 

SPRINGBOOT系列教材 (十七)- CRUD+分页 - SPRINGBOOT使用MYBATIS实现完整的 增删改查 CRUD和分页

(1)增加对PageHelper的支持

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>4.1.6</version>
</dependency>

(2)PageHelperConfig

分页技术

原理:

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

 p.setProperty("offsetAsPageNum", "true");

rowBoundsWithCount:设置为true时,使用RowBounds分页会进行count查询.

p.setProperty("rowBoundsWithCount", "true");

reasonable:启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页。

p.setProperty("reasonable", "true");

 

注解@Configuration 表示PageHelperConfig 这个类是用来做配置的。

注解@Bean 表示启动PageHelper这个拦截器。

(4)CategoryMapper

增加CRUD方法的支持

(4)CategoryController

@RequestMapping("/addCategory")
public String listCategory(Category c) throws Exception {
	categoryMapper.save(c);
	return "redirect:listCategory";
}//add映射
@RequestMapping("/deleteCategory")
public String deleteCategory(Category c) throws Exception {
	categoryMapper.delete(c.getId());
	return "redirect:listCategory";
}//delete映射
@RequestMapping("/updateCategory")
public String updateCategory(Category c) throws Exception {
	categoryMapper.update(c);
	return "redirect:listCategory";
}//update映射
@RequestMapping("/editCategory")
public String listCategory(int id,Model m) throws Exception {
	Category c= categoryMapper.get(id);
	m.addAttribute("c", c);
	return "editCategory";
}//edit映射,用于跳转到editCategory页面对数据进行update操作
 

 修改findAll映射

@RequestMapping("/listCategory")
public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
    PageHelper.startPage(start,size,"id desc");
    List<Category> cs=categoryMapper.findAll();
    PageInfo<Category> page = new PageInfo<>(cs);
    m.addAttribute("page", page);         
    return "listCategory";
}

1. 在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5。

@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5"

2. 根据start,size进行分页,并且设置id 倒排序

PageHelper.startPage(start,size,"id desc");

3. 因为PageHelper的作用,这里就会返回当前分页的集合了

List<Category> cs=categoryMapper.findAll();

4. 根据返回的集合,创建PageInfo对象

PageInfo<Category> page = new PageInfo<>(cs);

5. 把PageInfo对象扔进model,以供后续显示

m.addAttribute("page", page);      

6. 跳转到listCategory.jsp

return "listCategory";

(5)listCategory.jsp

通过page.getList遍历当前页面的Category对象。

在分页的时候通过page.pageNum获取当前页面,page.pages获取总页面数。

注:page.getList会返回一个泛型是Category的集合。

(6)editCategory.jsp

用于进行获取单个元素并进行更新操作

get 和update

(7)运行结果

 

 

改变desc为asc,跳转页面从start=2改为start=0

对Product表进行CRUD增删改查的操作

(1)准备数据

(2)application.properties

连接数据库

(3)pom.xml

增加对mysql和mybatis的支持

(4)Product

创建实体类Product

 

(5)ProductMapper

Mybatis mapper接口,mybatis的注解方法,帮助调用存在mapper里的sql语句

(6)CategoryController

 

(7)listProduct.jsp

 (8)editProduct.jsp

 (9)运行

 

改变呈现顺序

http://127.0.0.1:8080/listProduct?start=0

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值