介绍
开发java web项目的小伙伴们都知道,如果用原生态的来开发分分钟让自己有想死的感觉,总是写一大堆类似的代码。幸好现在有很多非常优秀的框架可以大大方便我们项目的开发,比如spring,springmvc,mybatis,hibernate等等 。虽然框架可以大大方便我们开发的速度,但有一个很多小伙伴都无法忍受的,就是大量的xml配置,特别对于初学者来说,这无疑是一个噩梦。自古乱世出英雄,spring boot就此横空出世。spring boot相当于高配版的springmvc,它不用我们自己搭建环境,只需少量的代码就可以直接进行业务模块的开发。小伙伴们是不是惊呆了,下面有一个小demo是springboot集成spring data jpa可以供初学者学习。
项目结构
本人推荐使用IntellijlDEA这款IDE,直接创建springboot项目就可以了,这里我就不介绍每一步如何创建了,百度是我们最好的老师。
这个是spring boot项目的入口类,创建好springboot项目后IDE会自动生成该类,要运行时直接右键运行可以了。
package com.springbootjpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootjpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootjpaApplication.class, args);
}
}
创建好项目好IDE会自动生成pom.xml文件,并自动填写好一些依赖
<?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.springbootjpa</groupId>
<artifactId>springbootjpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootjpa</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.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</artifactId>
</dependency>
<!--这个是编写spring data jpa时需要自己补充的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这个是controller
package com.springbootjpa.controller;
import com.springbootjpa.entity.User;
import com.springbootjpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by haitang on 2017/5/15.
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/find")
public User findUser(){
System.out..println("成功了");
return userService.findUserById();//这里暂时先不管它
}
@RequestMapping("/")
public void insert(){
User user = new User(10,"aaa","ccc","ddd");
userService.insertUser(user);//这里暂时先不管它
}
}
现在就可以直接运行,在浏览器输入http://localhost:8080/find进行测试。是不是很方便啊。
下面继续介绍如何集成spring data jpa
先创建一个实体类User,对应数据库的一张表
User类
package com.springbootjpa.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* Created by haitang on 2017/5/15.
*/
@Entity
public class User {
@Id
@GeneratedValue
private int userId;
private String name;
private String password;
private String phone;
public User(){
}
//这里省略getter和setter方法
}
UserService类
package com.springbootjpa.service;
import com.springbootjpa.dao.UserDao;
import com.springbootjpa.entity.User;
import com.springbootjpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by haitang on 2017/5/15.
*/
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findUserById(){
return userRepository.findOne(3);
}
public void insertUser(User user){
userRepository.save(user);
}
}
关键地方来了,Repository就是所谓dao层,这里只需要编写一个接口继承JpaRepository接口就可以了,不需要写实现类
package com.springbootjpa.repository;
import com.springbootjpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by haitang on 2017/5/15.
*/
public interface UserRepository extends JpaRepository<User,Integer> {
}
JpaRepository接口已经自带一系列操作
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.data.jpa.repository;
import java.io.Serializable;
import java.util.List;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
List<T> findAll();
List<T> findAll(Sort var1);
List<T> findAll(Iterable<ID> var1);
<S extends T> List<S> save(Iterable<S> var1);
void flush();
<S extends T> S saveAndFlush(S var1);
void deleteInBatch(Iterable<T> var1);
void deleteAllInBatch();
T getOne(ID var1);
<S extends T> List<S> findAll(Example<S> var1);
<S extends T> List<S> findAll(Example<S> var1, Sort var2);
}
最后在application.properties里写数据库的一些配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/数据库名
spring.datasource.username=你的用户名
spring.datasource.password=对应的密码
现在你可以运行项目进行测试了