springboot显示信息并且修改_Spring Boot实现简单的增删改查

在pom.xml添加相应的依赖

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.3

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

配置文件配置数据库等

#server

server.port=80

#项目名:server.servlet.context-path

#spring dataSource

spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8&characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=root

mybatis.mapper-locations=classpath:/mapper/*/*.xml

#spring log

logging.level.com.cy=debug

#spring thymeleaf(假如没有配置也会默认配置,在默认配置中prefix默认值为classpath:/templates/,后缀默认为.html)

#不用重启服务器,网页就能刷新

spring.thymeleaf.cache=false

spring.thymeleaf.prefix=classpath:/templates/pages/

spring.thymeleaf.suffix=.html

数据层添加相应注解实现sql语句(或者通过xml配置来实现)

数据层封装了商品信息,并提供get和set方法,为Goods类

1.查询所有数据

@Select("select * from tb_goods")

List findAll();

2.按照id删除数据

@Delete("delete from tb_goods where id=#{id}")

int deleteById(Integer id);

3.修改数据

(1)修改数据首先要新建一个界面,按照id查找内容,并将查找到的内容显示到文本框内

@Select("select * from tb_goods where id=#{id}")

Goods findById(Integer id);

(2)再添加查找的方法

@Update("update tb_goods set name=#{name},remark=# {remark},createdTime=now() where id=#{id}")

int update(Goods goods);

4.新增数据

@Insert("insert into tb_goods(name,remark,createdTime) values (#{name},#{remark},now())")

int add(Goods goods);

业务层提供对应接口方法和实现类

1.业务层接口

public interface GoodsService {

List findObject();

int add(Goods goods);

int update(Goods goods);

Goods findById(Integer id);

}

2.业务层实现类

@Service

public class GoodsServiceImpl implements GoodsService {

@Autowired

private GoodsDao goodsDao;

@Override

public List findObject() {

long start=System.currentTimeMillis();

List list = goodsDao.findObjects();

long end=System.currentTimeMillis();

System.out.println("query time:"+(end-start));

return list;

}

@Override

public int add(Goods goods) {

return goodsDao.add(goods);

}

@Override

public int update(Goods goods) {

return goodsDao.update(goods);

}

@Override

public Goods findById(Integer id) {

return goodsDao.findById(id);

}

控制层写具体实现

1.跳转到首页并且查找所有商品

@RequestMapping("doGoodsUI")

public String doGoodsUI(Model model) {

List list = goodsService.findObject();

model.addAttribute("goods",list);

return "goods";

}

2.业务层实现类

@Service

public class GoodsServiceImpl implements GoodsService {

@Autowired

private GoodsDao goodsDao;

@Override

public List findObject() {

long start=System.currentTimeMillis();

List list = goodsDao.findObjects();

long end=System.currentTimeMillis();

System.out.println("query time:"+(end-start));

return list;

}

@Override

public int add(Goods goods) {

return goodsDao.add(goods);

}

@Override

public int update(Goods goods) {

return goodsDao.update(goods);

}

@Override

public Goods findById(Integer id) {

return goodsDao.findById(id);

}

控制层写具体实现

1.跳转到首页并且查找所有商品

@RequestMapping("doGoodsUI")

public String doGoodsUI(Model model) {

List list = goodsService.findObject();

model.addAttribute("goods",list);

return "goods";

}

2.删除商品

@RequestMapping("doDeleteById/{id}")

// (@PathVariable Integer id)告诉服务器,id拿到的是从网页上同样叫id的数据

public String dodeletebyId(@PathVariable Integer id){

int delete = goodsDao.deleteById(id);

//doGoodsUI前面没有加/的话,跳转的网址是替代了最后一个/后面的内容

return "redirect:/goods/doGoodsUI";

}

3.修改商品

(1)先将查找出来的商品显示在文本框中

@RequestMapping("doFindById/{id}")

public String doFindByID(@PathVariable Integer id,Model model){

Goods goods = goodsService.findById(id);

model.addAttribute("goods",goods);

return "goods-update";

}

(2)实现修改

@RequestMapping("doUpdateGoods")

public String doUpdateGoods(Goods goods){

goodsService.update(goods);

return "redirect:/goods/doGoodsUI";

}

4.新增商品

@RequestMapping("doSaveGoods")

public String doSaveGoods(Goods goods){

goodsService.add(goods);

return "redirect:/goods/doGoodsUI";

}

前端采用html+thymeleaf模板代替jsp

2.each表示遍历拿到的数组,goods是从控制层拿到的model的名字

3.id,name和remark与数据库对应,date要格式化拿到数据,该语法是thymeleaf固定写法

1AAAAAAAaaaa 删除 修改

4.新增商品界面

(1)标签里的name属性要和sql语句一致

(2)这里由于数据库中的id列设置了自增长,所以不需要id属性,createdTime列使用了now()获取当前时间,所以也不需要传值,所以在控制层的doUpdateGoods方法里可以使用封装好的Goods来接收从html拿到的参数

  • name:
  • remark:

5.修改商品界面

(1)因为id列自增长,所以修改商品信息不需要id这一列,但传参数有需要一起传送过去,所以添加了一个输入框,默认设置为隐藏,将其value设置为id的值

  • name:
  • remark:

以上就是Spring Boot实现简单的增删改查的详细内容,更多关于Spring Boot增删改查的资料请关注脚本之家其它相关文章!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值