先吐槽下:第一天研究了一天 照着视频做就是不对 ,各种错误 然后睡一觉
第二天 MMP 好使了 不知道抽什么风。
对于程序员来说痛苦的不是学习 ,而是莫名其妙的错误。
数据库(和Mybatis一样)
然后是POM文件 ,添加一个jpa依赖,当然也得需要mysql的支持
之前看了好多 现在数据库真的五花八门 , 什么h2, HSQLD 等等内存型数据库
无力吐槽 (听说是轻量级)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
目录结构和上一个差不多
控制层
Controller->HelloController
package com.demo.Controller;
import com.demo.Model.Girl;
import com.demo.Respository.GirlRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private GirlRespository girlRespository;
@GetMapping("/id")
public Girl girl(Integer id){
Girl girl = girlRespository.findOne(id);
return girl;
}
}
然后是映射层 感觉跟mybatis没什么区别
也是接口类型
Respository->GirlRespository
package com.demo.Respository;
import com.demo.Model.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GirlRespository extends JpaRepository<Girl,Integer> {
}
这里说明下 JpaRepository会提供一些方法 也就是说到时候咱们继承之后直接就可以用 框架会提供实现
@Override
List<Girl> findAll();
@Override
List<Girl> findAll(Sort sort);
@Override
List<Girl> findAll(Iterable<Integer> iterable);
@Override
<S extends T> List<S> save(Iterable<S> iterable);
@Override
void flush();
@Override
<S extends T> S saveAndFlush(S s);
@Override
void deleteInBatch(Iterable<Girl> iterable);
@Override
void deleteAllInBatch();
@Override
Girl getOne(Integer integer);
@Override
<S extends T> List<S> findAll(Example<S> example);
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
@Override
Page<Girl> findAll(Pageable pageable);
@Override
<S extends T> S save(S s);
@Override
Girl findOne(Integer integer);
@Override
boolean exists(Integer integer);
@Override
long count();
@Override
void delete(Integer integer);
@Override
void delete(Girl girl);
@Override
void delete(Iterable<? extends Girl> iterable);
@Override
void deleteAll();
@Override
<S extends T> S findOne(Example<S> example);
@Override
<S extends T> Page<S> findAll(Example<S> example, Pageable pageable);
@Override
<S extends T> long count(Example<S> example);
@Override
<S extends T> boolean exists(Example<S> example);
启动项
不需要更改
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
数据模型层 这个有点区别(我没学过hibernate)
package com.demo.Model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "girl")
public class Girl {
public Girl() {
}
@Id
@GeneratedValue
private Integer id;
@Column(name = "use_name")
private String userName;
@Column(name = "cup_size")
private char cupSize;
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 char getCupSize() {
return cupSize;
}
public void setCupSize(char cupSize) {
this.cupSize = cupSize;
}
@Override
public String toString() {
return "Girl{" +
"id=" + id +
", userName='" + userName + '\'' +
", cupSize=" + cupSize +
'}';
}
}
@Entity(name = "girl") 告诉这个类你映射的是哪个标
public Girl() { } 需要创建一个无参构造器 否则会出错误(什么错误我不知道)
主键ID上添加 @Id @GeneratedValue这两个注解 一个是标记Id 一个是自增长
其他列上 需要注解 @Column 好像默认驼峰式命名方法 映射到数据库里变成下划线
ex: userName -> user_name;
如果想不一致 那么@Column(name = "use_name" ) name是告诉实体类 表中字段叫什么名字
清理下maven 重新编译
然后基本上可以运行了