Hello, Spring_Data_JPA! 学习笔记(一)

=====================================================================
                    hello ,  Data JPA
=====================================================================
         --------------------------------------------------------
            1/ 配置xml
            ----------------------------------------------------
            <?xml version="1.0" encoding="UTF-8"?>
            <beans default-autowire="byName" 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"
                   xmlns="http://www.springframework.org/schema/beans"
                   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.0.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-4.1.xsd">
                <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
                <!--1/ 配置数据源-->
                <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    <property name="driverClassName" value="${driver}"></property>
                    <property name="url" value="${url}"></property>
                    <property name="username" value="${user}"></property>
                    <property name="password" value="${pwd}"></property>
                </bean>
                <!--2/ 配置 entityManager   -->
                <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                    <!--数据源-->
                    <property name="dataSource" ref="dataSource"></property>
                    <!--生成器必传-->
                    <property name="jpaVendorAdapter">
                        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
                    </property>
                    <!--扫描相应的注解文件位置-->
                    <property name="packagesToScan" value="jpatest"/>
                    <!--配置生成sql表时的流程范式-->
                    <property name="jpaProperties">
                        <props>
                            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                            <prop key="hibernate.show_sql">true</prop>
                            <prop key="hibernate.format_sql">true</prop>
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                        </props>
                    </property>
                </bean>
                <!--配置事务管理-->
                <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                        <property name="entityManagerFactory" ref="entityManagerFactory"></property>
                </bean>
                <!--配置支持注解的事务-->
                <tx:annotation-driven transaction-manager="transactionManager"/>
                <!--spring data -->
                <jpa:repositories base-package="jpatest" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
            </beans>
            ----------------------------------------------------
            2/创建实体类
            ----------------------------------------------------
            @Entity
            public class Student {
                /**
                 * @Parm 主键
                 */
                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Integer id;
                /**
                 * @Parm  学生姓名
                 */
                private String name;
                /**
                 * @Parm 学生年龄
                 */
                private Integer age;
            
                public Student() {
                }
            
                public Student(Integer id, String name, Integer age) {
                    this.id = id;
                    this.name = name;
                    this.age = age;
                }
            
                public Student(String name, Integer age) {
                    this.name = name;
                    this.age = age;
                }
            
                public Integer getId() {
                    return id;
                }
            
                public void setId(Integer id) {
                    this.id = id;
                }
            
                public String getName() {
                    return name;
                }
            
                public void setName(String name) {
                    this.name = name;
                }
            
                public Integer getAge() {
                    return age;
                }
            
                public void setAge(Integer age) {
                    this.age = age;
                }
            
                @Override
                public String toString() {
                    return "Student{" +
                            "id=" + id +
                            ", name='" + name + '\'' +
                            ", age=" + age +
                            '}';
                }
            }

            ----------------------------------------------------
            3/创建接口
            ----------------------------------------------------
                按照Spring Data的规范的规范,查询方法以find | read | get 开头,涉及查询条件时,
         条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写。
                     ------------------------------------------
                  public interface StudentDao {
                      public List<Student>  getList();
                      public boolean  saveStudent(Student student);
                  }
            ----------------------------------------------------
            4/测试
            ----------------------------------------------------
            public class StudentTest {
                private ApplicationContext applicationContext = null;
                @Before
                public void  getContext () {
                    applicationContext = new ClassPathXmlApplicationContext("beans-new.xml");
                }
                @After
                public void  close(){
                    applicationContext= null;
                }
            
                @Test
                public void EntyTest() {
                    StudentRepository studentRepository = applicationContext.getBean(StudentRepository.class);
                    System.out.println(studentRepository.getByName("zhangsan"));
                }
            ----------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值