
1. 前言
Java Web 核心逻辑操作数据库,数据库默认优先选择 MySQL 。
SpringBoot 项目中 Java 代码操作数据库提供了常用的 2 种框架,JPA「Java Persistence API」与 MyBatis, JPA 是 Spring 家族框架中一种,简洁易用。相对而言,MyBatis 互联网公司使用较多。最为入门优先学习下 JPA 。
2. 构建依赖配置
2.1 项目构建Gradle与Maven构建,选择一种构建即可;
Gradle 构建添加依赖:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Maven 构建添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3. 创建数据库
数据库操作 GUI 界面软件: navicat 「推荐,All 平台」 与 SQLyog
使用 Navicat 软件创建 db_example
数据库;
4. Java 代码实现
4.1 application.properties 文件添加如下配置:
spring.jpa.hibernate.ddl-auto=create //the first one,after must update update config
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root //MySQL userName
spring.datasource.password=password //MySQL password
4.2 Create the @Entity
model
@Entity // This tells Hibernate to make a table out of this class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
....ignore ... getMethod and setMethod
}
4.3 Create the repository
import hello.User;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Long> {
}
4.4 Create a new controller for your Spring application
@RestController
public class MainController {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired // This means to get the bean called userRepository
//Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(value = "/add")
public String addNewUser(@RequestParam String name, @RequestParam String email){
System.out.println(name+email);
User user = new User();
user.setName(name);
user.setEmail(email);
userRepository.save(user);
logger.error("addNewUser name = "+name + "; email = "+email);
return "Save";
}
@GetMapping(value = "/all")
public Iterable<User> getAllUser(){
return userRepository.findAll();
}
}
备注::测试工具推荐,Postman
网友评论
MyBatis 提供两种方案:①注解; ②xml配置
参考文章链接:
https://www.jianshu.com/p/c2444ddd2de9
注解 GitHub 链接:
https://github.com/435242634/Spring-Boot-Demo/tree/feature/3-spring-boot-mybatis
xml 配置GitHub链接:
https://github.com/435242634/Spring-Boot-Demo/tree/feature/4-spring-boot-mybatis-xml
Mybatis 官方使用代码:
https://github.com/mybatis/spring-boot-starter/blob/e7ad640500f5b8f8be3ab5a736095015663092a5/mybatis-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/sample/mybatis/mapper/CityMapper.xml
https://spring.io/guides/gs/accessing-data-mysql/
Windows 安装参考:https://blog.csdn.net/fxbin123/article/details/80220509