SpringDataJPA入门

1.导入坐标

 

2.配置文件springContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///jpa"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="cn.itcast.entity"/>
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false"/>
                <property name="database" value="MYSQL"/>
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
        
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    <jpa:repositories base-package="cn.itcast.dao"
                      transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory"/>


</beans>

3.dao层接口继承JpaRepository和JpaSpecificationExecutor两个接口,JpaRepository中提供了基本增删改查的方法,JpaSpecificationExecutor提供相对复杂的方法

   这样dao层连方法都不用写,就可以进行基本的增删改查

  自定义的方法还是要写方法的

 

public interface CustomerDao extends JpaRepository<Customer,Long>,JpaSpecificationExecutor<Customer> {
 @Query("from Customer")
 public List<Customer> findAllCustomer();

 @Query("from Customer where custName like ?")
 public List<Customer> findByName(String custName);

 @Query("update Customer set custName= ?2 where custId = ?1") //参数索引
 @Modifying
 public void updateCustomer(Long id,String custName);

 @Query("delete from Customer where custId = ?1")
 @Modifying
 public void deleteCustomer(Long id);

 @Query(value = "select * from cst_customer" , nativeQuery = true)
 public List<Customer> findCustomer();

 public Customer findByCustName(String custName);

 public Customer findByCustIndustryAndCustAddress(String industry,String address);
}

4.测试

  

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:springContext.xml")
public class CustomerDaoTest {
    @Autowired
    private CustomerDao customerDao;
    @Test
    public void findOne(){ //查询
        Customer one = customerDao.findOne(3l);
        System.out.println(one);
    }
    @Test
    public void save(){ //插入
        Customer customer =new Customer();
        customer.setCustName("小黑播报");
        customerDao.save(customer);
    }

    @Test
    public void update(){ //save中有id 就是更新
        Customer customer = customerDao.findOne(1l);
        customer.setCustName("新黑马");
        customerDao.save(customer);
    }

    @Test
    public void delete(){
        customerDao.delete(4l); //不能删除不存在的 会报错
    }
}

 

jpql语句

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:springContext.xml")
public class CustomerDaoJpqlTest {
    @Autowired
    private CustomerDao customerDao;
   @Test
    public void findAll(){
       List<Customer> allCustomer = customerDao.findAllCustomer();
       System.out.println(allCustomer);
   }

   @Test
    public void findByName(){
       List<Customer> byName = customerDao.findByName("%小黑%");
      System.out.println(byName);
   }

   @Test
   @Transactional
   @Rollback(value = false)
    public void update(){
       customerDao.updateCustomer(3l,"黑马放假了" );
   }

   @Test
   @Transactional
   @Rollback(value = false)
   public void delete(){
       customerDao.deleteCustomer(6l);
   }

   //sql语句查询
   @Test
    public void findAll1(){
       List<Customer> customer = customerDao.findCustomer();
       System.out.println(customer);
   }

   @Test
    public void findByCustomer(){
       Customer customer= customerDao.findByCustName("黑马程序员");
       System.out.println(customer);
   }

   @Test
    public void findByindustyandaddr(){
       Customer c = customerDao.findByCustIndustryAndCustAddress("it", "北京");
       System.out.println(c);
   }


}

 

posted on 2018-12-04 09:01  雨后黄昏  阅读(200)  评论(0编辑  收藏  举报

导航