创建数据库mybatis并创建表customer和student
创建SpringBoot工程
导入依赖
mysql
mysql-connector-java
5.1.45
com.baomidou
mybatis-plus
3.3.2
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
在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=x5 mybatis.mapper-locations=classpath:mapper/*.xml
创建Student和Customer实体类
package com.lijunbo.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.lijunbo.pojo;
public class Student {
private Integer id;
private String name;
private String 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;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", pwd='" + pwd + ''' +
'}';
}
}
创建CustomerDao和StudentDao层
package com.lijunbo.dao;
import com.lijunbo.pojo.Customer;
import java.util.List;
public interface CustomerDao {
public List query(Customer customer);
public void add(Customer customer);
}
package com.lijunbo.dao;
import com.lijunbo.pojo.Student;
import java.util.List;
public interface StudentDao {
public List query();
public void add(Student student);
}
在resources下创建mapper文件夹并在下面创建studentMapper.xml和customerMapper.xml文件
select * from customer where 1=1 and username=#{username} and jobs=#{jobs} insert into customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
insert into student(name,pwd) values (#{name},#{pwd})
创建service层
package com.lijunbo.service;
import com.lijunbo.pojo.Customer;
import com.lijunbo.pojo.Student;
import java.util.List;
public interface CustomerService {
public List query(Customer customer);
public void add(Customer customer, Student student);
}
创建service的接口实现类
package com.lijunbo.service.impl;
import com.lijunbo.dao.CustomerDao;
import com.lijunbo.dao.StudentDao;
import com.lijunbo.pojo.Customer;
import com.lijunbo.pojo.Student;
import com.lijunbo.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 query(Customer customer) {
return customerDao.query(customer);
}
@Transactional
public void add(Customer customer, Student student) {
customerDao.add(customer);
studentDao.add(student);
}
}
创建Controller层
package com.lijunbo.controller;
import com.lijunbo.pojo.Customer;
import com.lijunbo.pojo.Student;
import com.lijunbo.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 {
@Autowired
CustomerService service;
@RequestMapping("/query")
public List query(Customer customer){
List 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接口
package com.lijunbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan({"com.lijunbo.dao"})
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
启动后在浏览器中输入网址:http://localhost:8080/query,就可以输出你数据库中的数据



