目前比较常见的持久层框架(后端连接数据库的玩意)应该就那么几个:mybatis、hibernate、JPA 等 ,具体可自行查看文档(快速开始 | MyBatis-Plus (baomidou.com))
这里试试mybatisPlus,简单上手
1、添加pom依赖
如果下载依赖太慢, 更换国内maven镜像
2、application.yml 中添加数据库相关信息
spring: datasource: url: jdbc:mysql://localhost:3306/mysqldb username: root password: root
mybatis-plus:
configuration:
# MyBatis 配置
map-underscore-to-camel-case: true
global-config:
# 全局配置
db-config:
# 数据库配置
id-type: auto
3、在启动类上添加mapper扫描路径
如果不添加则需要在mapper类添加@mapper 注释
4、添加实体类和mapper类
User: @Data public class User { private Long userId; private String name; private Integer age; private String sex; }
UserMapper:
public interface UserMapper extends BaseMapper<User> { }
5、在启动类后续目录后,不要同级目录
@SpringBootTest @RunWith(SpringRunner.class) public class MybatisTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { List<User> userList = userMapper.selectList(null); userList.forEach(System.out::println); } }
测试一下