SpringBoot系列-第三章 SpringData JPA(Oracle)

创建数据库

使用数据库工具创建数据库 demo

配置依赖 oracle的依赖是无法从maven下载下来的,需要自行下载

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1</version>
		</dependency>
		<!--<dependency>-->
			<!--<groupId>mysql</groupId>-->
			<!--<artifactId>mysql-connector-java</artifactId>-->
			<!--<version>5.1.46</version>-->
		<!--</dependency>-->

application.yml 配置数据源

server:
  port: 10087
spring:
  datasource:
#    driver-class-name: com.mysql.jdbc.Driver
#    username: root
#    password: root
#    url: jdbc:mysql://localhost:3306/demo?CharacterEncoding=utf8
    driver-class-name: oracle.jdbc.driver.OracleDriver
    username: demo
    password: root
    url: jdbc:oracle:thin:@172.17.13.98:1521:orcl
  jpa:
#    database: mysql
    database: oracle
    hibernate:
      ddl-auto: update
    show-sql: true

创建数据库实体 注意主键生成策略

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity //数据实体
@Table(name = "t_user") //数据库表名称
public class User {

    @Id
    @GeneratedValue //使用默认的主键生成策略GenerationType.AUTO
    private Integer id;

    private String name;

    private Integer age;

    private String city;

	private String favorite;

}

解释:@Data @AllArgsConstructor @NoArgsConstructor是Lombok插件中的注解方法,@Data可以生成getter,setter,重写tostring,equals,hashcode,@AllArgsConstructor @NoArgsConstructor无参构造,有参构造等方法,减少样板代码,增加可读性。

Dao层编码:


public interface UserRepository extends JpaRepository<User, Integer> {

}

通过继承链可以看到:

Repository 是一个普通是声明接口
CrudRepository 添加增删改查的相关方法
PagingAndSortingRepository 添加分页及排序的功能
JpaRepository 扩展了常用方法
UserRepository 自定义Repository

新建一个Dao层测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest{

    @Autowired
    private UserRepository userRepository;

    @Test
    public void save() {
        User user = new User();
        user.setName("zyf");
        user.setAge(26);
        user.setCity("北京");
		user.setFavorite("coding");
		
        userRepository.save(user);
        System.out.println(user.getId());
        //Hibernate: insert into t_user (age, city, name) values (?, ?, ?)
        Assert.assertNotNull(user.getId());
    }

    @Test
    public void find(){
        User user = userRepository.findOne(4);
        System.out.println(user);
        //Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.city as city3_0_0_, user0_.name as name4_0_0_ from t_user user0_ where user0_.id=?
        Assert.assertEquals("zyf", user.getName());
    }

    @Test
    public void delete() {
        userRepository.delete(2);
        //Hibernate: delete from t_user where id=?
    }
}

//新增
Hibernate: select hibernate_sequence.nextval from dual   //主键生成策略
Hibernate: insert into t_user (age, city, favorite, name, id) values (?, ?, ?, ?, ?)
4
~~~~ 
//查询
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.city as city3_0_0_, user0_.favorite as favorite4_0_0_, user0_.name as name5_0_0_ from t_user user0_ where user0_.id=?
User(id=4, name=zyf, age=26, city=北京, favorite=coding)

你可能感兴趣的:(SpringBoot)