1.搭建简单的springBoot项目
1.创建maven项目
2.引入依赖(父工程坐标+web启动器+...)
<?xml version="1.0" encoding="UTF-8"?>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">
4.0.0
cn.itcast.springboot
hello-springboot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.3.8.RELEASE
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
3.配置application.yaml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: 1234
logging:
level:
cn.itcast: debug
org.springframework: debug
server:
port: 80
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# mybatis
type-aliases-package: com.it.springBoot.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mapper-locations: classpath:mappers/*.xml
4.创建启动类SpringBootStartApplication
5.resource
package com.it.springBoot.resource;
import com.it.springBoot.pojo.UserDto;
import com.it.springBoot.service.impl.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserResource {
@Autowired
private UserService userService;
// http://localhost//getUserName/7654
@GetMapping("/getUserName/{empno}")
public String queryEnameByEmpno(@PathVariable String empno) {
String user = userService.queryEnameByEmpno(empno);
return user;
}
// http://localhost//getUserInfo
@GetMapping("/getUserInfo")
public List getUserInfo() {
List users = userService.getUserInfo();
return users;
}
}
6.service接口
package com.it.springBoot.service.impl;
import com.it.springBoot.pojo.UserDto;
import java.util.List;
public interface UserService {
String queryEnameByEmpno(String empno);
List getUserInfo();
}
7.service接口实现类
package com.it.springBoot.service.impl;
import com.it.springBoot.mapper.UserMapper;
import com.it.springBoot.pojo.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public String queryEnameByEmpno(String empno) {
userMapper.queryEnameByEmpno(empno);
return userMapper.queryEnameByEmpno(empno);
}
@Override
public List getUserInfo() {
List users= userMapper.getUserInfo();
return users;
}
}
8.mapper类
package com.it.springBoot.mapper;
import com.it.springBoot.pojo.UserDto;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
String queryEnameByEmpno(@Param("empno")String empno);
List getUserInfo();
}
9.xxMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
10:注意
a,service实现类上加上注解,注入到Spring容器中
@Service
public class UserServiceImpl implements UserService {
xxx
}
b,启动类加上mapper包扫描注解
@MapperScan("com.it.springBoot.mapper")//mapper包扫描
c.yml文件中加上mapper文件映射
mapper-locations: classpath:mappers/*.xml