博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JPA--SpringData JPA环境搭建及简单操作
阅读量:2443 次
发布时间:2019-05-10

本文共 7702 字,大约阅读时间需要 25 分钟。

创建工程并导入依赖

4.0.0
pers.zhang
spirngdata_jpa
1.0-SNAPSHOT
4.2.4.RELEASE
5.0.7.Final
1.6.6
1.2.12
0.9.1.2
5.1.6
junit
junit
4.9
test
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-core
${spring.version}
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-entitymanager
${hibernate.version}
org.hibernate
hibernate-validator
5.2.1.Final
c3p0
c3p0
${c3p0.version}
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
mysql
mysql-connector-java
${mysql.version}
org.springframework.data
spring-data-jpa
1.9.0.RELEASE
org.springframework
spring-test
4.2.4.RELEASE
javax.el
javax.el-api
2.2.4
org.glassfish.web
javax.el
2.2.4

创建表并准备数据

在这里插入图片描述

实体类

package pers.zhang.entity;/** * @author zhang * @date 2019/12/15 - 22:10 */import javax.persistence.*;/** *  1.实体类和表的映射关系 *      @Entity *      @Table *  2.类中属性和表中字段的映射关系 *      @Id *      @GeneratedValue *      @Colum */@Entity //声明实体类@Table(name="cst_customer") //建立实体类和表的映射关系public class Customer {
@Id//声明当前私有属性为主键 @GeneratedValue(strategy= GenerationType.IDENTITY) //配置主键的生成策略 @Column(name="cust_id") //指定和表中cust_id字段的映射关系 private Long custId; @Column(name="cust_name") //指定和表中cust_name字段的映射关系 private String custName; @Column(name="cust_source")//指定和表中cust_source字段的映射关系 private String custSource; @Column(name="cust_industry")//指定和表中cust_industry字段的映射关系 private String custIndustry; @Column(name="cust_level")//指定和表中cust_level字段的映射关系 private String custLevel; @Column(name="cust_address")//指定和表中cust_address字段的映射关系 private String custAddress; @Column(name="cust_phone")//指定和表中cust_phone字段的映射关系 private String custPhone; public Long getCustId() {
return custId; } public void setCustId(Long custId) {
this.custId = custId; } public String getCustName() {
return custName; } public void setCustName(String custName) {
this.custName = custName; } public String getCustSource() {
return custSource; } public void setCustSource(String custSource) {
this.custSource = custSource; } public String getCustIndustry() {
return custIndustry; } public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry; } public String getCustLevel() {
return custLevel; } public void setCustLevel(String custLevel) {
this.custLevel = custLevel; } public String getCustAddress() {
return custAddress; } public void setCustAddress(String custAddress) {
this.custAddress = custAddress; } public String getCustPhone() {
return custPhone; } public void setCustPhone(String custPhone) {
this.custPhone = custPhone; } @Override public String toString() {
return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; }}

创建测试类

package pers.zhang;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import pers.zhang.dao.CustomerDao;import pers.zhang.entity.Customer;import java.util.List;/** * @author zhang * @date 2019/12/15 - 22:40 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class mytest {
@Autowired private CustomerDao customerDao; /* 根据id查询 */ @Test public void testFindById(){
Customer one = customerDao.findOne(2l); System.out.println(one); } /* save:保存或更新 如果传递的对象中没有id:保存 如果传递的对象中有id:根据id查询,然后更新数据 */ @Test public void testSave(){
Customer customer = new Customer(); customer.setCustName("三星"); customer.setCustAddress("思密达"); customerDao.save(customer); } /* delete:根据id删除 */ @Test public void testDelete(){
customerDao.delete(7l); } /* findAll:查询所有 */ @Test public void testFindAll(){
List
all = customerDao.findAll(); for(Customer c : all) System.out.println(c); }}

转载地址:http://jzpqb.baihongyu.com/

你可能感兴趣的文章
mac 方向键调整_如何调整Mac方向键的速度
查看>>
在Windows Media Center中快速隐藏菜单栏
查看>>
android wi-fi_如何从Android到任何智能手机共享Wi-Fi密码
查看>>
带有Bisigi主题的Ubuntu焕然一新
查看>>
tumblr_使用Tumblr创建美丽且易于更新的博客
查看>>
使用VidCoder简化DVD到MP4的转换
查看>>
使用VLC Media Player将媒体从Windows 7流传输到XP
查看>>
书签 自动更新浏览器的书签_针对您的浏览器的英语翻译书签
查看>>
如何使用Zappy,Mac的新屏幕截图和注释工具
查看>>
火狐 dns_如何在Firefox中通过HTTPS启用DNS
查看>>
如何使用Google Chrome浏览器的新深层链接功能
查看>>
linux iso 挂载_在Linux上挂载ISO映像
查看>>
ubuntu修复eth0_在Ubuntu虚拟机中修复“无法启动eth0”
查看>>
c++命令行刷新特定行_在特定时间从命令行关闭Windows
查看>>
chrome 版本测试_如何在Google Chrome的发行版,测试版和开发版本之间切换
查看>>
使用Windows Web平台运行WordPress和其他Web应用程序
查看>>
通过这些3D Google动物和物体成为虎王
查看>>
mac关闭终端命令_如何使用终端关闭Mac
查看>>
sysinternals_批处理脚本以自动更新Sysinternals工具
查看>>
关闭windows激活提示_快速提示:关闭Windows 7中的游戏
查看>>