前言
我们已经学习了如何通过IDEA创建一个简单的springboot helloworld了,那么springboot如何连接mysql呢?
几种方式
Spring为各种支持的持久化技术,都提供了简单操作的模板和回调,目前大概有如下几种
ORM持久化技术
模板类
JDBC
org.springframework.jdbc.core.JdbcTemplate
Hibernate
org.springframework.orm.hibernate.HibernateTemplate
IBatis
org.springframework.orm.ibatis.SqlMapClientTemplate
JPA
org.springfrmaework.orm.jpa.JpaTemplate
目前比较通用流行的做法是使用mybatis,这里我们简单的介绍下JPA的方式,因为它使用的也不少,后续的教程很多都是基于mybatis或mybatis-plus的,所以这边不多介绍。至于其它的方式,有时间我再补上。
创建空项目
首先我们使用IDEA创建一个基础的springboot2.0的项目(创建的过程略,可以参考前几章),无需加载任何依赖(依赖还是手动加载感悟更深)。
添加依赖
编辑pom.xml:
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot