yml
spring:
datasource:
username: root
password: 123
# mysql8版本以上的驱动包,需要指定一下时区
url: jdbc:mysql://127.0.0.1:3306/jpa?serverTimezone=GMT%2B8
#自动创建数据库表
jpa:
hibernate:
# 会根据映射自动,创建或跟新数据表
ddl-auto: update
# 控制台打印sql语句
show-sql: true
# 指定引擎,创建表innodb类型,如果不指定是myISAM的类型,不支持事物
database-platform: org.hibernate.dialect.MySQL57Dialect
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
entity
user
package com.example.demo.entity;
import javax.persistence.*;
/**
* jpa模型
*/
@Entity//说明他是表的映射类
@Table(name="tab_user")//指定对应的映射表名,省略不写默认是类同名
public class User {
@Id//指定主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增长
private Integer id ;
@Column(name ="user_name" , length = 5)//与表对应的字段
public String username;
@Column //省略不写默认跟属性名
public 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;
}
}
dao
UserRepository
package com.example.demo.dao;
import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/*
继承JpaRepository接口,就有crud功能
*/
//JpaRepository<实体类,主键类型>
public interface UserRepository extends JpaRepository<User,Integer> {
}
service
UserService
package com.example.demo.service;
import com.example.demo.entity.User;
public interface UserService {
Boolean addUser(User user) ;
}
service
UserServiceImpl
package com.example.demo.service;
import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 模拟事物管理
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Transactional//当前方法开启事物管理
@Override
public Boolean addUser(User user) {
userRepository.save(new User("1","1"));
userRepository.save(new User("12","1"));
userRepository.save(new User("123","1"));
userRepository.save(new User("1234","1"));
userRepository.save(new User("12345","1"));
//转入模拟失败
// userRepository.save(new User("123456","1"));
// userRepository.save(new User("1234567","1"));
userRepository.save(user);
return null;
}
}
controller
UserController
package com.example.demo.Controller;
import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
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("/selectById/{id}")
public User add(@PathVariable("id") Integer id){
Optional<User> byId = userRepository.findById(id);
User user = byId.get();
return user;
}
@GetMapping("/addUser")
public User add(User user){
User save = userRepository.save(user);
return save;
}
@Autowired
UserService userService;
@GetMapping("/addUser2")
public User add2(User user) {
userService.addUser(user);
return user;
}
}
DemoApplication(主类)
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement//开启注解事物
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}