在搭建得我环境的会
<?xml version="1.0" encoding="UTF-8"?><!--spring 和 spring data jpa的配置-->
<!-- 1.创建entityManagerFactory对象交给spring容器管理-->
<bean id="entityManagerFactoty" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--配置的扫描的包(实体类所在的包) -->
<property name="packagesToScan" value="cn.itcast.domain" />
<!-- jpa的实现厂家 -->
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
</property>
<!--jpa的供应商适配器 -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--配置是否自动创建数据库表 -->
<property name="generateDdl" value="false" />
<!--指定数据库类型 :源码里的 是enum 这样设置是 不可改动 -->
<property name="database" value="MYSQL" />
<!--数据库方言:支持的特有语法 其他的 找到的就可以-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<!--是否显示sql -->
<property name="showSql" value="true" />
</bean>
</property>
<!--jpa的方言 :高级的特性 -->
<property name="jpaDialect" >
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<!--2.创建数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql:///jpa" ></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--3.整合spring dataJpa-->
<jpa:repositories base-package="cn.itcast.dao" transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactoty" ></jpa:repositories>
<!--4.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoty"></property>
</bean>
<!-- 4.txAdvice-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 5.aop-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* cn.itcast.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!--5.声明式事务 -->
<!-- 6.注解 里的 配置包扫描-->
<context:component-scan base-package="cn.itcast" ></context:component-scan>
依据表 构建实体类 并 使用注解 俩完成 实体类 与 数据库 表字段 的关联
package cn.itcast.domain;
import javax.persistence.*;//一定导的 包
/**
- 1.实体类和表的映射关系
-
@Eitity
-
@Table
- 2.类中属性和表中字段的映射关系
-
@Id
-
@GeneratedValue
-
@Column
*/
@Entity
@Table(name=“cst_customer”)
public class Customer {
///可以 将 表里 内容字段的 复制到这里
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name=“cust_id”)
private Long custId;
@Column(name=“cust_address”)
private String custAddress;
@Column(name=“cust_industry”)
private String custIndustry;
@Column(name=“cust_level”)
private String custLevel;
@Column(name=“cust_name”)
private String custName;
@Column(name=“cust_phone”)
private String custPhone;
@Column(name=“cust_source”)
private String custSource;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
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 getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custAddress='" + custAddress + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custName='" + custName + '\'' +
", custPhone='" + custPhone + '\'' +
", custSource='" + custSource + '\'' +
'}';
}
}
开始测试
package cn.itcast.test;
import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;
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;
/**
-
编者:封红卫
-
@Classname CustomerDaoTest
-
@Description TODO
-
@Date 2019/2/13 18:46
-
@Created by Administrator
*/
@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境
@ContextConfiguration(locations = “classpath:applicationContext.xml”)//指定spring容器的配置信息
public class CustomerDaoTest {
@Autowired//注入一个
private CustomerDao customerDao;/**
- 根据id查询
/
@Test
public void testFindOne() {
// 这时 有质疑我的饿 是: 只有2个 接口的 实现类 的 就会 有 方法???
// 其实以封装 了 好多的 方法
Customer customer = customerDao.findOne((long) 1);
System.out.println(customer);
}
}
效果如下图-----------------------------------
/* - save : 保存或者更新
-
根据传递的对象是否存在主键id,
-
如果没有id主键属性:保存
-
存在id主键属性,根据id查询数据,更新数据
*/
@Test
public void testSave() {
Customer customer = new Customer();
customer.setCustName(“封红卫程序员”);
customer.setCustLevel(“fhw”);
customer.setCustIndustry(“it与java”);
customerDao.save(customer);
}
效果 如下图-----------------------------------
接着 我们 进行更新 的update 操作 就会发现 save 在保存的时候 也是更新 ,是的@Test
public void testUpdate() {
Customer customer = new Customer();
customer.setCustId((long) 1);
customer.setCustName(“封红卫程序员在努力”);
customer.setCustLevel(“fhw!!!”);
customer.setCustIndustry(“it与java!!!”);
customerDao.save(customer);
}
效果 如下图-----------------------------------
- 根据id查询
测试删除的 操作
@Test
public void testDalete(){
customerDao.delete((long)1);
}
效果 如下图-----------------------------------
查询所有 的
@Test
public void testFindall(){
List<Customer> list = customerDao.findAll();
for (Customer customer : list) {
System.out.println(customer);
}
}
效果 如下图-----------------------------------
========= 总结 一下的时候:;
1: findOne(id):根据id查询 的
2: save(Customer): 保存或更新 (依据 出汗低的 实体类里 是否包含 id 的 属性 : 没有就 保存 有就更新 )
3: delete(id): 根据id 的 删除 的
4: findAll (): 查询全部 的
package cn.itcast.test;
import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;
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 java.util.List;
/**
-
编者:封红卫
-
@Classname CustomerDaoTest
-
@Description TODO
-
@Date 2019/2/13 18:46
-
@Created by Administrator
*/
@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境
@ContextConfiguration(locations = “classpath:applicationContext.xml”)//指定spring容器的配置信息
public class CustomerDaoTest {
@Autowired//注入一个
private CustomerDao customerDao;/**
- 根据id查询
*/
@Test
public void testFindOne() {
// 这时 有质疑的 是: 只有2个 接口的 实现类 的 就会 有 方法???
// 其实以封装 了 好多的 方法
Customer customer = customerDao.findOne((long) 1);
System.out.println(customer);
}
/**
- save : 保存或者更新
-
根据传递的对象是否存在主键id,
-
如果没有id主键属性:保存
-
存在id主键属性,根据id查询数据,更新数据
/
@Test
public void testSave() {
Customer customer = new Customer();
customer.setCustName(“封红卫程序员”);
customer.setCustLevel(“fhw”);
customer.setCustIndustry(“it与java”);
customerDao.save(customer);
}
@Test
public void testUpdate() {
Customer customer = new Customer();
customer.setCustId((long) 1);
customer.setCustName(“封红卫程序员在努力”);
customer.setCustLevel(“fhw!!!”);
customer.setCustIndustry(“it与java!!!”);
customerDao.save(customer);
}
@Test
public void testDalete(){
customerDao.delete((long)1);
}
/*- 查询所有
*/
@Test
public void testFindall(){
List list = customerDao.findAll();
for (Customer customer : list) {
System.out.println(customer);
}
}
- 根据id查询
}