1
2
| implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3'
implementation 'org.mariadb.jdbc:mariadb-java-client:2.6.1'
|
각 버전은 원하는 버전을 입력하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # Data Model의 package 경로
mybatis.type-aliases-package = com.hours22.devstudent.Entity
# .xml Mapper의 경로
mybatis.mapper-locations = mapper/*.xml
# MariaDB Driver
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
# MariaDB URL
# ex) mariadb://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.url=jdbc:mariadb://{ip}:{port}/{DB}?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=22hours
spring.datasource.password=22hours
|
1
2
3
4
5
6
7
8
9
10
| @Repository
@Mapper
public interface CarMapper {
void createTable();
List<Car> findList();
void saveCar(Car car);
}
|
1
2
3
4
5
6
7
8
9
10
| @Getter
@Setter
@ToString
@AllArgsConstructor
public class Car {
private String name;
private String color;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| @Service
public class MapperService {
@Autowired
public CarMapper mapper;
public void saveCar(String name, String color) {
Car car = new Car(name, color);
mapper.save(Car);
}
public void createTable(){
mapper.createTable();
}
public void findList() {
mapper.findList().forEach(data -> System.out.println(data));
}
}
|
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
| <?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.hours22.devstudent.Repository.CarMapper">
<select id="findList" resultType="car">
SELECT name,
color
FROM car
</select>
<insert id="saveCar" parameterType="car">
INSERT INTO car (name,
color)
VALUES (#{name},
#{color})
</insert>
<insert id="createTable">
CREATE TABLE `car`
(
`name` VARCHAR(20) NOT NULL,
`color` VARCHAR(30) NOT NULL,
PRIMARY KEY (`name`)
)
</insert>
</mapper>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| @@RestController
@RequestMapping("/mapper/*")
public class CarContoller {
@Autowired
private MapperService mapperService;
@RequestMapping(value = "/create/table",method = RequestMethod.GET)
public void createTable(){
alarmService.createTable();
}
@RequestMapping(value = "/create/car",method = RequestMethod.GET)
public void createCar(){
alarmService.saveCar("Audi","RED");
}
@RequestMapping(value = "/find/list",method = RequestMethod.GET)
public void findList(){
alarmService.findList();
}
}
|