pom.xml中添加依赖
mysql
mysql-connector-java
org.projectlombok
lombok
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
说是简单测试版,所有的文件都在Application.java中,不适合正经的开发。
在application.properties中配置数据库信息:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#根据自己环境配置可连接的数据库信息
spring.datasource.url=jdbc:mysql://172.16.14.40:3306/springboot
spring.datasource.username=devgroup
spring.datasource.password=devgroup1
2
3
4
5
sql语句:
drop table if exists car;
create table car (
id int(10) auto_increment not null,
username varchar(50) not null,
primary key(id)
)1
2
3
4
5
6
7
8
注解版
Application.java
package com.zhu;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner demo(CarMapper carMapper) {
return args -> {
List cars = Arrays.asList(new Car("zhangsan"), new Car("lisi"), new Car("wangwu"));
cars.forEach(car -> {
carMapper.insert(car);
System.err.println("插入数据:" + car.toString());
});
System.err.println("-----------查询所有的数据-----------------");
carMapper.selectAll().forEach(System.out::println);
System.err.println("----------删除数据---------------");
carMapper.RemoveOne(carMapper.selectAll().size());
System.err.println("----------修改数据---------------");
carMapper.UpdateOne(135,"zhaoliu");
};
}
}
@Mapper
interface CarMapper {
@Options(useGeneratedKeys = true)
@Insert("insert into car(username) values(#{username})")
void insert(Car car);
@Update("update car set username = #{username} where id = #{id}")
void UpdateOne(@Param("id") int id,@Param("username") String username);
@Insert("delete from car where id = #{id}")
void RemoveOne(int id);
@Select("select * from car")
Collection selectAll();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Car {
public Car(String username) {
this.username = username;
}
public Car(Integer id, String username) {
this.id = id;
this.username = username;
}
private int id;
private String username;
@Override
public String toString() {
return "Car [id=" + id + ", username=" + username + "]";
}
}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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
点击run Application:
配置文件版
src/main/resources/com/zhu/CarMapper.xml
SELECT * FROM car
username = #{username}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Application.java
package com.zhu;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner demo(CarMapper carMapper) {
return args -> {
List cars = Arrays.asList(new Car("zhangsan"), new Car("lisi"), new Car("wangwu"));
cars.forEach(car -> {
carMapper.insert(car);
System.err.println("插入数据:" + car.toString());
});
System.err.println("-----------查询所有的数据-----------------");
carMapper.selectAll().forEach(System.out::println);
System.err.println("----------删除数据---------------");
carMapper.RemoveOne(carMapper.selectAll().size());
System.err.println("----------修改数据---------------");
carMapper.UpdateOne(135,"zhaoliu");
System.err.println("---------配置文件查询---------------------");
carMapper.searchOne("zhangsan").forEach(System.out::println);
};
}
}
@Mapper
interface CarMapper {
@Options(useGeneratedKeys = true)
@Insert("insert into car(username) values(#{username})")
void insert(Car car);
@Update("update car set username = #{username} where id = #{id}")
void UpdateOne(@Param("id") int id,@Param("username") String username);
@Insert("delete from car where id = #{id}")
void RemoveOne(int id);
@Select("select * from car")
Collection selectAll();
Collection searchOne(@Param("username") String username);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Car {
public Car(String username) {
this.username = username;
}
public Car(Integer id, String username) {
this.id = id;
this.username = username;
}
private int id;
private String username;
@Override
public String toString() {
return "Car [id=" + id + ", username=" + username + "]";
}
}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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
OK