1.写出你的实体类
package com.example.demossmshiro.jpa;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.persistence.*;
import java.sql.Date;
/**
* @Author lyr
* @create 2020/2/10 22:25
*/
@Data
@Accessors(chain = true)
@Entity
// @Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String lastName;
private String gender;
private String email;
private Date birthday;
private Integer age;
private Integer isDeleted;
}
2.写接口
package com.example.demossmshiro.jpa;
import com.example.demossmshiro.jpa.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @Author lyr
* @create 2020/2/10 22:32
*/
@Repository
public interface EmployeeRespository extends JpaRepository<Employee,Integer> {
// /**
// * @return
// */
// Employee findTopByAge();
}
3.写 yml配置文件
spring:
thymeleaf:
cache: false
mode: LEGACYHTML5
datasource:
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 422525
driver-class-name: com.mysql.jdbc.Driver
jpa:
show-sql: true //日志中显示sql语句
jpa:
properties:
hibernate:
enable_lazy_load_no_trans: true
server:
port: 80
servlet:
context-path: /
4.springboot测试类
package com.example.demossmshiro;
import com.example.demossmshiro.jpa.EmployeeRespository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
class DemoSsmShiroApplicationTests {
@Autowired
EmployeeRespository employeeRespository;
@Test
void contextLoads() {
// System.out.println(employeeRespository.findTopByAge());
}
@Test
public void print()
{
System.out.println(employeeRespository.getOne(1));
}
}
如果要生成数据库表,也可以这样