SpringBoot2.0应用(四):SpringBoot2.0之spring-data-jpa

如何整合spring data jpa

一、pom依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
复制代码

二、添加配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
复制代码

三、建立dto对象

@Entity
public class City implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@SequenceGenerator(name = "city_generator", sequenceName = "city_sequence", initialValue = 23)
	@GeneratedValue(generator = "city_generator")
	private Long id;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private String state;

	@Column(nullable = false)
	private String country;

	@Column(nullable = false)
	private String map;
    ......
}
复制代码

四、建立操做数据的Repository对象

public interface CityRepository extends Repository<City, Long> {

	Page<City> findAll(Pageable pageable);

	Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
			String country, Pageable pageable);

	City findByNameAndCountryAllIgnoringCase(String name, String country);

}
复制代码

五、写个简单的Controller触发调用

@Controller
public class CityController {

    @Autowired
    private CityRepository cityRepository;

    @GetMapping("/")
    @ResponseBody
    @Transactional(readOnly = true)
    public void helloWorld() {
        City city = cityRepository.findByNameAndCountryAllIgnoringCase("Bath", "UK");
        System.out.println(city);

        Page<City> cityPage = cityRepository.findAll(new PageRequest(0,
                10,
                new Sort(Sort.Direction.DESC, "name")));
        System.out.println(Arrays.toString(cityPage.getContent().toArray()));
    }
}
复制代码

启动项目后访问http://localhost:8080/,控制台输出:java

Bath,Somerset,UK
[Washington,DC,USA, Tokyo,,Japan, Tel Aviv,,Israel, Sydney,New South Wales,Australia, Southampton,Hampshire,UK, San Francisco,CA,USA, Palm Bay,FL,USA, New York,NY,USA, Neuchatel,,Switzerland, Montreal,Quebec,Canada]
复制代码

到此,一个简单的SpringBoot2.0集成spring-data-jpa就完成了。 spring-data-jpa对一些简单的数据库操做进行了支持。具体的关键字以下:And,Or,Is,Equals,Between,LessThan,LessThanEqual,GreaterThan,GreaterThanEqual,After,Before,IsNull,IsNotNull,NotNull,Like,NotLike,StartingWith,EndingWith,Containing,OrderBy,Not,In,NotIn,TRUE,FALSE,IgnoreCase。spring-data-jpa对这些关键字的支持原理将在源码分析篇讲解,欢迎关注。mysql

若是有复杂一些的sql语句,依靠上面的关键字是确定不行的,因此spring-data-jpa还提供了注解用来支持自定义sql。在SQL的查询方法上面使用@Query注解,如涉及到删除和修改在须要加上@Modifying。 例:git

@Query("select c from City c where c.id = ?1")
    City queryById(long id);

    @Modifying
    @Query("update City c set c.name = ?2 where c.id = ?1")
    int updateNameById(long id, String name);
复制代码

注意:自定义sql的语句是对对象进行操做,风格和hql类似。github

SQL数据文件在源码中,源码地址:GitHubspring


本篇到此结束,若是读完以为有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

相关文章
相关标签/搜索