本文共 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(){ Listall = customerDao.findAll(); for(Customer c : all) System.out.println(c); }}
转载地址:http://jzpqb.baihongyu.com/