springDateJpa 的 CRUD
1...pom.xml 文件配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc</groupId> 8 <artifactId>springdatalpa01</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 <spring.version>4.2.4.RELEASE</spring.version> 17 <hibernate.version>5.0.7.Final</hibernate.version> 18 <slf4j.version>1.6.6</slf4j.version> 19 <log4j.version>1.2.12</log4j.version> 20 <c3p0.version>0.9.1.2</c3p0.version> 21 <mysql.version>5.1.6</mysql.version> 22 </properties> 23 <dependencies> 24 <dependency> 25 <groupId>junit</groupId> 26 <artifactId>junit</artifactId> 27 <version>4.12</version> 28 <scope>test</scope> 29 </dependency> 30 <!--spring start--> 31 <dependency> 32 <groupId>org.aspectj</groupId> 33 <artifactId>aspectjweaver</artifactId> 34 <version>1.6.8</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-aop</artifactId> 39 <version>${spring.version}</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-context</artifactId> 44 <version>${spring.version}</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-context-support</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-orm</artifactId> 54 <version>${spring.version}</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-beans</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-core</artifactId> 64 <version>${spring.version}</version> 65 </dependency> 66 <!--spring end--> 67 <!--hibernate start--> 68 <dependency> 69 <groupId>org.hibernate</groupId> 70 <artifactId>hibernate-core</artifactId> 71 <version>${hibernate.version}</version> 72 </dependency> 73 <dependency> 74 <groupId>org.hibernate</groupId> 75 <artifactId>hibernate-entitymanager</artifactId> 76 <version>${hibernate.version}</version> 77 </dependency> 78 <dependency> 79 <groupId>org.hibernate</groupId> 80 <artifactId>hibernate-validator</artifactId> 81 <version>5.2.1.Final</version> 82 </dependency> 83 <!--hibernate end--> 84 <dependency> 85 <groupId>c3p0</groupId> 86 <artifactId>c3p0</artifactId> 87 <version>${c3p0.version}</version> 88 </dependency> 89 <dependency> 90 <groupId>log4j</groupId> 91 <artifactId>log4j</artifactId> 92 <version>${log4j.version}</version> 93 </dependency> 94 <dependency> 95 <groupId>org.slf4j</groupId> 96 <artifactId>slf4j-api</artifactId> 97 <version>${slf4j.version}</version> 98 </dependency> 99 <dependency> 100 <groupId>org.slf4j</groupId> 101 <artifactId>slf4j-log4j12</artifactId> 102 <version>${slf4j.version}</version> 103 </dependency> 104 <dependency> 105 <groupId>mysql</groupId> 106 <artifactId>mysql-connector-java</artifactId> 107 <version>${mysql.version}</version> 108 </dependency> 109 <dependency> 110 <groupId>org.springframework.data</groupId> 111 <artifactId>spring-data-jpa</artifactId> 112 <version>1.9.0.RELEASE</version> 113 </dependency> 114 <dependency> 115 <groupId>org.springframework</groupId> 116 <artifactId>spring-test</artifactId> 117 <version>4.2.4.RELEASE</version> 118 </dependency> 119 <dependency> 120 <groupId>javax.el</groupId> 121 <artifactId>javax.el-api</artifactId> 122 <version>2.2.4</version> 123 </dependency> 124 <dependency> 125 <groupId>org.glassfish.web</groupId> 126 <artifactId>javax.el</artifactId> 127 <version>2.2.4</version> 128 </dependency> 129 </dependencies> 130 </project>
2...resources/applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/data/jpa 14 http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 15 <!--1 dataSource--> 16 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="password" value="wsc"></property> 18 <property name="user" value="root"></property> 19 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8"></property> 20 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 21 </bean> 22 <!--2 工厂类对象--> 23 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 24 <!--数据源--> 25 <property name="dataSource" ref="dataSource"></property> 26 <property name="packagesToScan" value="com.wsc.core.entity"></property> 27 <!--配置供应商适配器--> 28 <property name="jpaVendorAdapter"> 29 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 30 <property name="showSql" value="true"></property> 31 <property name="generateDdl" value="true"></property> 32 <!--是否向控制台输出语句 update hibernate.hbm2ddl.auto--> 33 <property name="database" value="MYSQL"></property> 34 </bean> 35 </property> 36 </bean> 37 <!--配置事物--> 38 <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 39 <property name="entityManagerFactory" ref="entityManagerFactory"></property> 40 </bean> 41 <tx:advice id="txAdvice" transaction-manager="jpaTransactionManager"> 42 <tx:attributes> 43 <tx:method name="save*" propagation="REQUIRED"/> 44 <tx:method name="insert*" propagation="REQUIRED"/> 45 <tx:method name="update*" propagation="REQUIRED"/> 46 <tx:method name="delete*" propagation="REQUIRED"/> 47 <tx:method name="get*" read-only="true"/> 48 <tx:method name="find*" read-only="true"/> 49 <tx:method name="*" propagation="REQUIRED"/> 50 </tx:attributes> 51 </tx:advice> 52 <aop:config> 53 <aop:pointcut id="pointcut" expression="execution(* com.wsc.core.service.*.*(..)) "/> 54 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor> 55 </aop:config> 56 <!--dao--> 57 <jpa:repositories base-package="com.wsc.core.dao" 58 transaction-manager-ref="jpaTransactionManager" 59 entity-manager-factory-ref="entityManagerFactory"> 60 </jpa:repositories> 61 </beans>
3...实体类
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 5 /** 6 * @version 1.0 7 * @ClassName Account 8 * @Description TODO 9 * @Author WSC 10 * @Date 2019/8/16 11:31 11 **/ 12 @Entity 13 @Table(name="ac_account") 14 public class Account { 15 @GeneratedValue(strategy= GenerationType.IDENTITY) 16 @Id 17 @Column(name="ac_id") 18 private int acId; 19 @Column(name="ac_address") 20 private String acAddress; 21 @Column(name="ac_name") 22 private String acName; 23 @Column(name="ac_age") 24 private int acAge; 25 26 public int getAcId() { 27 return acId; 28 } 29 30 public void setAcId(int acId) { 31 this.acId = acId; 32 } 33 34 public String getAcAddress() { 35 return acAddress; 36 } 37 38 public void setAcAddress(String acAddress) { 39 this.acAddress = acAddress; 40 } 41 42 public String getAcName() { 43 return acName; 44 } 45 46 public void setAcName(String acName) { 47 this.acName = acName; 48 } 49 50 public int getAcAge() { 51 return acAge; 52 } 53 54 public void setAcAge(int acAge) { 55 this.acAge = acAge; 56 } 57 58 @Override 59 public String toString() { 60 return "Account{" + 61 "acId=" + acId + 62 ", acAddress='" + acAddress + '\'' + 63 ", acName='" + acName + '\'' + 64 ", acAge=" + acAge + 65 '}'; 66 } 67 }
4...dao层 继承 JpaRepository接口
1 package com.wsc.core.dao; 2 3 import com.wsc.core.entity.Account; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 //实现JpaRepository接口 6 public interface AccountDao extends JpaRepository<Account,Integer> { 7 }
5...service层暂时不需要接口类;
6...test 测试类
1 package com.wsc.core.test; 2 3 import com.wsc.core.dao.AccountDao; 4 import com.wsc.core.entity.Account; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 import org.springframework.test.context.ContextConfiguration; 10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 12 /** 13 * @version 1.0 14 * @ClassName TestJPA 15 * @Description TODO 16 * @Author WSC 17 * @Date 2019/8/16 13:12 18 **/ 19 @RunWith(SpringJUnit4ClassRunner.class) 20 @ContextConfiguration("classpath:applicationContext.xml") 21 public class TestJPA { 22 @Autowired 23 private AccountDao accountDao; 24 25 /** 26 * 添加数据 27 */ 28 @Test 29 public void test01(){ 30 // ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 31 // AccountDao accountDao = applicationContext.getBean(AccountDao.class); 32 Account account = new Account(); 33 account.setAcAddress("提阿女号"); 34 account.setAcAge(133); 35 account.setAcName("zhang"); 36 this.accountDao.save(account); 37 } 38 /** 39 * 删除数据 40 */ 41 @Test 42 public void test02(){ 43 accountDao.delete(22); 44 } 45 /** 46 * 修改数据 47 */ 48 @Test 49 public void test03(){ 50 Account account = accountDao.findOne(23); 51 account.setAcName("阿凡提123"); 52 account.setAcAddress("美丽的郑州"); 53 accountDao.save(account); 54 } 55 @Test 56 public void test04(){ 57 Account one = accountDao.findOne(23); 58 System.out.println(one); 59 } 60 }
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!wished for you successed !!!