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