二、创建Demo项目org.springframework.boot spring-boot-parent 2.1.2.RELEASE org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 mysql mysql-connector-java com.alibaba druid-spring-boot-starter 1.2.8 org.springframework.boot spring-boot-devtools src/main/java **/*.xml false
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/getDept")
public List getDeptInfo(){
return deptService.getDeptInfo();
}
}
public interface DeptService {
List getDeptInfo();
}
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List getDeptInfo() {
return deptMapper.getDeptInfo();
}
}
@Mapper
public interface DeptMapper {
List getDeptInfo();
}
三、添加配置文件select * from dept
配置文件
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis
driver‐class‐name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#mapper文件的映射
mapper-locations: classpath:com/tedu/server/dao/mapper/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
其中数据库的用户名、密码、url可以先在idea中尝试一下,是否能连通
选择DataSource—>mysql
出现上图连接成功的字样,则表示连接没问题。(第一次连接时,会没有测试连接的按钮,需要在选择左侧红框,添加mysql的驱动)
driver‐class‐name驱动类的值,则可以在添加的驱动依赖包中进行查看
整个文件的目录结构如下:
启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
启动main方法。
demo访问路径localhost:8080/getDept



