项目环境:
spring4.1.6
hibernate4.3.11
spring-data-jpa1.9.0
1. 创建一个Java Project,将jar导入到lib目录下
#spring spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-jdbc-4.1.6.RELEASE.jar spring-orm-4.1.6.RELEASE.jar spring-oxm-4.1.6.RELEASE.jar spring-test-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar #aop aopalliance-1.0.jar aspectjweaver-1.9.4.jar #hibernate antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-entitymanager-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar #spring data jpa spring-data-commons-1.11.0.RELEASE.jar spring-data-jpa-1.9.0.RELEASE.jar #slf4j slf4j-api-1.7.26.jar slf4j-log4j12-1.7.26.jar #log4j log4j-1.2.16.jar #c3p0 c3p0-0.9.2.1.jar hibernate-c3p0-4.3.11.Final.jar mchange-commons-java-0.2.3.4.jar #mysql mysql-connector-java-5.1.25.jar #commons-logging commons-logging-1.2.jar
2. 添加配置文件
(1) 在classpath下创建jdbc.properties
jdbc.url=jdbc:mysql://192.168.178.5:12345/ssm?useUnicode=true&characterEncoding=UTF-8 jdbc.driver.class=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=123456
(2) 在classpath下创建applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 读取 properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置 c3p0 数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="driverClass" value="${jdbc.driver.class}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- Spring 整合 JPA 配置 EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <!-- hibernate 相关的属性的注入 --> <!-- 配置数据库类型 --> <property name="database" value="MYSQL" /> <!-- 正向工程 自动创建表 --> <property name="generateDdl" value="true" /> <!-- 显示执行的 SQL --> <property name="showSql" value="true" /> </bean> </property> <!-- 扫描实体的包 --> <property name="packagesToScan"> <list> <value>com.linhw.demo.pojo</value> </list> </property> </bean> <!-- 配置 Hibernate 的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 配置开启注解事务处理 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置 springIOC 的注解扫描 --> <context:component-scan base-package="com.linhw.demo" /> <!-- Spring Data JPA 的配置 --> <!-- base-package:扫描 dao 接口所在的包 --> <jpa:repositories base-package="com.linhw.demo.dao" /> </beans>
3. 编写实体类
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY)//strategy=GenerationType.IDENTITY 自增长 @Column(name="user_id") private Integer userId; @Column(name="user_name") private String userName; @Column(name="age") private Integer age; public User() { } public User(Integer userId, String userName, Integer age) { this.userId = userId; this.userName = userName; this.age = age; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", age=" + age + "]"; } }
4. 编写Dao
public interface UserDao extends JpaRepository<User, Integer>{ }
5. 编写测试方法
//@RunWith是org.junit.runner.RunWith @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class UserDaoTest { @Autowired private UserDao userDao; @Test public void testSaveUser(){ User user = new User(); user.setUserName("zhangsan"); user.setAge(29); userDao.save(user); } }