关于springboot:Spring-Boot简单的增删改查

6次阅读

共计 5151 个字符,预计需要花费 13 分钟才能阅读完成。

在 pom.xml 增加相应的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 前端应用 thymeleaf 来代替 jsp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

配置文件配置数据库等

#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<Goods> 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<Goods> 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<Goods> findObject() {long start=System.currentTimeMillis();
        List<Goods> 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<Goods> 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

1.thymeleaf 的语法参考:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
2.each 示意遍历拿到的数组,goods 是从管制层拿到的 model 的名字
3.id,name 和 remark 与数据库对应,date 要格式化拿到数据,该语法是 thymeleaf 固定写法

<tr th:each="g:${goods}">
        <td th:text="${g.id}">1</td>
        <td th:text="${g.name}">AAAAAAA</td>
        <td th:text="${g.remark}">aa</td>
        <td th:text="${#dates.format(g.createdTime,'yyyy-MM-dd HH:mm')}">aa</td>
<!--        <td><a href="#" th:href="@{/goods/doDeleteById(id=${g.id})}"><button> 删除 </button></a></td>-->
        <td><a href="#" th:href="@{/goods/doDeleteById/{doDeleteById}(doDeleteById=${g.id})}"><button> 删除 </button></a></td>
        <td><a href="#" th:href="@{/goods/doFindById/{id}(id=${g.id})}"><button> 批改 </button></a></td>
</tr>

4. 新增商品界面
(1)标签里的 name 属性要和 sql 语句统一
(2)这里因为数据库中的 id 列设置了自增长,所以不须要 id 属性,createdTime 列应用了 now() 获取以后工夫,所以也不须要传值,所以在管制层的 doUpdateGoods 办法里能够应用封装好的 Goods 来接管从 html 拿到的参数

<form th:action="@{/goods/doSaveGoods}" method="post">
    <ul>
        <li>name:<input type="text" name="name"></li>
        <li>remark:<textarea rows="3" cols="20" name="remark"></textarea></li>
        <li><input type="submit" value="Save Goods"></li>
    </ul>
</form>

5. 批改商品界面
(1)因为 id 列自增长,所以批改商品信息不须要 id 这一列,但传参数有须要一起传送过来,所以增加了一个输入框,默认设置为暗藏,将其 value 设置为 id 的值

<form th:action="@{/goods/doUpdateGoods}" method="post">
    <input type="hidden" name="id" th:value="${goods.id}">
    <ul>
        <li>name:<input type="text" name="name" th:value="${goods.name}"></li>
        <li>remark:<textarea rows="3" cols="20" name="remark" th:text="${goods.remark}"></textarea></li>
        <li><input type="submit" value="Update Goods"></li>
    </ul>
</form>
正文完
 0