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表达
AndfindByLastnameAndFirstname... where x.lastname=?1 and x.firstname=?2
OrfindByLastnameOrFirstname...where x.lastname=?1 or x.firstname=?2
Is、EqualsfindByFirstname、findByFirstnameIs、findByFirstnameEquals...where x.firstname = ?1
BetweenfindByStartDateBetween..where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan...where x.age<?1
LessThanEqualfindByAgeLessThanEqual...where x.age<=?1
GreaterThanfindByAgeGreaterThan... where x.age>?1
GreaterThanEqualfindByAgeGreaterThanEqual... where x.age>=?1
AfterfindByStartDateAfter...where x.startDate>?1
BeforefindByStartDateBefore... where x.startDate<?1
isNullfindByAgeIsNull...where x.age is null
IsNotNull,NotNullfindByAge(Is)NotNull..where x.age not null
LikefindByFirstnameLike...where x.firstname like ?1
NotLikefindByFirstnameNotLike...where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith...where x.firstname like ?1(参数增加前缀%)
EndingWithfindByFirstnameEndingWith...where x.firstname like ?1(参数增加后缀%)
ContainingfindByFirstnameContaining...where x.firstname like ?1(参数被%包裹)
OrderByfindByAgeOrderByLastnameDesc...where x.age=?1 order by x.lastname desc
NotfindByLastnameNot... where x.lastname <> ?1
InfindByAgeIn(Collection<Age>ages)...where x.age in ?1
NotInfindByAgeNotIn(Collection<Age>ages)...where x.age not in ?1
TruefindByActiveTrue()...where x.active = true
FalsefindByActiveFalse()...where x.active = false
IgnoreCasefindByFirstnameIgnoreCase...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

 

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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值