Spring Boot基础教程十四:使用 SQL 关系型数据库 spring-data-jpa


1、配置数据源

=======

在pom文件中加入:

org.springframework.boot

spring-boot-starter-data-jpa

mysql

mysql-connector-java

runtime

2、Jpa 配置

========

在application-dev.properties 或者类似文件中

#开发环境

#端口配置

server.port=8080

#数据库连接配置

spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&charac

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

terEncoding=utf-8

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

JPA

spring.jpa.hibernate.ddl-auto= update

spring.jpa.show-sql=true

3、实体类

=====

@Entity

public class RoncooUserLog {

@Id

@GeneratedValue

private Integer id;

@Column

private Date createTime;

@Column

private String userName;

@Column

private String userIp;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date createTime) {

this.createTime = createTime;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserIp() {

return userIp;

}

public void setUserIp(String userIp) {

this.userIp = userIp;

}

@Override

public String toString() {

return “RoncooUserLog [id=” + id + “, createTime=” + createTime + “, userName=”

  • userName + “, userIp=” + userIp + “]”;

}

}

4、定义接口(继承 JpaRepository)

=========================

public interface RoncooUserLogDao extends JpaRepository<RoncooUserLog, Integer> {

/**

  • @param string

  • @return

*/

@Query(value = “select u from RoncooUserLog u where u.userName=?1”)

List findByUserName(String userName);

/**

  • @param string

  • @param string2

  • @return

*/

List findByUserNameAndUserIp(String string, String string2);

/**

  • @param exampl

  • @param pageable

  • @return

*/

Page findByUserName(String userName, Pageable pageable);

}

5、测试

====

@RunWith(SpringRunner.class)

@SpringBootTest

public class SpringBootDemo141ApplicationTests {

@Autowired

private RoncooUserLogDao roncooUserLogDao;

@Test

public void insert() {

RoncooUserLog entity = new RoncooUserLog();

entity.setUserName(“无境”);

entity.setUserIp(“192.168.0.1”);

entity.setCreateTime(new Date());

roncooUserLogDao.save(entity);

}

@Test

public void delete() {

roncooUserLogDao.delete(1);

}

@Test

public void update() {

RoncooUserLog entity = new RoncooUserLog();

entity.setId(2);

entity.setUserName(“无境2”);

entity.setUserIp(“192.168.0.1”);

entity.setCreateTime(new Date());

roncooUserLogDao.save(entity);

}

@Test

public void select() {

RoncooUserLog result = roncooUserLogDao.findOne(1);

System.out.println(result);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值