package com.qx.springdata;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="jpa_persons")
public class Person {
private Integer id;
private String lastName;
private String email;
private Date birth;
@GeneratedValue(strategy=GenerationType.IDENTITY) // 按照指定的方式进行自增
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", lastName=" + lastName + ", email=" + email + ", birth=" + birth + "]";
}
}
自动生成的表:
二 正式操作 ,CRUD
2.1 单表单对象
往jpa_persons表中插入几条数据,以便测试:
在PersonRepository接口中按命名规范,定义方法:
/**
* 操作person类的接口
* 需要继承自Repository
* 参1: 代表当前操作的实体类
* 参2: 代表实体类的主键类型
* @author dell
*
*Repository是springdata的核心接口,这个接口的实现规定了spring data操作数据库的规范--命名规范
*查询是以get或者是find或者是read开头
*/
public interface PersonRepository extends Repository {
//根据名字查找
Person getByLastName(String lastName);
//查询名字以xxx开头同时id小于xxx的值
List getByLastNameStartingWithAndIdLessThan(String lastName,Integer id);
List getByLastNameEndingWithAndIdLessThan(String lastName,Integer id);
//查询对应邮件的人
List getByEmailIn(List emails);
//查询邮件在对应里面里面且id小于某个值的人
List getByEmailInAndIdLessThan(List emails,Integer id);
}
测试类中进行测试
public class TestSpringData {
private ApplicationContext context;
private PersonRepository personRepository;
@org.junit.Before //该注解含义在执行@Test注解之前先执行这个代码
public void Before(){
context=new ClassPathXmlApplicationContext("applicationContext.xml");
personRepository=context.getBean(PersonRepository.class);
System.out.println("测试前");
}
@Test
public void testHellord(){
Person person = personRepository.getByLastName("张三");
System.out.println(person);
}
@Test
public void testKeyWords(){
// List list = personRepository.getByLastNameStartingWithAndIdLessThan("张", 8);
// System.out.println(list);
// List list = personRepository.getByLastNameEndingWithAndIdLessThan("c", 8);
// System.out.println(list);
//使用 Arrays.asList(T... a)可以把传进来的一个可变参数数组快速转变成集合
List list = personRepository.getByEmailIn(Arrays.asList("[email protected]","[email protected]","[email protected]"));
System.out.println(list);
List list2=personRepository.getByEmailInAndIdLessThan(Arrays.asList("[email protected]","[email protected]","[email protected]"), 6);
System.out.println(list2);
}
}
测试testKeyWords结果:底层执行的sql语句和查询结果:
INFO: HHH000228: Running hbm2ddl schema update
测试前
七月 30, 2017 11:17:24 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
select
person0_.id as id1_0_,
person0_.birth as birth2_0_,
person0_.email as email3_0_,
person0_.lastName as lastName4_0_
from
jpa_persons person0_
where
person0_.email in (
? , ? , ?
)
[Person [id=2, lastName=bb, [email protected], birth=2017-07-01 19:31:44.0]
, Person [id=3, lastName=cc, [email protected], birth=2017-07-09 22:24:17.0]
, Person [id=6, lastName=张三, [email protected], birth=2017-07-29 22:25:45.0]
]
Hibernate:
select
person0_.id as id1_0_,
person0_.birth as birth2_0_,
person0_.email as email3_0_,
person0_.lastName as lastName4_0_
from
jpa_persons person0_
where
(
person0_.email in (
? , ? , ?
)
)
and person0_.id
[Person [id=2, lastName=bb, email=bb@163.com, birth=2017-07-01 19:31:44.0]
, Person [id=3, lastName=cc, email=cc@163.com, birth=2017-07-09 22:24:17.0]
]
List getByAddressIdGreaterThan(Integer addressId);
在定义方法时,如果对象中存在这个属性addressId,会优先使用自带的属性而不是关联数据
在Person类中增加一属性addressId, 设置getter setter方法
而Person类中有一关联属性Address,那么这次去调用方法
List getByAddressIdGreaterThan(Integer addressId);时,
会优先使用自带的属性AddressId
如果想用关联数据的,而里面刚好有一个属性是同名的,那么你需要加一个下划线分割一下.
List getByAddress_IdGreaterThan(Integer addressId); 使用_后,代表Address的id
接口中同时定义两个方法
List getByAddressIdGreaterThan(Integer addressid);//注意,如果对象中存在这个属性,会优先使用自带的属性而不是关联数据
List getByAddress_IdGreaterThan(Integer addressid);
测试类的测试方法:
//查询关联数据
@Test
public void testKeyWords2(){
List list = personRepository.getByAddressIdGreaterThan(110);
System.out.println(list);
List list2=personRepository.getByAddress_IdGreaterThan(110);
System.out.println(list2);
}
// 多态, 在JAVA中是这样用的, 其实在PHP当中可以自然消除, 因为参数是动态的, 你传什么过来都可以, 不限制类型, 直接调用类的方法
abstract class Tiger {
public abstract function climb();
}
class XTiger extends Tiger {
public function climb()
jQuery.extend({
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) {
s.error.call( s.context || s, xhr, status, e );
}
always 总是
rice 水稻,米饭
before 在...之前
live 生活,居住
usual 通常的
early 早的
begin 开始
month 月份
year 年
last 最后的
east 东方的
high 高的
far 远的
window 窗户
world 世界
than 比...更
最近使用mybatis.3.1.0时无意中碰到一个问题:
The errors below were detected when validating the file "mybatis-3-mapper.dtd" via the file "account-mapper.xml". In most cases these errors can be d