springboot jpa 连接数据库
来源:互联网 发布:虚拟化软件esxi 编辑:程序博客网 时间:2024/06/12 02:11
1:在pom.xml 中添加依赖
<!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Spring Boot JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>2:数据库连接的配置和其它配置
spring.datasource.username=rootspring.datasource.password=123456spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-security?useUnicode=true&characterEncoding=UTF-8spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.database-platform=org.hibernate.dialect.MySQLDialectspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=update
3:创建一个实体类
@Entity@Table(name = "score")public class Score implements Serializable { private static final long serialVersionUID = 8127035730921338189L; @Id @GeneratedValue private int id; @Column(nullable = false, name="STUDENTID") // 这里说一下,我使用指定数据库列的时候,使用小写会不起作用,修改为大写便正常了。不知道为何,如果遇到一样问题的可以尝试下。 private int stuId; @Column(nullable = false, name="SUBJECTNAME") private String subjectName; @Column(nullable = false) private float score; @Column(nullable = false, name="EXAMTIME") private Date examTime; // 省去get、set 方法(占用文章空间)}4:创建一个接口然后我们继承框架为我们提供好的接口 Repository 或 CrudRepository (CrudRepository 继承自 Repository),其中为我们提供了对数据库的基本操作方法。如果你其中使用了修改、新增、删除操作,则必须要在接口上面或者对应的方法上面添加 @Transactional 注解,否则会抛出异常。
public interface IScoreDao extends CrudRepository<Score, Integer> { @Transactional @Modifying //修改和删除需要这个配置 @Query("update Score t set t.score = :score where t.id = :id") int updateScoreById(@Param("score") float score, @Param("id") int id); @Query("select t from Score t ") List<Score> getList();}5:controller层
@RestController@RequestMapping("/score")public class ScoreController { private static final Logger logger = LoggerFactory.getLogger(ScoreController.class); @Autowired private IScoreDao scoreService; @RequestMapping("/scoreList") public List<Score> getScoreList(){ logger.info("从数据库读取Score集合"); // 测试更新数据库 logger.info("更新的行数:" + scoreService.updateScoreById(88.8f, 2)); scoreService.delete(23); return scoreService.getList(); }}
阅读全文
0 0
- springboot jpa 连接数据库
- springboot JPA操作数据库
- SpringBoot JPA标准规范操作数据库
- SpringBoot整合JPA操作数据库
- SpringBoot 使用JPA操作数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- 【SpringBoot】SpringBoot 连接mysql数据库
- SpringBoot连接SQLite数据库(基于Spring-Data-Jpa和Gradle配置)
- JPA连接数据库
- jpa连接数据库
- SpringBoot-JPA进行多表连接查询
- SpringBoot之连接MySQL,JPA操作
- Springboot 之 JPA数据库操作进阶篇
- springBoot+Jpa(hibernate)数据库基本操作
- springboot jpa
- Springboot+Jpa
- springboot jpa
- JAVA深复制(深克隆)与浅复制(浅克隆)
- 体验异步的终极解决方案-ES7的Async/Await
- Python MQTT客户端实现
- 如何发布Web项目到互联网
- 下载
- springboot jpa 连接数据库
- WP-CopyRightPro 禁止复制、禁止选择、禁止引用 、保护图片
- python练习题 1 简易登录
- Dreamweaver CS5支持Html5
- UE4C++获得UMG控件
- Java 温度转换
- centos通过源码升级python版本2.7->3.6
- Win7 vs2015编译protobuf-3.0.0
- 119. Pascal's Triangle II