标签:star 事务 less 实现 优先 c3p0 font hibernate work
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.xuweiwei"></context:component-scan> <context:property-placeholder location="classpath*:db.properties"></context:property-placeholder> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置EntityManager --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置JPA的产品提供商 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <property name="packagesToScan" value="com.xuweiwei.springdata.entity"></property> <property name="jpaProperties"> <props> <!-- 开启驼峰命名:此属性很好,不过注意的是这个是Hibernate4.x支持的,Hibernate5.x需要修改 比如:LastName是java类中的属性,对应数据库中的表的列是last_name --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- 开启注解式的事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置Spring Data --> <!-- 加入jpa的命名空间 其中base-package:用来扫描Repository Bean所在的package --> <jpa:repositories base-package="com.xuweiwei.springdata.repository" entity-manager-factory-ref="entityManagerFactory"/> </beans>
jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///jpa
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person, Integer> { Person getByLastName(String lastName); }
package com.xuweiwei.springdata.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ @Entity @Table public class Person { private String lastName; private Integer id; private String email; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Person{" + "lastName=‘" + lastName + ‘\‘‘ + ", id=" + id + ", email=‘" + email + ‘\‘‘ + ‘}‘; } }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; static{ context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testDataSource(){ DataSource dataSource = (DataSource) context.getBean("dataSource"); System.out.println(dataSource); } @Test public void testCreateTable(){ } @Test public void testHelloWorldSpringData(){ PersonRepository personRepository = context.getBean(PersonRepository.class); Person person = personRepository.getByLastName("xuweiwei"); System.out.println(person); } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.RepositoryDefinition; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ @RepositoryDefinition(domainClass = Person.class,idClass = Integer.class) public interface PersonRepository { Person getByLastName(String lastName); }
导入jar包,或用maven或gradle构建项目。
jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///jpa
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.xuweiwei"></context:component-scan> <context:property-placeholder location="classpath*:db.properties"></context:property-placeholder> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置EntityManager --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置JPA的产品提供商 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <property name="packagesToScan" value="com.xuweiwei.springdata.entity"></property> <property name="jpaProperties"> <props> <!-- 开启驼峰命名:此属性很好,不过注意的是这个是Hibernate4.x支持的,Hibernate5.x需要修改 比如:LastName是java类中的属性,对应数据库中的表的列是last_name --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> </props> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- 开启注解式的事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置Spring Data --> <!-- 加入jpa的命名空间 其中base-package:用来扫描Repository Bean所在的package --> <jpa:repositories base-package="com.xuweiwei.springdata.repository" entity-manager-factory-ref="entityManagerFactory"/> </beans>
package com.xuweiwei.springdata.entity; import javax.persistence.*; import java.util.Date; /** * @author 许威威 * @description: * <dl> * <dt> * 注意 * </dt> * <dd> * 此处我之所以不使用@Column来自定义数据库列的名称,是因为我使用的是Hibernate的驼峰规则,在applicationContext.xml中查看到 * </dd> * </dl> * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ @Entity @Table(name="person") public class Person { private Integer id; private String lastName; private String email; private Integer age; private Date birthday; private Date createTime; @Id @GeneratedValue 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Temporal(TemporalType.TIMESTAMP) public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
package com.xuweiwei.springdata.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testCreateTable(){ } }
insert into `person`(`id`,`age`,`birthday`,`create_time`,`email`,`last_name`) values (1,8,‘2018-05-02‘,‘2018-05-11 19:30:23‘,‘aa@qq.com‘,‘sd‘), (2,34,‘2018-04-01‘,‘2018-05-11 19:30:26‘,‘dd@qq.com‘,‘dafd‘), (3,32,‘2018-04-03‘,‘2018-05-03 19:30:28‘,‘ds@qq.com‘,‘fdaf‘), (4,56,‘2018-04-05‘,‘2018-05-03 19:30:30‘,‘dadf@qq.com‘,‘fda‘), (5,34,‘2018-04-07‘,‘2018-05-11 19:30:33‘,‘vdf@qq.com‘,‘fda‘), (6,23,‘2018-01-10‘,‘2018-05-11 19:30:34‘,‘dadfd@qq.com‘,‘fda‘), (7,73,‘2017-04-10‘,‘2018-05-11 19:30:37‘,‘da@qq.com‘,‘fda‘), (8,8,‘2018-02-06‘,‘2018-05-11 19:30:39‘,‘da@qq.com‘,‘fa‘), (9,9,‘2018-04-20‘,‘2018-05-11 19:30:41‘,‘da@qq.com‘,‘fa‘), (10,33,‘2017-02-15‘,‘2018-05-11 19:30:43‘,‘fgs@qq.com‘,‘f‘), (11,22,‘2017-07-25‘,‘2018-05-11 19:30:44‘,‘da@qq.com‘,‘fda‘), (12,12,‘2016-06-22‘,‘2018-05-11 19:30:46‘,‘we@qq.com‘,‘da‘), (13,27,‘2010-06-16‘,‘2018-05-11 19:30:49‘,‘ewe@qq.com‘,‘fda‘), (14,56,‘2000-06-06‘,‘2018-05-11 19:30:51‘,‘ewe@qq.com‘,‘fda‘), (15,79,‘2009-06-09‘,‘2018-05-11 19:30:52‘,‘ewe@qq.com‘,‘dfa‘);
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * And关键字 * @param lastName * @param age * @return */ List<Person> getByLastNameAndAge(String lastName,Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testAnd(){ List<Person> persons = personRepository.getByLastNameAndAge("sd", 8); for (Person person : persons) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字 Or * @param lastName * @param email * @return */ List<Person> getByLastNameOrEmail(String lastName,String email); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testOr(){ List<Person> persons = personRepository.getByLastNameOrEmail("fa", "da@qq.com"); for (Person person : persons) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.Date; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:between * @param startTime * @param endTime * @return */ List<Person> getByBirthdayBetween(Date startTime,Date endTime); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testBetween() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date startTime = sdf.parse("2011-11-11"); List<Person> persons = personRepository.getByBirthdayBetween(startTime, new Date()); for (Person person : persons) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:LessThan * @param age * @return */ List<Person> getByAgeLessThan(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testBetween() throws ParseException { List<Person> personList = personRepository.getByAgeLessThan(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:LessThanEqual * @param age * @return */ List<Person> getByAgeLessThanEqual(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByAgeLessThanEqual(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:GreaterThan * @param age * @return */ List<Person> getByAgeGreaterThan(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByAgeGreaterThan(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:GreaterThanEqual * @param age * @return */ List<Person> getByAgeGreaterThanEqual(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByAgeGreaterThanEqual(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:Before * @param age * @return */ List<Person> getByAgeBefore(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByAgeBefore(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:Before * @param age * @return */ List<Person> getByAgeAfter(Integer age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByAgeAfter(100); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:IsNull * @param * @return */ List<Person> getByLastNameIsNull(); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByLastNameIsNull(); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:IsNotNull * @param * @return */ List<Person> getByLastNameIsNotNull(); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByLastNameIsNotNull(); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:Like * @param * @return */ List<Person> getByLastNameLike(String lastName); /** * 关键字:NotLike * @param * @return */ List<Person> getByLastNameNotLike(String lastName); /** * 关键字:StartingWith * @param * @return */ List<Person> getByLastNameStartingWith(String lastName); /** * 关键字:EndingWith * @param * @return */ List<Person> getByLastNameEndingWith(String lastName); /** * 关键字:Containing * @param * @return */ List<Person> getByLastNameContaining(String lastName); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByLastNameLike("fd"); for (Person person : personList) { System.out.println(person); } personList = personRepository.getByLastNameNotLike("fd"); for (Person person : personList) { System.out.println(person); } personList = personRepository.getByLastNameStartingWith("fd"); for (Person person : personList) { System.out.println(person); } personList = personRepository.getByLastNameEndingWith("a"); for (Person person : personList) { System.out.println(person); } personList = personRepository.getByLastNameContaining("d"); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:Not * @param * @return */ List<Person> getByLastNameNot(String lastName); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.getByLastNameNot("fa"); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { /** * 关键字:In * @param * @return */ List<Person> readByAgeIn(List<Integer> age); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.text.ParseException; import java.util.Arrays; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void testPrimary() throws ParseException { List<Person> personList = personRepository.readByAgeIn(Arrays.asList(34,9,12)); for (Person person : personList) { System.out.println(person); } } }
package com.xuweiwei.springdata.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ @Table @Entity public class Address { private Integer id; private String province; private String city; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
package com.xuweiwei.springdata.entity; import javax.persistence.*; import java.util.Date; /** * @author 许威威 * @description: * <dl> * <dt> * 注意 * </dt> * <dd> * 此处我之所以不使用@Column来自定义数据库列的名称,是因为我使用的是Hibernate的驼峰规则,在applicationContext.xml中查看到 * </dd> * </dl> * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ @Entity @Table(name="person") public class Person { private Integer id; private String lastName; private String email; private Integer age; private Date birthday; private Date createTime; private Address address; @Id @GeneratedValue 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Temporal(TemporalType.TIMESTAMP) public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @JoinColumn(name="address_id") @ManyToOne public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
package com.xuweiwei.springdata.repository; import com.xuweiwei.springdata.entity.Person; import org.springframework.data.repository.Repository; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public interface PersonRepository extends Repository<Person,Integer> { List<Person> getByAddressId(Integer id); }
package com.xuweiwei.springdata.test; import com.xuweiwei.springdata.entity.Person; import com.xuweiwei.springdata.repository.PersonRepository; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * @author 许威威 * @description: * @motto 代码虐我千百遍,我视代码如初恋 * @created date :2018-05-11 * @modified by: */ public class SpringDataTest { private static ApplicationContext context = null; private static PersonRepository personRepository = null; static { context = new ClassPathXmlApplicationContext("applicationContext.xml"); personRepository = context.getBean(PersonRepository.class); } @Test public void testCreateTable(){ } @Test public void test(){ List<Person> personList = personRepository.getByAddressId(1); for (Person person : personList) { System.out.println(person); } } }
标签:star 事务 less 实现 优先 c3p0 font hibernate work
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9024813.html