Fork me on GitHub

SpringBoot———整合Mybatis-注解形式

SpringBoot———整合Mybatis-注解形式,

添加依赖

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.zzqd</groupId>
<artifactId>SpringBoot_Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringBoot-Child1</artifactId>


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


<!-- 加入依赖,代码做修改,可以不用重新运行 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- mybatis的分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>
</project>
application.properties
1
2
3
4
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC
spring.datasource.username: root
spring.datasource.password: root

持久层

pojo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package net.zzqd.pojo;

public class Customer {
private Integer id;
private String name;
private String gender;
private String telephone;
private String address;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
Mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package net.zzqd.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import net.zzqd.pojo.Customer;

public interface CustomerMapper
{
/**
* 查询所有数据
*/
public List<Customer> findAll();

/**
* 保存数据
* @param customer
*/
@Insert("insert into t_customer (name,gender,telephone,address) values(#{name},#{gender},#{telephone},#{address}")
public void save(Customer customer);
/**
* 根据id查询对象
* @param id
* @return
*/
@Select("select * from t_customer where id=#{id}")
public Customer findById(@Param("name")Integer id);

/**
* 修改对象数据
* @param customer
*/
@Update("update(#{name},#{gender},#{telephone},#{address}) set t_customer where id=#{id}")
public void update(Customer customer);

/**
* 删除数据
* @param id
*/
@Delete("delete from t_customer where id=#{id}")
public void delete(Integer[] id);


}

业务层

Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.zzqd.service;

import java.util.List;

import net.zzqd.pojo.Customer;

public interface CustomerService {
//添加用户
void addCustomer(Customer customer);

Customer findById(Integer id);
//分页查找
List<Customer> findCustomer(int page,int rows);
}
ServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package net.zzqd.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import net.zzqd.mapper.CustomerMapper;
import net.zzqd.pojo.Customer;

@Service
public class CustomerServiceImpl implements CustomerService {

@Autowired
private CustomerMapper customerMapper;

@Override
public void addCustomer(Customer customer) {

customerMapper.save(customer);
}

@Override
public List<Customer> findCustomer(int page, int rows) {
List<Customer> customers = customerMapper.findAll();
PageHelper.startPage(page, rows);

return customers;
}

@Override
public Customer findById(Integer id) {

return customerMapper.findById(id);
}
}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package net.zzqd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import net.zzqd.pojo.Customer;
import net.zzqd.service.CustomerService;

@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;

@RequestMapping("/saveCustomer")
// @ResponseBody
public String saveCustomer()
{
Customer customer = new Customer();
customer.setName("张张");
customer.setGender("女");
customer.setAddress("郑州");
customer.setTelephone("18888885284");

customerService.addCustomer(customer);
return "success";
}

@RequestMapping("/findCustomer/{page}/{rows}")
// @ResponseBody
public List<Customer> findCustomer(@PathVariable int page, @PathVariable int rows)
{
return customerService.findCustomer(page, rows);
}

@RequestMapping("/findCustomer")
// @ResponseBody
public Customer findCustomer(Integer id)
{
return customerService.findById(id);
}
}

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package net.zzqd.app;

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

//@EnableAutoConfiguration
//@ComponentScan("net.zzqd.controller")
//默认情况下扫描的是当前包以及当前包的子包
@SpringBootApplication(scanBasePackages= {"net.zzqd"})//组合注解
@MapperScan("net.zzqd.mapper")
public class SpringApplications {
public static void main(String[] args)
{
SpringApplication.run(SpringApplications.class, args);
}
}