Springboot整合mybatis

1.建库建表

2.创建项目,引入依赖
3.创建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
4.创建customer的pojo类 
package com.hxci.zmy.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 + '\'' +
                '}';
    }
}
5.创建customerDao接口
package com.hxci.zmy.dao;
 
import com.hxci.zmy.pojo.Customer;
import com.hxci.zmy.pojo.Student;
 
import java.util.List;
 
public interface CustomerDao {
    public List<Customer> query();
    public void add(Customer customer);
}
6.创建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.zmy.dao.CustomerDao">
    <select id="query" resultType="com.hxci.zmy.pojo.Customer">
        select * from customer
    </select>
    <insert id="add">
        insert into customer (username,jobs,phone) value (#{username},#{jobs},#{phone})
    </insert>
   <!-- <delete id="update">
        update customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id}
    </delete>
    <delete id="delete">
        delete from customer where id=#{id}
    </delete>
    <select id="queryById" resultType="com.hxci.zmy.pojo.Customer">
        select * from customer where id=#{id}
    </select>-->
</mapper>
7.创建CustomerService和CustomerServiceImpl
package com.hxci.zmy.service;
 
import com.hxci.zmy.pojo.Customer;
import com.hxci.zmy.pojo.Student;
 
import java.util.List;
 
public interface CustomerService {
    public List<Customer> query();
 
    void add(Customer customer, Student student);
}
package com.hxci.zmy.service;
 
import com.hxci.zmy.dao.CustomerDao;
import com.hxci.zmy.dao.StudentDao;
import com.hxci.zmy.pojo.Customer;
import com.hxci.zmy.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    CustomerDao dao;
    @Autowired
    StudentDao studentDao;
    public List<Customer> query(){
        return dao.query();
    }
 
    @Override
    public void add(Customer customer, Student student) {
        dao.add(customer);
        System.out.println(1/0);//模拟异常
        studentDao.add(student);
    }
}
8.创建CustomerController
package com.hxci.zmy.controller;
 
import com.hxci.zmy.pojo.Customer;
import com.hxci.zmy.pojo.Student;
import com.hxci.zmy.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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class CustomerController {
    //CustomerService service = new CustomerServiceImpl();
 
    @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;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值