1. Create a new maven project
2. Add the necessary dependencies
<!--springboot的必须依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--启动springmvc的相关配置,springboot的自动配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
3. Create a new springboot startup class
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
4. Create a new application.properties in the resources and directory
#Create/update the configuration of the data table spring.jpa.hibernate.ddl-auto=update #Database address spring.datasource.url=jdbc:mysql://localhost:3306/qian?useUnicode=true&characterEncoding=utf-8 #database username spring.datasource.username=root #Database password spring.datasource.password=123
- update: Hibernate changes the database according to the given Entity structure.
- create: the database will be created every time and will not be deleted when it is closed
- none: the default setting of mysql, does not change the data structure
- create-drop: Create a database, but delete it every time the sessionFactory is closed
5. Create a new entity class User
At this time, springboot can already be started, but the data table will not be generated, because the jpa of the entity class has not been configured
First create a new user.java
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by Andy on 2018/1/20.
*/
//表明这是个需要生成数据表的类
@Entity
public class User {
// 定义主键id
@Id
// 声明一个策略通用生成器,name为”system-uuid”,策略strategy为”uuid”。
@GenericGenerator(name = "system-uuid", strategy ="uuid")
// 用generator属性指定要使用的策略生成器。
@GeneratedValue(generator = "system-uuid")
private String id;
private String name;
private Integer age;
private Boolean sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
}
At this time, when the project is started, a user data table will be generated under the specified location.
6. Implement CRUD
CrudRepository is an interface that provides common methods of adding, deleting, modifying, and checking. It is provided internally by spring, and we only need to call it.
@NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> save(Iterable<S> var1);
T findOne(ID var1);
boolean exists(ID var1);
Iterable<T> findAll();
Iterable<T> findAll(Iterable<ID> var1);
long count();
void delete(ID var1);
void delete(T var1);
void delete(Iterable<? extends T> var1);
void deleteAll();
}
New UserRepository.java
public interface UserRepository extends CrudRepository<User, String> { }
7. Implement controller control
New UserController.java
@RestController public class UserController { @Autowired private UserRepository userRepository; @RequestMapping("/add") public User add(String name){ User user = new User(); user.setName(name); return userRepository.save(user); } @RequestMapping("/list") public Iterable<User> list(){ Iterable<User> all = userRepository.findAll(); return all; } }