当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot + Mybatis增删改查实战记录

SpringBoot + Mybatis增删改查实战记录

2019年07月19日  | 移动技术网IT编程  | 我要评论

简介

springboot和mybatis是啥请自行百度,作者这里也是花了几天时间入门了这个框架用来完成任务,并且也算符合要求的完成了任务,期间也各种百度但是没找到自己想要的那种简单易懂的教程,所以踩了很多坑,写这个博客的目的就是为了让大家少踩一点坑,开始。

创建一个springboot项目

点开这个网站,创建一个springboot项目,如下图,这里用的是2.1.5,学技术嘛,就是要学新的。


选择依赖,点击左下角的dependencies

  1. web 我们这次开发的是web应用所以选择web
  2. thymeleaf 一款模板引擎,能够比较方便的展现后台传来的数据
  3. mysql 我们这次使用mysql数据库
  4. jdbc java 数据库连接 java database connectivity,简称jdbc
  5. mybatis 请看第一段


最后点击左下角的generate project,将会开始下载一个以你项目名称开头的zip文件,下载完成后解压到你的工作目录。

打开这个项目

这里使用的是idea,别的啥也行比如eclipse,这里只讲解idea的操作,安装破解idea百度一大堆,安装好之后打开idea(发现idea有个问题,有的时候自动import包好用,有的时候不好用,坑!),然后选择左上角的file->open,找到你刚刚解压的项目文件里的pom.xml点击ok如下图

目录结构

增加修改目录结构为下图

开始编写

这里我们就编写一个人员信息的增删改查

配置数据库数据库创建

打开mysql数据库创建一个叫test的数据库之后创建person表,这里使用的是navicat百度有破解版,会命令用命令行也行,如下图,记得设置主键自增,然后随便加几个数据以便之后查询。

application.yml

路径:/resources/application.yml

server:
 port: 8080

spring:
 datasource:
 name:
 url: jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf-8&allowmultiqueries=true&servertimezone=utc
 username: root
 password: root

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

person类

路径/model/person.java

package com.ljsh.test.model;

public class person {
 /*
 {id} 自增主键
 {name} 人员姓名
 {mobile} 人员电话
  */
 private int id;
 private string name;
 private string mobile;
 
 // 右键 generate -> setter and getter -> shift全选 -> ok 生成如下代码

 public int getid() {
  return id;
 }

 public void setid(int id) {
  this.id = id;
 }

 public string getname() {
  return name;
 }

 public void setname(string name) {
  this.name = name;
 }

 public string getmobile() {
  return mobile;
 }

 public void setmobile(string mobile) {
  this.mobile = mobile;
 }
 
 // 右键 generate -> tostring() -> 全选 -> ok 生成如下代码

 @override
 public string tostring() {
  return "person{" +
    "id=" + id +
    ", name='" + name + '\'' +
    ", mobile='" + mobile + '\'' +
    '}';
 }
}

persondao

路径:/dao/persondao.java

package com.ljsh.test.dao;

import com.ljsh.test.model.person;
import org.apache.ibatis.annotations.mapper;
import java.util.list;

@mapper
public interface persondao {
  /*
  查所有
  return list<person>
   */
  list<person> getall();

  /*
  根据id查询
  {id} 要查询人员的 id
   */
  person getpersonbyid(int id);
  
  /*
  删除
  {id} 要删除人员的 id
   */
  void delete(int id);

  /*
  更新
  {p} 要更新的person实例
   */
  void update(person p);

  /*
  增加
  {p} 要新增的person实例
   */
  void newp(person p);
}

persondao.xml

路径:/mapper/persondao.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" >
<!-- 这里填写对应的dao文件所在的路径 -->
<mapper namespace="com.ljsh.test.dao.persondao"  >
  <!-- 填写数据库里实例person对应的表的表名 -->
  <!-- 这里是作为一个变量使用 -->
  <sql id="table">person</sql>

  <!-- id属性填写dao文件里的函数名称 xxtype是参数或是结果的类型根据情况填写 -->
  <!-- 查询所有  -->
  <select id="getall" resulttype="com.ljsh.test.model.person">
    select
    *
    from
    <include refid="table" />
  </select>


  <!-- 根据id查询 -->
  <select id="getpersonbyid" resulttype="com.ljsh.test.model.person">
    select
    *
    from
    <include refid="table"/>
    where
    id = #{id}
  </select>

  <!-- 增 -->
  <insert id="newp" parametertype="com.ljsh.test.model.person">
    insert into
    <include refid="table"/>
    (name,phone)
    values
    (#{name},#{phone})
  </insert>

  <!-- 改 -->
  <update id="update" parametertype="com.ljsh.test.model.person">
    update
    <include refid="table"/>
    set
    <!--<if test="name != null">name = #{name}</if>-->
    name = #{name},phone = #{phone},status = #{status}
    where
    id = #{id}
  </update>

  <!-- 删 -->
  <delete id="delete" parametertype="com.ljsh.test.model.person">
    delete from
    <include refid="table"/>
    where
    id = #{id}
  </delete>
</mapper>

personservice

路径:/service/personservice.java

package com.ljsh.test.service;
import com.ljsh.test.dao.persondao;
import com.ljsh.test.model.person;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import java.util.list;

@service
public class personservice {
  @autowired
  persondao persondao;

  /*
    service层介于controller和dao之间作为服务层进行一些逻辑处理,
    这里逻辑太简单所以知识单纯调用dao所以不做注释
   */
  public list<person> getall(){
    return persondao.getall();
  }

  public person getpersonbyid(int id){
    return persondao.getpersonbyid(id);
  }

  public void delete(int id){
    persondao.delete(id);
  }

  public void update(person p){
    persondao.update(p);
  }

  public void newp(person p){
    persondao.newp(p);
  }
}

personcontroller

路径:/controller/personcontroller.java

package com.ljsh.test.controller;

import com.ljsh.test.model.person;
import com.ljsh.test.service.personservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;
import java.util.list;

@controller
public class personcontroller {

  @autowired
  personservice personservice;

  // 设置访问路由值为路径
  @requestmapping("/")
  public modelandview index(){
    // 顾名思义 实体和数据 同时返回页面模板和数据
    modelandview mav = new modelandview("index");
    list<person> list = personservice.getall();
    mav.addobject("list",list);
    return mav;
  }
}

前端页面

路径:/templates/

<!doctype html>
<html lang="en">
<!-- -->
<!-- 使用thymeleaf需引入 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div id="tablep">
    <table>
      <caption>人员信息</caption>
      <tr>
        <th>name</th>
        <th>phone</th>
      </tr>
      <!-- 通过th命令使用一些操作 -->
      <!-- 通过${} 使用变量 -->
      <tr th:each="item: ${list}">
        <td th:text="${{item.name}}">还没有任何人员信息哦</td>
        <td th:text="${{item.mobile}}">你是不是想独吞奖品</td>
      </tr>
    </table>
  </div>
</div>
</body>
</html>

右上角运行


要是没有这个可以右侧选择testapplication右键run,结果图如下

未完待续

熄灯睡觉了,写的有点慢,删改查还没来及写,如果需求留言,我会继续更新。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网