利用SpringBoot整合MyBatis

1.我们先创建一个springboot工程

选择jdk1.8的版本,点击next

起自己想要的名字,jar包选择8,点击next我们在这里勾选springweb,点击next,这样我们就创建好了一个springboot工程

 二、配置maven工程

在这里点击file然后在点击settings

找到maven工程,选择自己本地的maven版本

 三、添加依赖

我们在pom.xml中添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <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>

 四、连接数据库

我们在resources文件夹下找到application.properties文件,并将下列代码写进去,就代表这我们成功连接到了数据库。

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

五、创建如下目录结构

 六、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.jys.dao.CustomerDao">
    <select id="query" resultType="com.hxci.jys.pojo.Customer">
       select * from customer
    </select>

   <insert id="add">
      insert  into  customer (username,jobs,phone) values (#{username},#{jobs},#{phone})
    </insert>

    <!--<delete id="delete">
        delete from customer where id=#{id}
    </delete>

    <update id="update">
        update customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id};
    </update>

    <select id="queryById" resultType="com.hxci.jys.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.jys.dao.StudentDao">
    <select id="query" resultType="com.hxci.jys.pojo.Student">
       select * from student
    </select>

    <insert id="add">
      insert  into student (username,sno) values (#{username},#{sno})
    </insert>

    <!--<delete id="delete">
        delete from student where id=#{id}
    </delete>

    <update id="update">
        update student set username=#{username},sno=#{sno} where id=#{id};
    </update>

    <select id="queryById" resultType="com.hxci.jiangyunsong.pojo.Student">
        select * from student where id=#{id}
    </select>-->

</mapper>

六、实现查找以及添加功能

controller:

CustomerController:

package com.hxci.jys.controller;


import com.hxci.jys.pojo.Customer;
import com.hxci.jys.pojo.Student;
import com.hxci.jys.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CustomerController {
    //CustomerService service = new CustomerServiceImpl();

    @Autowired
    CustomerService service;

    //传递一个参数 参数名称和pojo类的属性名 和数据库的列名一致
    //如果接收的参数名是pojo类的属性名,那么由dao层接口 直接由#{}(一个占位符)或者${} 传递到mapper.xml
    @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;
    }
}

dao:

CustomerDao:

package com.hxci.jys.dao;

import com.hxci.jys.pojo.Customer;

import java.util.List;

public interface CustomerDao {

    List<Customer> query();

    void  add (Customer customer);
}

StudentDao:

package com.hxci.jys.dao;


import com.hxci.jys.pojo.Student;

import java.util.List;

public interface StudentDao {

    List<Student> query();

    void  add (Student customer);



}

pojo:

Customer:

package com.hxci.jys.pojo;

public class Customer {
    private Integer id;
    private String username;
    private String jobs;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getJobs() {
        return jobs;
    }

    public void setJobs(String jobs) {
        this.jobs = jobs;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", jobs='" + jobs + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

Student:

package com.hxci.jys.pojo;

public class Student {
    private Integer id;
    private String username;
    private String sno;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sno='" + sno + '\'' +
                '}';
    }
}

service:

CustomerService:

package com.hxci.jys.service;

import com.hxci.jys.pojo.Customer;
import com.hxci.jys.pojo.Student;

import java.util.List;

public interface CustomerService {
    List<Customer> query();
    void  add (Customer customer, Student student);

}

ServiceCustomerImpl:

package com.hxci.jys.service;


import com.hxci.jys.dao.CustomerDao;
import com.hxci.jys.dao.StudentDao;
import com.hxci.jys.pojo.Customer;
import com.hxci.jys.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao dao;
    @Autowired
    StudentDao studentDao ;

    public List<Customer> query(){
        return dao.query();
    }

    @Transactional
    public void add(Customer customer, Student student) {
        dao.add(customer);
        studentDao.add(student);
    }


}

JysApplication:

package com.hxci.jys;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.hxci.jys.dao")
public class JysApplication {
    //spring boot == spring + spring mvc
    public static void main(String[] args)
    {
        SpringApplication.run(JysApplication.class, args);
    }

}

七、Postman

在这里我们使用一个插件Postman来实现我们程序的查找和添加

查找:我们要注意是查找,所以上面的路径名字要是和我们定义的方法名一致

 添加也是同理,路径名要和定义的方法名一致

 我们看一下数据库也成功插入了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值