继承父工程
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
</parent>
java 无效的源发行版 17 解决方法
xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
web场景启动器
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
设置端口
yml
server:
port: 8081
mysql驱动,没有的话在yml文件中的driver:com.mysql.cj.jdbc.Driver会报错
xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
mysql配置
yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/XXXX
username: root
password: XXXXXX
jdbc场景启动器:需要使用时JdbcTemplate导入依赖
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
测试
java
@SpringBootTest
public class MainTest {
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void test() throws SQLException {
String sql = "select * from user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
System.out.println(maps.toString());
}
}
测试结果
bash
[{id=1, username=user1, password=pass1, nickname=nick1}, {id=2, username=user2, password=pass2, nickname=nick2}]
lombok(需要和idea的lombok插件一起使用)
xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
SpringBoot整合Web、MyBatis
依赖
xml
<!--web场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Mybatis整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
配置文件
yml
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx
username: root
password: 123456
mybatis:
# xml文件路径
mapper-locations: classpath:mapper/*.xml
# 起别名
type-aliases-package: com.xxx.pojo
统一返回类
java
@Data
//返回结果类
public class R<T> {
//详细信息
private String msg;
//返回码
private Integer code;
//返回数据
private T data;
//构造方法私有化
private R(){}
public static <T> R<T> build(String msg, Integer code,T data){
R<T> r = new R<>();
r.data = data;
r.msg = msg;
r.code = code;
return r;
}
public static <T> R<T> build(ResultEnum resultEnum,T data){
R<T> r = new R<>();
r.data = data;
r.msg = resultEnum.getMsg();
r.code = resultEnum.getCode();
return r;
}
public static <T> R<T> success(){
R<T> r = R.build(ResultEnum.Success, null);
return r;
}
public static <T> R<T> fail(){
R<T> r = R.build(ResultEnum.Fail, null);
return r;
}
public static <T> R<T> success(T data){
R<T> r = R.build(ResultEnum.Success, data);
r.data = data;
return r;
}
}
返回类信息枚举类
java
@AllArgsConstructor
@Getter
public enum ResultEnum {
Success("成功",200),
Fail("失败",500);
private String msg;
private Integer code;
}
web架构
控制层
java
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
Userservice userservice;
@GetMapping("list")
public R list(){
return userservice.list();
}
}
业务层
java
//业务层接口
public interface Userservice {
R list();
}
//业务层具体实现
@Service
public class UserServiceImpl implements Userservice {
@Autowired
private UserMapper userMapper;
@Override
public R list() {
List<User> list = userMapper.list();
return R.success(list);
}
}
数据层
java
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> list();
}
也可以:
xml
<?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.xx.UserMapper">
<select id="list" resultType="user">
select * from user;
</select>
</mapper>