整合 Springboot+Mybatis

1.所需依赖

<!--父子依赖-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.4</version>
</parent>
<dependencies>
<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis整合依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<!--jdbc启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>2.4.4</version>
</dependency>
 <!--web启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.4</version>
</dependency>
 <!--测试-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>
<!--jdbc启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
    
<!--快速构建实体类-->
<!--需要先引入lombok的插件-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<!--页面模板-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

附赠:
maven依赖库:https://mvnrepository.com/

2.整合配置文件 application.yml

#设置端口
server:
  port: 81
#配置数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/admin?useSSL=true&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver
    #设置templates访问地址及文件格式
  thymeleaf:
    cache: false
    prefix: classpath:/templates/pages/
    suffix: .html

#映射文件
#使用xml形式写SQL语句需要配置
mybatis:
  mapper-locations: classpath:/mapper/*/*.xml

springboot支持properties和yml或(yaml)文件格式

3.环境图示

1).图示

项目目录

2).实体类

import lombok.Data;

import java.util.Date;

//通过引入lombok插件和依赖使用@Data可省略get set等等
@Data
public class tb_goods {
    private Integer id;
    private String name;
    private String remark;
    private Date createdTime;
}

3).持久层接口 Mapper

import cn.shopping.pojo.tb_goods;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface GoodsMapper {
    //查询全部信息
    List<tb_goods> findAll();
	//通过名称模糊查询相关信息
    List<tb_goods> findGoodsByName(String name);
    //根据id删除
    int deleteById(Integer id);
	//添加
    void insertGoods(tb_goods tb_goods);
	//根据id查询一条数据
    tb_goods selectById(int id);
	//修改
    void updateByGoods(tb_goods tb_goods);
}

4).业务逻辑层 及 实现层 Service ServiceImpl

逻辑层接口

import cn.shopping.pojo.tb_goods;

import java.util.List;

public interface GoodsService {
    //查询全部
    List<tb_goods> findAll();
    //根据名称模糊查询
    List<tb_goods> findGoodsByName(String name);
     //根据id删除
    int deleteById(Integer id);
    //添加
    void insertGoods(tb_goods tb_goods);
    //根据id查询数据
    tb_goods selectById(int id);
    //修改
    void updateByGoods(tb_goods tb_goods);
}

实现类

import cn.shopping.mapper.GoodsMapper;
import cn.shopping.pojo.tb_goods;
import cn.shopping.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<tb_goods> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public List<tb_goods> findGoodsByName(String name) {
        return goodsMapper.findGoodsByName(name);
    }

    @Override
    public int deleteById(Integer id) {
        return goodsMapper.deleteById(id);
    }

    @Override
    public void insertGoods(tb_goods tb_goods) {
        goodsMapper.insertGoods(tb_goods);
    }

    @Override
    public tb_goods selectById(int id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public void updateByGoods(tb_goods tb_goods) {
        goodsMapper.updateByGoods(tb_goods);
    }
}

5).控制层 Controller

import cn.shopping.pojo.tb_goods;
import cn.shopping.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class GoodsController {
	@Autowired
    private GoodsService goodsService;

    //查询全部 体现方式一
    @RequestMapping("/doFindGoods")
    public String doFindGoods(Model model){
        long t1=System.currentTimeMillis();
        List<tb_goods> tbGoods = goodsService.findAll();
        long t2=System.currentTimeMillis();

        model.addAttribute("goods", tbGoods);//把查询到的数据放在model中
        model.addAttribute("time", t2-t1);//实现在页面上显示执行时长
        return "index";
    }
	//查询全部 体系方式二
    @RequestMapping("/doFindGoodsTwo")
    public ModelAndView doFindGoodsTwo(ModelAndView view){
        //获取首次时间
        long t1=System.currentTimeMillis();
        List<tb_goods> serviceAll = goodsService.findAll();
        //获取二段时间
        long t2=System.currentTimeMillis();
        view.addObject("goods",serviceAll);
        //二段时间 减去 首次时间 得到运行时长
        view.addObject("time",t2-t1);
        view.setViewName("index");
        return view;
    }

	//根据名称模糊查询
    @RequestMapping("/doFindGoodByName")
    public ModelAndView doFindGoodByName(String name){
        //获取首次时间
        long t1=System.currentTimeMillis();
        List<tb_goods> goodsByName = goodsService.findGoodsByName(name);
        //获取二段时间
        long t2=System.currentTimeMillis();
        //创建视图模型
        ModelAndView view=new ModelAndView();
        view.addObject("goods",goodsByName);
        //二段时间 减去 首次时间 得到运行时长
        view.addObject("time",t2-t1);
        view.setViewName("index");
        return view;
    }
	//根据id删除
    @RequestMapping("/deleteById/{id}")
    public String deleteById(@PathVariable Integer id){
        goodsService.deleteById(id);
        return "redirect:/doFindGoodsTwo";
    }
	
    //添加
    @RequestMapping("/insertGoods")
    public String insertGoods(tb_goods tb_goods){
        goodsService.insertGoods(tb_goods);
        return "redirect:/doFindGoodsTwo";
    }

    //设置页面跳转
    @RequestMapping("/jump")
    public String add_Goods(){
        return "/goods-add";
    }

    //根据id查询
    @RequestMapping("/doFindById/{id}")
    public ModelAndView doFindById(@PathVariable int id,ModelAndView view){
        tb_goods byId = goodsService.selectById(id);
        view.addObject("goods",byId);
        view.setViewName("goods-update");
        return view;
    }
	//修改
    @RequestMapping("/doUpdateByGoods")
    public String doUpdateByGoods(tb_goods tb_goods){
        goodsService.updateByGoods(tb_goods);
        return "redirect:/doFindGoodsTwo";
    }
}

6).xml形式SQL GoodsMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.shopping.mapper.GoodsMapper">
    <!--查询全部-->
    <select id="findAll" resultType="cn.shopping.pojo.tb_goods">
        select * from tb_goods
    </select>
    <!--根据名称模糊查询-->
    <select id="findGoodsByName" resultType="cn.shopping.pojo.tb_goods" parameterType="String">
        select * from tb_goods
        <if test="value!=null and value!=''">
            where `name` like concat("%",#{value},"%")
        </if>
    </select>
    <!--根据id删除-->
    <delete id="deleteById" parameterType="int">
        delete from tb_goods where id=#{id}
    </delete>
    <!--添加-->
    <insert id="insertGoods" parameterType="cn.shopping.pojo.tb_goods">
        insert into tb_goods(`name`,`remark`,`createdTime`) values(#{name},#{remark},NOW())
    </insert>
    <!--根据id查询-->
    <select id="selectById" resultType="cn.shopping.pojo.tb_goods" parameterType="int">
        select * from tb_goods where id=#{id}
    </select>
    <!--修改-->
    <update id="updateByGoods" parameterType="cn.shopping.pojo.tb_goods">
        update tb_goods set `name`=#{name},remark=#{remark} where id=#{id}
    </update>
</mapper>

7.页面

注意需要bootStrap的css样式{bootstrap.min.css} 和 引入thymeleaf模板依赖
@{}表示 http://localhost:81/

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <link rel="stylesheet"  th:href="@{/css/bootstrap.min.css}">
    <style type="text/css">
        #a{
            text-decoration: none;
        }
        h1{
            text-align: center;
        }

        .first{
            display: inline-block;

        }
        .form-search{
            float: right;
            display: inline-block;

        }
        h6{
            text-align: right;
        }
    </style>

</head>
<body>
<div class="container">
    <div class="row ">
        <div class="span12">
            <h1>
                <small >商品信息</small>
            </h1>

            <div class="first">
                <a type="button" class="btn btn-primary" th:href="@{/jump}" >新增商品</a>
            </div>

            <form class="form-search" action="/doFindGoodByName" method="get">
                <input class="input-medium search-query" type="text"  placeholder="请输入商品名" name="name"/>
                <button type="submit" class="btn">查找</button>
            </form>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>产品</th>
                    <th>描述</th>
                    <th>创建时间</th>
                    <th colspan="2">&nbsp;&nbsp;&nbsp;操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="g : ${goods}">
                    <td th:text="${g.getId()}">1</td>
                    <td th:text="${g.getName()}">AAAAAAA</td>
                    <td th:text="${g.getRemark()}">aaa</td>
                    <td th:text="${#dates.format(g.getCreatedTime(), 'yyyy/MM/dd HH:mm')}">aa</td>
                    <!-- @{ } 表示 http://localhost/ -->
                    <td>
                        <a  th:href="@{/deleteById/{id}(id=${g.getId()})}">删除</a>
                        <a type="button" class="btn btn-info" th:href="@{/doFindById/{id}(id=${g.id})}">修改</a>
                    </td>
                </tr>
                </tbody>
            </table>
            <!-- 在页面上显示执行时长 -->
            <h6>执行时长:[[${time}]]ms</h6>
        </div>
    </div>
</div>
</body>
</html>

goods-add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>新增商品</title>
    <link rel="stylesheet"  th:href="@{/css/bootstrap.min.css}">
</head>
<body>

<div class="container">
    <h3 style="text-align: center">新增商品</h3>
    <form th:action="@{/insertGoods}" method="post">
        <div class="form-group">
            <label >Name:</label>
            <input type="text" class="form-control" placeholder="Enter name" name="name">
        </div>
        <div class="form-group">
            <label >Remark:</label>
            <textarea  class="form-control" placeholder="Enter remark" name="remark"></textarea>
        </div>
        <a href="/doFindGoods" class="btn btn-primary " role="button">return</a>
        <button type="submit" class="btn btn-primary" style="float:right">提交</button>
    </form>
</div>
</body>
</html>

goods-update.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>修改商品</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
</head>
<body>
<div class="container">
    <h3 style="text-align: center">修改商品</h3>
    <form th:action="@{/doUpdateByGoods}" method="post">
        <input type="hidden" name="id" th:value="${goods.id}">
        <div class="form-group">
            <label >Name:</label>
            <input type="text" class="form-control" placeholder="Enter name" name="name" th:value="${goods.name}" >
        </div>
        <div class="form-group">
            <label >Remark:</label>
            <textarea  class="form-control" placeholder="Enter remark" name="remark" th:text="${goods.remark}"></textarea>
        </div>
        <a href="/doFindGoodsTwo" class="btn btn-primary" role="button">return</a>
        <button type="submit" class="btn btn-primary" style="float:right">修改</button>
    </form>
</div>
</body>
</html>

比较简单的整合springboot+mybatis框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笔墨画卷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值