美文网首页
idea下整合springboot+spring data jp

idea下整合springboot+spring data jp

作者: 寻找大海的鱼 | 来源:发表于2018-09-24 11:55 被阅读0次

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

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

相关文章

网友评论

      本文标题:idea下整合springboot+spring data jp

      本文链接:https://www.haomeiwen.com/subject/guvqoftx.html