SpringBoot+Mybatis

创建数据库mybatis并创建表customer和student

创建SpringBoot工程

创建完工程后更改为本地的maven

更改完后导入依赖

        <!--MySQL-->        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!--mybatis和SpringBoot整合包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

 在application.properties添加数据库连接和mapper扫描

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

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

创建Student和Customer实体类(一定要和数据库字段名一致)

package com.wang.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 + '\'' +
                '}';
    }
}
package com.wang.pojo;

public class Student {
    private Integer id;
    private String name;
    private String pwd;

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


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

创建CustomerDao和StudentDao层

package com.wang.dao;

import com.wang.pojo.Customer;

import java.util.List;

public interface CustomerDao {

    public List<Customer> query(Customer customer);

    public void add(Customer customer);
}
package com.wang.dao;

import com.wang.pojo.Student;


import java.util.List;

public interface StudentDao {
    public List<Student> query();

    public void add(Student student);
}

在resources下创建mapper文件夹并在下面创建studentMapper.xml和customerMapper.xml文件添加sql语句(这里加入了if判断可以根据姓名或者职业查询实现条件查询)

<?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="com.wang.dao.CustomerDao">
    <select id="query" resultType="com.wang.pojo.Customer">
       select * from customer where 1=1
       <!--test中放接口参数名-->
       <if test="username!=null and username!=''">
          and  username=#{username}
       </if>
       <if test="jobs!=null and jobs!=''">
           and jobs=#{jobs}
       </if>
    </select>
    <insert id="add">
        insert  into customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
    </insert>
</mapper>
<?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="com.wang.dao.StudentDao">
    <select id="query" resultType="com.wang.pojo.Student">
       select * from student
    </select>
    <insert id="add">
        insert  into student(name,pwd) values (#{name},#{pwd})
    </insert>
</mapper>

创建service层(这里把student和customer同时使用一个添加)

package com.wang.service;

import com.wang.pojo.Customer;
import com.wang.pojo.Student;

import java.util.List;

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

创建service的接口实现类(CustomerServiceImpl)(这里的@Transactional是spring提供的事务管理如果不使用这个数据就会出现添加错误)(这里调用的两个dao可能会出现红线但不会报错)

package com.wang.service.impl;

import com.wang.dao.CustomerDao;
import com.wang.dao.StudentDao;
import com.wang.pojo.Customer;
import com.wang.pojo.Student;
import com.wang.service.CustomerService;
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 customerDao;
    @Autowired
    StudentDao studentDao;

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

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

创建Controller层(@RestController是让controller不进入视图解析器返回字符串可以不使用@RestController要想让他不返回字符串就使用@Controller如果不想改动@Controller 可以在想要返回字符串的方法上加@ResponseBody)(@RequestMapper("/xxxx")是请求的路径详情可以取spring中文文档:Spring 中文文档社区 | 中文文档

package com.wang.controller;

import com.wang.pojo.Customer;
import com.wang.pojo.Student;
import com.wang.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CustomerController {
    @Autowired
    CustomerService service;

    @RequestMapping("/query")
    public List<Customer> query(Customer customer){
        List<Customer> list=service.query(customer);
        System.out.println(list);
        return list;
    }
    @RequestMapping("/add")
    public String add(Customer customer, Student student){
        service.add(customer,student);
        return "1";
    }
}

在SpringBoot自带的启动类上加@MapperScan("xxxx.xxxx.xxx")扫描mapper接口(注意:SpringbootMybatisApplication是在创建项目时自动生成的他会创建在你自定义的包下)

package com.wang;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan({"com.wang.dao"})
public class SpringbootMybatisApplication {
//springboot=spring+springMvc
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

点击启动后在浏览器中输入网址:http://localhost:8080/query

就可以输出你数据库中的数据

想要添加数据就把路径后的query改为add并在后面拼接添加的值即可

http://localhost:8080/add?username=aaaa&jobs=qqweqw&phone=1123123&name=zzzzz&pwd=121212

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AqrJan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值