ideaJPA实现mysql_idea下整合springboot+spring data jpa

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

4ff25c13394a

1.png

4ff25c13394a

2.png

4ff25c13394a

3.png

4ff25c13394a

4.png

4ff25c13394a

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 {

}

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.age1

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.startDate1

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(Collectionages)

...where x.age in ?1

NotIn

findByAgeNotIn(Collectionages)

...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);

}

}

4ff25c13394a

6.png

9.浏览器测试

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

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

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

4ff25c13394a

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()

4ff25c13394a

8.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值