一.创建项目
1.在idea里新建SpringBoot工程
2.改一些信息,符合自己需求就行,比如java版本,我只有8,所以就选它
3.选择所需的依赖,选了之后会在右边显示,然后next创建

二.配置项目环境
 1.建好项目后,第一件事,打开settings,搜索Maven,改成本地的位置

2.添加所需依赖
  <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency> 
2.建立项目结构,值得注意的是,建完springboot项目之后那个自带的类(引导类)要放置在外面,否则运行后404,banner.txt是修改运行时的那个spring图像的,没啥用,忽略就行

3.application.properties,第一行是端口号,最后一行是mapper的地址,中间是连接数据库
server.port=80
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=x5
mybatis.mapper-locations=classpath:mapper/**.xml
 
4.数据库--表的创建

5.mapper
customerMapper.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">
         <!-- 1.namespace:接口的一个路径 ,2.id :接口下抽象方法名
     3.接口的返回值和resultType 对应上 ,4接口的参数 对应上-->
<mapper namespace="com.hxci.hanbo.dao.CustomerDao">
    <select id="query" resultType="com.hxci.hanbo.pojo.Customer">
       select * from customer
    </select>
    <insert id="add" >
      insert  into  customer (username,jobs,phone) values (#{username},#{jobs},#{phone})
    </insert>
    <update id="update">
    update customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id}
</update>
    <delete id="delete">
        delete from customer where id=#{id}
    </delete>
    <select id="queryById" resultType="com.hxci.hanbo.pojo.Customer">
       select * from customer where id=#{id}
    </select>
</mapper> 
studentMapper.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">
<!-- 1.namespace:接口的一个路径 ,2.id :接口下抽象方法名
3.接口的返回值和resultType 对应上 ,4接口的参数 对应上-->
<mapper namespace="com.hxci.hanbo.dao.StudentDao">
    <select id="query" resultType="com.hxci.hanbo.pojo.Student">
       select * from student
    </select>
    <insert id="add" >
      insert  into  student (studentname,age,address) values (#{studentname},#{age},#{address})
    </insert>
    <update id="update">
    update customer set studentname=#{studentname},jobs=#{age},address=#{address} where id=#{id}
</update>
    <delete id="delete">
        delete from student where id=#{id}
    </delete>
    <select id="queryById" resultType="com.hxci.hanbo.pojo.Student">
       select * from student where id=#{id}
    </select>
</mapper> 
三.写代码
编写业务代码
实体:根据数据库表里的字段建
dao:
package com.hxci.hanbo.dao;
import com.hxci.hanbo.pojo.Student;
import java.util.List;
public interface StudentDao {
    public List<Student> query();
    public void add(Student student);
    public void update(Student student);
    public Student queryById(Integer id);
    public void delete(Integer id);
}
 
package com.hxci.hanbo.dao;
import com.hxci.hanbo.pojo.Customer;
import java.util.List;
public interface CustomerDao {
    public List<Customer> query();
    void  add (Customer customer);
    public void update(Customer customer);
    public Customer queryById(Integer id);
    public void delete(Integer id);
}
 
 
 
 
service:
package com.hxci.hanbo.service;
import com.hxci.hanbo.pojo.Customer;
import com.hxci.hanbo.pojo.Student;
import java.util.List;
public interface CustomerService {
    public List<Customer> query();
    public void add(Customer customer, Student student);
    void update(Customer customer);
    void delete(Integer id);
    public Customer queryById(Integer id);
}
 
serviceimpl:
package com.hxci.hanbo.service.impl;
import com.hxci.hanbo.dao.CustomerDao;
import com.hxci.hanbo.dao.StudentDao;
import com.hxci.hanbo.pojo.Customer;
import com.hxci.hanbo.pojo.Student;
import com.hxci.hanbo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class CustomerImpl implements CustomerService {
    @Autowired
    CustomerDao dao;
    @Autowired
    StudentDao studentDao ;
    public List<Customer> query(){
        return dao.query();
    }
    public void add(Customer customer, Student student) {
        dao.add(customer);
        studentDao.add(student);
    }
    @Override
    public void update(Customer customer) {
        dao.update(customer);
    }
    @Override
    public void delete(Integer id) {
        dao.delete(id);
    }
    @Override
    public Customer queryById(Integer id) {
        Customer ById = dao.queryById(id);
        return ById;
    }
}
 
controller:
package com.hxci.hanbo.controller;
import com.hxci.hanbo.pojo.Customer;
import com.hxci.hanbo.pojo.Student;
import com.hxci.hanbo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CustomController {
    @Autowired
    CustomerService service;
    @RequestMapping("query")
    public List<Customer> query() { // spring boot controller方法返回值看可以自动转json,ssm框架不行
        List<Customer> list = service.query();
        return list;
    }
    @RequestMapping("add")
    public String add(Customer customer, Student student) {
        service.add(customer, student);
        return null;
    }
    @RequestMapping("/update")
    public void update(Customer customer) {
        service.update(customer);
    }
    @RequestMapping("/delete")
    public void delete(Integer id) {
        service.delete(id);
    }
    ;
    @RequestMapping("/ById")
    public String queryById(Integer id) {
        Customer customer = service.queryById(id);
        System.out.println(customer);
        return null;
    }
    
}
 
引导类:
在里面加了个@MapperScan注解,扫描mapper的
package com.hxci.hanbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
@SpringBootApplication
@MapperScan("com.hxci.hanbo.dao")
public class HanboApplication {
    public static void main(String[] args) {
        SpringApplication.run(HanboApplication.class, args);
    }
}
 
四.运行测试
使用Postman工具Get旁边写地址
查询:

添加:
因为是get请求,所以填这些东西的时候,上面的路径也会跟着变

key对应字段,value对应值,点击send执行 ,一般来说没报错就是成功了,也可以去数据库里看一看

以此类推:删除

查询一条:

更改的就只是后面的地址,根据controller里的@RequestMapping改










