idea下整合springboot+spring data jpa

1.建立springboot工程,File->new->project

1.png
2.png
3.png
4.png
5.png

2.配置application.properties文件

spring.datasource.url=jdbc:mysql://localhost:3306/students?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false

3.建立数据库

建立students数据库

create database students CHARACTER SET utf8 COLLATE utf8_general_ci;

4.建立Stu实体类(实体类建立好以后,运行项目,jpa便会自动在students数据库中生成相应的stu表)

@Entity
//@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"})
@Proxy(lazy = false)    //关闭延迟加载,不然测试单元总是报错
public class Stu {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String school;

    public Stu(){
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
}

@Entity实体类里的基本注解包括有@Entity、@Table、@Id、@IdClass、@GeneratedValue、@Basic、@Transient、@Column、@Temporal、@Enumerated、@Lob

5.建立StuRepository接口

public interface StuRepository extends JpaRepository<Stu, Integer> {
}

JPA关键字列表如下所示:

关键字 示例 JPQL表达
And findByLastnameAndFirstname ... where x.lastname=?1 and x.firstname=?2
Or findByLastnameOrFirstname ...where x.lastname=?1 or x.firstname=?2
Is、Equals findByFirstname、findByFirstnameIs、findByFirstnameEquals ...where x.firstname = ?1
Between findByStartDateBetween ..where x.startDate between ?1 and ?2
LessThan findByAgeLessThan ...where x.age<?1
LessThanEqual findByAgeLessThanEqual ...where x.age<=?1
GreaterThan findByAgeGreaterThan ... where x.age>?1
GreaterThanEqual findByAgeGreaterThanEqual ... where x.age>=?1
After findByStartDateAfter ...where x.startDate>?1
Before findByStartDateBefore ... where x.startDate<?1
isNull findByAgeIsNull ...where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull ..where x.age not null
Like findByFirstnameLike ...where x.firstname like ?1
NotLike findByFirstnameNotLike ...where x.firstname not like ?1
StartingWith findByFirstnameStartingWith ...where x.firstname like ?1(参数增加前缀%)
EndingWith findByFirstnameEndingWith ...where x.firstname like ?1(参数增加后缀%)
Containing findByFirstnameContaining ...where x.firstname like ?1(参数被%包裹)
OrderBy findByAgeOrderByLastnameDesc ...where x.age=?1 order by x.lastname desc
Not findByLastnameNot ... where x.lastname <> ?1
In findByAgeIn(Collection<Age>ages) ...where x.age in ?1
NotIn findByAgeNotIn(Collection<Age>ages) ...where x.age not in ?1
True findByActiveTrue() ...where x.active = true
False findByActiveFalse() ...where x.active = false
IgnoreCase findByFirstnameIgnoreCase ...where UPPER(x.firstname)UPPER(?1)

6.建立StuDao接口以及实现类

public interface StuDao {
    Stu findOne(Integer id);
}
@Repository
public class StuDaoImpl implements StuDao {
    @Autowired
    private StuRepository stuRepository;


    @Override
    public Stu findOne(Integer id) {
        return stuRepository.getOne(id);
    }
}

7.建立service接口以及实现类

public interface StuService {
    Stu findStuById(Integer id);
}
@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuDao stuDao;

    @Override
    public Stu findStuById(Integer id) {
        return stuDao.findOne(id);
    }
}

8.建立controller层

@RestController
public class StuController {
    @Autowired
    private StuService stuService;

    @GetMapping(value = "/findStu/{id}")
    public Stu findStu(@PathVariable("id") Integer id) {
        return stuService.findStuById(id);
    }
}
6.png

9.浏览器测试

向students数据库中插入一条数据

insert stu(id, name, school)values(1,'zhangsan', 'zju');

通过TestApplication里的主函数来启动项目程序,然后在浏览器中输入


7.png

测试成功,整合完毕!

10.单元测试

若要测试stuService接口,先打开stuService界面,然后右键->Go To->Test->Create New Test,然后选中要测试的方法,idea便会在test文件下自动帮你生成一个测试的service的测试类,然后自己补充测试代码

@RunWith(SpringRunner.class)
@SpringBootTest
//如果报错就将@SpringBootTest改成下列这句话
//@SpringBootTest(classes = StuServiceTest.class)
public class StuServiceTest {
    @Autowired
    private StuService stuService;

    @Test
    public void findStuById() {
        Stu stu = stuService.findStuById(1);
        System.out.println(stu);
    }
}

右键->Run findStuById()


8.png

测试成功,信息打印在控制台里了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,038评论 2 89
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 2017年8月21日 我原本只想简单记录一下springboot中应用Jpa的简单操作。不想由于hibernate...
    行者N阅读 6,530评论 0 23
  • 风景图
    知啊知阅读 138评论 0 0