3、快速配置application.properties文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123 spring.datasource.url=jdbc:mysql://localhost:3306/hello mybatis.mapper-locations=classpath:mapper/*.xml //实现mapper接口配置4、新建6个文件夹以及在文件夹下创建文件
5、编写entity-Person
package com.example.entity;
import lombok.Data;
@Data
public class Person {
private String name; //这里的属性和数据库表的字段一致
private String age;
}
6、编写mapper文件
PersonMapper.java
package com.example.mapper;
import com.example.entity.Person;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PersonMapper {
Person getPerson();
}
personMapper.xml
7、编写Service PersonService.javaselect * from person
package com.example.service;
import com.example.entity.Person;
public interface PersonService {
Person getPerson();
}
PersonServiceImpl.java
package com.example.service.Impl;
import com.example.entity.Person;
import com.example.mapper.PersonMapper;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;
@Override
public Person getPerson() {
Person person = personMapper.getPerson();
return person;
}
}
8、编写controller
Controller.java
package com.example.controller;
import com.example.entity.Person;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@Autowired
private PersonService personService;
@GetMapping("")
public Person getPerson(){
Person person = personService.getPerson();
return person;
}
}
9、启动项目访问地址即可
10、注意事项
- 各个类的注解需要检查是否齐全数据库url可以先使用idea右侧的Database测试一下是否连接成功数据库的名称配置项是username而不是name,曾经因为这个找不不少时间!!!



