IDEA spring-boot jpa jsp 框架搭建(二)
来源:互联网 发布:nga npc数据库 编辑:程序博客网 时间:2024/06/05 19:29
IDEA spring-boot jpa jsp 框架搭建(二)
上一节讲解了如何搭建一个spring-boot框架,今天来介绍一下jpa的相关知识。
jpa
我使用jpa最主要的一点就是由于它不用在数据库中创建数据表,只要你把对应的关系,数据表的描述在你的实体类中写好,启动spring-boot数据库就会自动生成好。
添加jpa相关的配置
需要在application.properties文件中添加以下代码:
########################################################### Java Persistence Api -- Spring jpa 的配置信息#其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构,有四个值#create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。#create-drop :每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。#update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。#validate :每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。#dialect 主要是指定生成表名的存储引擎为InneoDB#show-sql 是否打印出自动生产的SQL,方便调试的时候查看########################################################spring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialectspring.jpa.show-sql= true
jpa 使用
分包创建实体类、启动测试
我们再次新建两个包,分别命名为 entity 和 repository,这两个包用于存放一些实体类和数据库操作的接口。
在entity包中创建实体类 Userinfo ,类中具体内容如下:
package cn.itlaobing.web.entity;import javax.persistence.*;import javax.print.attribute.standard.MediaSize;@Entity@Table(name="userinfos")public class Userinfo { @Id @GeneratedValue @Column(name = "id") private Integer id; // 主键 @Column(name = "user_name") private String username; //用户名 @Column(name = "user_pass") private String userpass; //密码 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; }}
解释一下代码中的注解:
@Entity : 这个注解代表这个Userinfo是一个实体类,也就是说,当你创建实体类时,需要加上@Entity 标志着此类是实体类;
@Table(name=”userinfos”) :这个注解代表对应表的名称,userinfos就是利用jpa要生成的表名称;
@id :表明此属性是主键;
@GeneratedValue :表示的是 主键生成策略,默认主键是自增长的,当然你也可以使用 uuid
@Column :表明此属性在数据库中对应的字段,如果实体中的属性与数据库中的字段一样,可以省略不写。
3. 测试一下,启动SpringBootJpaApplication,查看数据库是否生成userinfos表。
数据库已经成功创建了userinfos表。
再看看其中的字段是否一致
好的,字段也是一致的。
继承 jpa 相关操作接口,实现增删改查
- 下面我们来实现数据的增删改查,在repository包中新建一个UserinfoRepository接口 并且继承JpaRepository,具体代码如下:
package cn.itlaobing.web.repository;import cn.itlaobing.web.entity.Userinfo;import org.springframework.data.jpa.repository.JpaRepository;public interface UserinfoRepository extends JpaRepository<Userinfo,Integer> {}
解释一下:
JpaRepository 这个里面已经写好了一些增删改查的方法,我们只需要调用就行了。
编写测试用例
新建一个测试用例:
新建好的测试用例在test包里。
编写测试配置:
打开UserinfoRepositoryTest.java 需要引用一些配置:
package cn.itlaobing.web.repository;import cn.itlaobing.SpringBootJpaApplication;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)//例如你的main方法的application是xxxApplication.java,classes就要等于xxxApplication.class@SpringBootTest(classes = SpringBootJpaApplication.class)public class UserinfoRepositoryTest {}
编写测试方法:
- 增
新建一个方法 save(),测试向数据库中添加数据,代码用法已在注释中表明;
package cn.itlaobing.web.repository;import cn.itlaobing.SpringBootJpaApplication;import cn.itlaobing.web.entity.Userinfo;import org.junit.Assert;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringBootJpaApplication.class)public class UserinfoRepositoryTest { /* * 添加 @Autowired * 自动注入UserinfoRepository * */ @Autowired private UserinfoRepository userinfoRepository; /* * 向数据库中增加一条数据 * @Test 代表这是一个测试方法 * */ @Test public void save(){ //创建对象并实例化 Userinfo userinfo = new Userinfo(); userinfo.setUsername("admin"); userinfo.setUserpass("123456789"); //调用JPA中的save方法,向数据库中增加一条数据,返回一个对象 Userinfo us = userinfoRepository.save(userinfo); //断言 Assert.assertEquals(us.getUsername(),"admin"); }}
执行这个方法:
测试结果:
查看数据库是否成功增加一条数据:
数据已经成功保存到数据库里面了。
- 删
在新建一个delete()方法,测试删除数据库中的数据,代码用法已在注释中表明
package cn.itlaobing.web.repository;import cn.itlaobing.SpringBootJpaApplication;import cn.itlaobing.web.entity.Userinfo;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringBootJpaApplication.class)public class UserinfoRepositoryTest { /* * 添加 @Autowired * 自动注入UserinfoRepository * */ @Autowired private UserinfoRepository userinfoRepository; /* * 删除数据库的一条数据 * */ @Test public void delete(){ //调用delete方法,根据id删除 userinfoRepository.delete(1); }}
执行删除的方法
已成功删除!
- 改
由于刚刚删掉了,在改之前,我们需要添加一批数据;我就不在演示添加数据了,直接开始改;
创建一个update()方法,测试修改数据库中的数据;
但JpaRepository中并没有关于修改的方法,所以需要我们自己创建一个修改的方法;
打开我们自己的UserinfoRepository接口,创建一个修改的方法;
package cn.itlaobing.web.repository;import cn.itlaobing.web.entity.Userinfo;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.Modifying;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.query.Param;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;public interface UserinfoRepository extends JpaRepository<Userinfo,Integer> { @Transactional @Modifying @Query("update Userinfo u set u.userpass = ?1 where u.id = ?2") int updateById(String userpass, int id);}
代码分析:
自定义的修改方法,必须加上
@Transactional 开启事务
@Modifying 修改方法专用注解
这两个注解
@Query(“update Userinfo u set u.userpass = ?1 where u.id = ?2”)
此注解书写JPql语句 其中 Userinfo 是实体类名,userpass 和 id 是实体类的属性名,?1代表是第一个参数 ?2是第二个参数。
测试用例:在测试类中新建一个update()方法,测试更新数据库中的数据,代码用法已在注释中表明
package cn.itlaobing.web.repository;import cn.itlaobing.SpringBootJpaApplication;import cn.itlaobing.web.entity.Userinfo;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringBootJpaApplication.class)public class UserinfoRepositoryTest { /* * 添加 @Autowired * 自动注入UserinfoRepository * */ @Autowired private UserinfoRepository userinfoRepository; @Test public void update(){ userinfoRepository.updateById("admin123",5); }}
运行结果:
- 查
在测试类中新建一个select()方法,测试查询数据库中的数据,代码用法已在注释中表明
package cn.itlaobing.web.repository;import cn.itlaobing.SpringBootJpaApplication;import cn.itlaobing.web.entity.Userinfo;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringBootJpaApplication.class)public class UserinfoRepositoryTest { /* * 添加 @Autowired * 自动注入UserinfoRepository * */ @Autowired private UserinfoRepository userinfoRepository; @Test public void select(){ //第一种查询 返回List集合 List<Userinfo> lists = userinfoRepository.findAll(); for (Userinfo us: lists) { System.out.println(">>>>>>>>>>>>>>>>>>第一种,用户名是:"+us.getUsername()); } //第二种查询 根据id 查询 返回实体 Userinfo uinfo = userinfoRepository.findOne(5); System.out.println(">>>>>>>>>>>>>>>>>>>>>第二种,用户名是"+uinfo.getUsername()); }}
测试结果:
好了,今天的spring-boot jpa 已经讲解完毕了,代码已更新到csdn ,如果有不懂得朋友,欢迎打扰。。。
未完待续。。。
- IDEA spring-boot jpa jsp 框架搭建(二)
- IDEA spring-boot jpa jsp 框架搭建(一)
- IDEA spring-boot jpa jsp 框架搭建(三)
- idea 搭建maven+spring mvc+jpa框架
- spring-boot(二) 整合jpa
- IntellJ IDEA+Spring Boot+JSP
- IDEA Spring-Boot没有JSP
- IDEA spring boot+jsp 404
- Spring Boot框架搭建
- spring boot jsp mvc jpa mysql
- Spring Boot (二) Spring Boot for JSP
- Spring Boot 之jpa(二)数据库
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- 【框架搭建】spring boot入门
- spring boot maven 框架搭建
- 在IDEA中使用spring-boot,mySql,JPA
- IDEA Spring boot工程的搭建
- Permutations
- 类的继承与封装
- Andrew Ng机器学习笔记week10 大规模机器学习
- 创业
- react-native-qiniu源码修改(实现文件上传,上传策略等)
- IDEA spring-boot jpa jsp 框架搭建(二)
- 欢迎使用CSDN-markdown编辑器
- ndk 的makefile ,加了静态参数,编译出来还是动态的,跪求指点
- c++map容器介绍
- 测试报告参考规范之测试结果和分析
- Linux下Docker部署文档
- MVP登录注册、跳转
- 欧几里得算法&&拓展欧几里得算法
- scala语法