springdata+jpa 个人学习记录过程,开发步骤,教程

springdata

这篇笔记没有任何知识点,只是本人入门的学习过程,如果有错误,欢迎大家指出改进!

1、新建maven项目,导入pom依赖
依赖就不啰嗦了
2、Web.Xml 导入配置

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
      classpath*:/applicationContext.xml, classpath*:/spring-mvc.xml
   </param-value>
</context-param>

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
   </init-param>
   <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
 <servlet>
   <servlet-name>springServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/spring-mvc.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>springServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

<session-config>
   <session-timeout>120</session-timeout>
</session-config>


<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

3、配置spring-mvc.xml

<context:annotation-config />
   <!-- 自动扫描且只扫描@Controller -->
   <context:component-scan base-package="com.scq.controller" >
   </context:component-scan>
   <mvc:annotation-driven>
      <mvc:message-converters register-defaults="true">
         <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8" />
            <property name="supportedMediaTypes">
               <list>
                  <value>text/plain;charset=UTF-8</value>
                  <value>text/html;charset=UTF-8</value>
               </list>
            </property>
         </bean>
      </mvc:message-converters>
   </mvc:annotation-driven>
    
    <!-- 将无法mapping到Controller的path交给default servlet handler处理 -->
   <mvc:default-servlet-handler />

   <!-- 定义JSP文件的位置 -->
   <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
      <property name="contentType" value="text/html; charset=utf-8" />
   </bean>

   <!-- 定义无Controller的path<->view直接映射 -->
   <mvc:view-controller path="/" view-name="redirect:/" />
4、配置application.xml
<context:property-placeholder location="classpath*:config.properties" />
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.scq"></context:component-scan>

<!-- SAMS DB的Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!-- Persistence Unit的名称必须是defualt,JpaBaseDao基类会固定引用default值 -->
    <property name="persistenceUnitName" value="default"/>
	<!-- 定义jpa -->
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <property name="dataSource" ref="scqDataSource"/>
    <property name="packagesToScan" value="com.scq"/>
    <property name="jpaProperties">
        <props>
            <!-- 默认打开,启用cglib反射优化。 cglib是用来在Hibernate中动态生成PO字节码的, 打开优化可以加快字节码构造的速度 -->
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
            <!-- 允许查询缓存, 个别查询仍然需要被设置为可缓存的. -->
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.default_batch_fetch_size">16</prop>
        </props>
    </property>
</bean>
<bean id="hibernateJpaVendorAdapter"
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect">
    </property>
</bean>

<!-- Smartrix的Spring Data Jpa配置 -->
<jpa:repositories base-package="com.scq"
                  transaction-manager-ref="scqTransactionManager"
                  entity-manager-factory-ref="entityManagerFactory"
/>
<!-- Smartrix的Jpa 事务配置 -->
<bean id="scqTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<!-- 定义SAMS使用的annotation事务 -->
<tx:annotation-driven transaction-manager="scqTransactionManager"
                      proxy-target-class="true"/>


<!-- Smartrix DataSource -->
<bean id="scqDataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <!-- Connection Info -->
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="${datasource.jdbc.url}"/>
    <property name="username" value="${datasource.user}"/>
    <property name="password" value="${datasource.password}"/>
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0"/>
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20"/>
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0"/>
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000"/>
    <!-- <property name="validationQuery" value="select id from con_currenstatus"
        /> -->
    <property name="testOnBorrow" value="false"/>
    <property name="testOnReturn" value="false"/>
    <property name="testWhileIdle" value="true"/>
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="25200000"/>
</bean>

4、配置完我们在数据库中新建一个表格

5、People实体类

@Entity
@Table(name = "jpa_people")
public class People {
    private Integer id;
    private String lastName;
    private String email;
    private Date birth;
    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
public void setId(Integer id) {
    this.id = id;
}

@Column(name = "last_name")
public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Date getBirth() {
    return birth;
}

public void setBirth(Date birth) {
    this.birth = birth;
}

@Override
public String toString() {
    return "\nPeople{" +
            "id=" + id +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            ", birth=" + birth +
            '}';
}

}

6、再声明一个接口,和一个方法

public interface PeopleRepository extends Repository<People, Integer>{
}

7、定义一个controller

@Controller
@Transactional
public class scqcontroller {
    @Autowired
    PeopleRepository peopleRepository;
@RequestMapping("/findall")
public void findall(){
    List< People > all = peopleRepository.findAll();
    for (People ma: all) {
        System.out.println(ma);
    }
}
}

9、启动tomcat访问 localhost:8080/findall


如果有错误,希望大家指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值