完成SSH项目 -- 实现dao层

现在web02项目有了controller 和 service 但还没有dao层,接下来我们就整合dao层

1:配置数据源 ---  使用c3p0数据源

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"         
            destroy-method="close">        
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>        
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/oos?useUnicode=true&characterEncoding=utf-8"/>        
        <property name="user" value="root"/>        
        <property name="password" value="root"/>        
    </bean>
这里有个问题就是什么是数据源,数据源是干什么的?

什么是数据源?

答:数据源定义的是连接到实际数据库的一条路径而已,数据源中并无真正的数据,它仅仅记录的是你连接到哪个数据库,以及如何连接的,如odbc数据源。也就是说数据源仅仅是数据库的连接名称,一个数据库可以有多个数据源连接。                                                                                                                              

       在Java语言中,DataSource对象就是一个代表数据源实体的对象。一个数据源就是一个用来存储数据的工具,它可以是复杂的大型企业级数据库,也可以是简单得只有行和列的文件。数据源可以位于在服务器端,也可以位于客服端


maven项目在整合dataSource和entityManagerFactory时只需要向pom.xml文件中加入

    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.9.1.RELEASE</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.1.0.Final</version>
</dependency>
就可以了,依赖jar会自动下载


下面是dao层配置

<?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:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.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"/> -->
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/oos?useUnicode=true&characterEncoding=utf-8" />
		<property name="user" value="root" />
		<property name="password" value="root" />
		
		<property name="initialPoolSize" value="5"/>
		<property name="maxPoolSize" value="10"/>
	</bean>
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    	<property name="dataSource" ref="dataSource" />  
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />  
        <property name="packagesToScan" value="com.web04.entity" />  
  
        <property name="jpaProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.max_fetch_depth">3</prop>  
                <prop key="hibernate.jdbc.fetch_size">18</prop>  
                <prop key="hibernate.jdbc.batch_size">10</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="javax.persistence.validation.mode">none</prop>  
            </props>  
        </property>  
    </bean>
     
    <!-- 用于设置JPA实现厂商的特定属性 -->  
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

	<!-- Jpa 事务管理器  -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

只需要在加上spring包扫描就可以完成service层






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值