作为一个java小白(*__*) 嘻嘻,发现公司在用JPA操作数据库 ,简单的自己实现了一个Demo,不是很标准,只记录一下学习过程,毕竟第一次嘛,后面会深入的 ^_^
导入maven依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application配置
#数据库的指定及连接根据自己的改
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#jpa的配置
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
实体类创建
根据实体类自动创建表
//Table就是在数据库中创建对应的表名
@Table(name = "people")
@Entity
//不加Entity这个注解后面的创建对应的Repositery就会报错 比如
//该例子中就会报错:Error creating bean with name 'peopleRepository'
public class People implements Serializable {
private static final long serializableUID = 1L;
/**
* 设置为主键 这个注解必须有 后面的Column可以没有
*/
@Id
/** 设置主键的生成策略
* TABLE,通过一个特定的表保存主键
* SEQUENCE,通过序列产生逐渐,mysql不支持
* IDENTITY采用数据库id自增的方式自增,oracle
* AUTO;主键由程序控制
*/
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
/**
* 字段名字按照注解创建 不加这个注释,表的字段默认名字就是name
*/
@Column(name = "people_name")
private String name;
@Column(name = "people_age")
private int age;
public static long getSerializableUID() {
return serializableUID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
Repository创建
//可以继承例子中这个也可以继承 CrudRepository
@Repository
public interface PeopleRepository extends JpaRepository<People,Long> {
}
分别创建Service接口和实现类
public interface IMyService {
}
@Service
public class MyServiceImpl implements IMyService {
@Autowired
PeopleRepository peopleRepository;
public PeopleRepository getPeopleRepository() {
return peopleRepository;
}
public void setPeopleRepository(PeopleRepository peopleRepository) {
this.peopleRepository = peopleRepository;
}
}
Controller创建
@RestController
public class MyController {
@Autowired
MyServiceImpl myServiceImple;
@RequestMapping("/getAll")
public List getPeople() {
List<People> list = myServiceImple.getPeopleRepository().findAll();
return list;
}
@RequestMapping("/getListOrderById")
public List getListOrderById() {
Sort sort = Sort.by(new Sort.Order(Sort.Direction.ASC, "age"));
List list = myServiceImple.getPeopleRepository().findAll(sort);
return list;
}
@RequestMapping("/getListOrderByName")
public List getListOrderByName() {
Sort sort = Sort.by(new Sort.Order(Sort.Direction.ASC, "name"));
List list = myServiceImple.getPeopleRepository().findAll(sort);
return list;
}
@RequestMapping("/setPeople")
public People setPeople(People people) {
People save = myServiceImple.getPeopleRepository().save(people);
return save;
}
}
运行程序就发现表已经创建了:
并设置了主键及自增 通过这两个注解@Id
@GeneratedValue(strategy = GenerationType.IDENTITY):
效果:查询people跟据年龄排序
插入某一个人的效果 ^_^ 其他就不展示了 ,圆满成功舒服了