1.首先创建一个工程
点击“File”->New Project
2.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
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.在java文件夹下创建controller、dao、pojo、service,在resources资源文件夹下建mapper文件夹,并创建CustomerMapper.xml文件
项目结构为
5.编写CustomerController
package com.hxci.hyc.controller;
import com.hxci.hyc.pojo.Customer;
import com.hxci.hyc.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 {
@Autowired
CustomerService service;
@RequestMapping("query")
public List<Customer> query(Customer customer、){
List<Customer> list=service.query(customer);
return list;
}
@RequestMapping("add")
public String add(Customer customer) {
service.add(customer);
return null;
}
}
6.在Customer创建实体类对象
package com.hxci.hyc.pojo;
public class Customer {
private Integer id;
private String sname;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
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;
}
}
7.编写CustomerService
package com.hxci.hyc.service;
import com.hxci.hyc.pojo.Customer;
import java.util.List;
public interface CustomerService {
public List<Customer> query(Customer customer);
public void add(Customer customer);
}
8.编写CustomerServiceImpl实现类
package com.hxci.hyc.service;
import com.hxci.hyc.dao.CustomerDao;
import com.hxci.hyc.pojo.Customer;
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;
public List<Customer> query(Customer customer){
return dao.query(customer);
}
@Override
public void add(Customer customer) {
dao.add(customer);
}
}
9.编写dao层接口
package com.hxci.hyc.dao;
import com.hxci.hyc.pojo.Customer;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CustomerDao {
public List<Customer> query(@Param("customer") Customer customer);
public void add(Customer customer);
}
10.编写dao层对应的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.hxci.hyc.dao.CustomerDao">
<select id="query" resultType="com.hxci.hyc.pojo.Customer">
select * from customer where 1=1
<if test="customer.sname != null and customer.sname !=''">
and sname=#{customer.sname}
</if>
<if test="customer.jobs != null and customer.jobs !=''">
and jobs=#{customer.jobs}
</if>
</select>
<insert id="add">
insert into customer (sname,jobs,phone) values(#{sname},#{jobs},#{phone})
</insert>
</mapper>