新建Module
新建 resources/application.yml
spring:
datasource:
username: root
password: 13718407978
url: jdbc:mysql://localhost:3306/jda?serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
#jpa 相关配置
jpa:
hibernate:
#会根据映射实体类自动创建或更新数据表
ddl-auto: update
#控制台打印语句
show-sql: true
#指定引擎,创建表时类型为innodb 支持事务,不写默认是 myISAM引擎不支持事务
database-platform: org.hibernate.dialect.MySQL57Dialect
新建 com.iris.springboot.entity.user.java
注意不要少 set 和 get 方法
package com.iris.springboot.entity;
import javax.persistence.*;
@Entity //说明 它是和数据表映射的类
@Table(name="tb1_user") //指定对应映射的表名,省略不写默认表名与类同名 user
public class User {
@Id //指定主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //指定自增长
private Integer id;
@Column(name = "user_name", length = 5) //与数据表对应的字段
private String username;
@Column //省略不写,默认属性名即为字段名
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
新建 com.iris.springboot.dao.UserRepository.java
package com.iris.springboot.dao;
import com.iris.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
//自定义接口继承 JpaRepository,就会后crud操作且分页排序
//指定的泛型<操作的实体类,主键的泛型>
public interface UserRepository extends JpaRepository<User,Integer> {
}
新建 com.iris.springboot.controller.UserController.java
package com.iris.springboot.controller;
import com.iris.springboot.dao.UserRepository;
import com.iris.springboot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id){
Optional<User> userOptional = userRepository.findById(id);
User user = userOptional.get();
return user;
}
@GetMapping("/user")
public User add(User user){
User user2 = userRepository.save(user);
return user2;
}
}
增加事务操作
新建 com.iris.springboot.service.IUserService.java
package com.iris.springboot.service.impl;
import com.iris.springboot.dao.UserRepository;
import com.iris.springboot.entity.User;
import com.iris.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service //模拟事务管理
public class UserServiceImpl implements IUserService {
@Autowired
UserRepository userRepository;
@Transactional //当前方法开启事务管理
@Override
public Boolean addUser(User user){
//模拟转账,转出
userRepository.save(new User("1","1"));
userRepository.save(new User("12","2"));
userRepository.save(new User("123","3"));
userRepository.save(new User("1234","4"));
userRepository.save(new User("12345","5"));
//模拟转账,转入
userRepository.save(new User("123456","6"));
userRepository.save(new User("1234567","7"));
userRepository.save(user);
return null;
}
}
在控制层 com.iris.springboot.UserController.java
@Autowired
IUserService userService;
@GetMapping("/user2")
public User addUser2(User user){
userService.addUser(user);
return user;
}
开启事务
在启动类 springBootXXXApplication
添加
@EnableTransactionManagement //开启注解的事务管理