MavenDay02(ssh配置文件 项目拆分和聚合 nexus配置 仓库组的配置 public repositories)

  1.  
    一、回顾
  2.  
    1、什么是maven
  3.  
    是一个软件开发的管理工具,主要管理的工作是:依赖管理、项目构建
  4.  
    2、使用maven的好处
  5.  
    能够集中管理jar包,提供一键构建
  6.  
    3、maven的安装及配置
  7.  
    配置MAVEN_HOME,PATH路径配置
  8.  
    本地仓库<localRepository>
  9.  
    运行:mvn -v
  10.  
    4、常用的maven命令
  11.  
    complie,test,package,install,deploy
  12.  
    5、maven工程是具有一定的目录结构的
  13.  
    src
  14.  
    main
  15.  
    java(程序主要代码)
  16.  
    resources(配置文件)
  17.  
    test
  18.  
    java(测试代码)
  19.  
    resources(测试的配置文件)
  20.  
    pom.xml
  21.  
    6、eclipse工具下的maven工具开发
  22.  
    7、在pom.xml文件中如何引入坐标
  23.  
    <dependency>
  24.  
    <groupId>javax.servlet</groupId>
  25.  
    <artifactId>servlet-api</artifactId>
  26.  
    <version>2.5</version>
  27.  
    <scope>provided</scope>
  28.  
    </dependency>
  29.  
     
  30.  
    二、Maven工程的拆分和聚合
  31.  
    一个完整的早期开发的crm项目,现在使用maven工程对其进行拆分,可以将dao拆解出来形成表现独立的工程,同样service和action也可以拆分
  32.  
    工程拆分之后,将来还要聚合(就是将拆分的工程进行进一步组合在一起,又形成一个完整的项目)
  33.  
    为了达到聚合的目标,所以会引入
  34.  
    父工程(maven project)
  35.  
    模块(maven module)dao,service,web
  36.  
     
  37.  
    开发步骤:
  38.  
    1、创建一个maven父工程
  39.  
    创建后的父工程结构很简单
  40.  
    从目录结构可以看出,父工程本身不写代码,里面有一个pom.xml文件,这个文件可以将多个子模块中通用的jar包所对应的坐标,
  41.  
    集中在父工程中配置,将来的子模块就可以不需要在pom.xml中配置通用jar的坐标了
  42.  
     
  43.  
    packaging->pom pom打包方式用于父工程
  44.  
    2、如何创建这个父工程的一个子模块
  45.  
    maven module -》next
  46.  
    3、再次查看父工程的pom.xml文件
  47.  
    4、查看子模块的pom.xml发现多了一个parent节点
  48.  
    <parent>
  49.  
    <groupId>my.mime.ssh</groupId>
  50.  
    <artifactId>ssh_parent</artifactId>
  51.  
    <version>0.0.1-SNAPSHOT</version>
  52.  
    </parent>
  53.  
    并且内部所包含的节点,其实就是父工程的坐标
  54.  
    坐标=groupId+artifactId+version
  55.  
    组织名 项目名 版本
  56.  
    三、冲突问题
  57.  
    当一个项目依赖的构件比较多时,它们相互之间存在依赖,当需要对依赖版本统一管理时如果让maven自动来处理可能不太好
  58.  
    org.apache.struts依赖spring-beans-3.0.5,spring-context依赖spring-beans-4.2.4,但是spring-beans-3.0.5加入到工程中,我们希望spring-beans-4.2.4加入工程
  59.  
    1、通过添加<exclusion>标签来解决jar包冲突
  60.  
    在父工程中引入struts-core,hibernate-core发现jar包有冲突
  61.  
    javassist存在版本上冲突问题
  62.  
    dependenct Hierarchy->jar包上右键execlude maven artifact
  63.  
     
  64.  
    父工程的pom.xml文件中添加了 exclusions 排除了之前排除的jar包
  65.  
     
  66.  
    依赖调解原则
  67.  
    maven自动按照下面的原则调解
  68.  
    1、第一声明者优先原则
  69.  
    在pom文件定义依赖,先声明的依赖为准
  70.  
     
  71.  
    2、路径近者优先原则
  72.  
    A依赖spring-beans-4.2.4,A依赖B依赖spring-beans-3.0.5,则spring-beans-4.2.4优先被依赖在A,因为spring-beans-4.2.4相对spring-beans-3.0.5被A依赖的路径最近
  73.  
     
  74.  
    3、使用版本锁定解决冲突
  75.  
    面对众多的依赖,不用考虑依赖路径,声明优化等因素可以采用直接锁定版本的方法确定依赖构件的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本的为准添加到工程中。
  76.  
    <dependencyManagement>
  77.  
    <dependencies>
  78.  
    <dependency>
  79.  
    <groupId>org.springframework</groupId>
  80.  
    <artifactId>spring-context</artifactId>
  81.  
    <version>${spring.version}</version>
  82.  
    </dependency>
  83.  
    <dependency>
  84.  
    <groupId>org.springframework</groupId>
  85.  
    <artifactId>spring-aspects</artifactId>
  86.  
    <version>${spring.version}</version>
  87.  
    </dependency>
  88.  
    </dependencies>
  89.  
    </dependencyManagement>
  90.  
    <dependencies>
  91.  
    <dependency>
  92.  
    <groupId>org.springframework</groupId>
  93.  
    <artifactId>spring-web</artifactId>
  94.  
    <!--不需要写版本信息,因为上面已经锁定版本-->
  95.  
    </dependency>
  96.  
    </dependencies>
  97.  
     
  98.  
    在使用坐标时,对于同一个框架,引入多次时,它的版本信息就会多次出现,所以可以借用常量的思想,将这些版本号提取出来,在需要用到的时候,直接写版本的常量名称就可以了
  99.  
     
  100.  
    三、依赖关系
  101.  
    传递依赖
  102.  
    当A依赖B,B依赖C,在A中导入B后会自动导入C,C是A的传递依赖,如果C依赖D则D也可能是A的传递依赖
  103.  
     
  104.  
    依赖具有传递性,但不是无限传递的
  105.  
     
  106.  
    解决方法:
  107.  
    再导入一次坐标
  108.  
     
  109.  
    因为junit的scope为test所以传递不下去
  110.  
    如果修改junit的scope为complie 则maven-first所以来的的junit的jar包会加入到maven-web工程中
  111.  
     
  112.  
     
  113.  
    四、编写service模块
  114.  
    1、创建一个maven module项目
  115.  
    父工程中多了个ssh_service
  116.  
    父工程的pom.xml多了 <module>ssh_service</module>
  117.  
    2、在service的pom.xml文件中引入dao 的jar包
  118.  
    <dependency>
  119.  
    <groupId>my.mime.ssh</groupId>
  120.  
    <artifactId>ssh_dao</artifactId>
  121.  
    <version>0.0.1-SNAPSHOT</version>
  122.  
    </dependency>
  123.  
     
  124.  
    web层的子模块 选 <packaging>war</packaging>
  125.  
     
  126.  
    五、maven私服
  127.  
    下载nexus
  128.  
    安装
  129.  
    nexus是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构建搜索功能
  130.  
    下载地址:www.sonatype.org/nexus/archived
  131.  
    1、进入D:\nexus-2.12.0-01\bin
  132.  
    cmd输入 nexus.bat install
  133.  
    在服务中启动nexus服务
  134.  
    http://localhost:8081/nexus
  135.  
    帐号默认admin 密码默认admin123
  136.  
     
  137.  
    查看nexus的配置文件conf/nexus.properties
  138.  
     
  139.  
    # Jetty section
  140.  
    application-port=8081 #nexus的访问端口配置
  141.  
    application-host=0.0.0.0 #nexus主机监听配置(不用修改)
  142.  
    nexus-webapp=${bundleBasedir}/nexus #nexus工程目录
  143.  
    nexus-webapp-context-path=/nexus #nexus的web访问路径
  144.  
     
  145.  
    # Nexus section
  146.  
    nexus-work=${bundleBasedir}/../sonatype-work/nexus #nexus仓库目录
  147.  
    runtime=${bundleBasedir}/nexus/WEB-INF #nexus运行程序目录
  148.  
     
  149.  
    拆分就是将原有的项目分成 ssh_dao,ssh_service,ssh_web,ssh_parent
  150.  
    拆分之后,以后还要聚合(聚合就是将拆分的工程进一步组合在一起,又形成一个完整的项目)
  151.  
     
  152.  
    网站中仓库有不同的type
  153.  
    1、hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括release和snapshot两部分,Releases公司内部发布版本仓库,Snapshots公司内部测试版本仓库
  154.  
    2、proxy 代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
  155.  
    3、group,仓库组,用于合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组
  156.  
    4、virtual(虚拟),兼容Maven1版本的jar或者插件
  157.  
     
  158.  
    nexus仓库默认在sonatype-work目录中
  159.  
    central :代理仓库,代理中央仓库
  160.  
    apache-snapshots:代理仓库
  161.  
    存储snapshots构建,代理地址http://repository.apache.org/snapshots/
  162.  
    central-m1:virtual类型仓库,兼容Maven1版本的jar或者插件
  163.  
    release:本地仓库,存储release构件
  164.  
    snapshots:本地仓库,存储snapshots构件
  165.  
    thirdparty:第三方仓库
  166.  
    public:仓库组
  167.  
     
  168.  
    将ssh_dao这个工程打成jar包,放到私服上
  169.  
    配置
  170.  
    第一步:在客户端即部署dao工程的电脑上配置maven环境,并修改settings.xml文件,配置连接私服的用户和密码
  171.  
     
  172.  
    此用户名和密码用于私服校验,因为私服需要知道上传的帐号和密码是否和私服中的帐号和一致
  173.  
    <server>
  174.  
    <!--访问releases这个私服上的仓库,所使用的帐号,密码-->
  175.  
    <id>releases</id>
  176.  
    <username>admin</username>
  177.  
    <password>admin123</password>
  178.  
    </server>
  179.  
    <server>
  180.  
    <id>snapshots</id>
  181.  
    <username>admin</username>
  182.  
    <password>admin123</password>
  183.  
    </server>
  184.  
     
  185.  
     
  186.  
     
  187.  
    测试
  188.  
    将项目dao工程打成jar包发布到私服
  189.  
    1、首先启动nexus
  190.  
    2、对dao工程执行deploy命令
  191.  
     
  192.  
    根据本项目pom.xml中version决定发布到哪个仓库,如果version定义为snapshot,执行deploy后查看nexus的snapshot仓库,如果version定义为release则项目发布到nexus的release仓库,本项目发布到snapshot仓库
  193.  
    <distributionManagement>
  194.  
    <repository>
  195.  
    <id>releases</id>
  196.  
    <url>http://localhost:8081/nexus/content/repositories/releases</url>
  197.  
    </repository>
  198.  
    <snapshotRepository>
  199.  
    <id>snapshots</id>
  200.  
    <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
  201.  
    </snapshotRepository>
  202.  
    </distributionManagement>
  203.  
     
  204.  
    pom.xml中的<id>和settings.xml配置<id>对应
  205.  
    http://localhost:8081/nexus/content/repositories/snapshots/my/mime/ssh/ssh_dao/
  206.  
     
  207.  
    从私服下载jar包
  208.  
    没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载jar包,
  209.  
    如果私服没有jar包私服同时作为代理服务器从中央仓库下载jar包,这样做的好处是一方面由私服对公司项目的依赖jar包统一管理,
  210.  
    一方面提高下载速度,项目连接私服下载jar包的速度比项目连接中央仓库的速度快的多
  211.  
     
  212.  
    nexus中包括很多仓库,hosted中存放的是企业自己分布的jar包以及第三方公司的jar包,proxy中存放的是中央仓库的jar,
  213.  
    为了方便从私服下载jar包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar包。
  214.  
     
  215.  
    在客户端的setting.xml中配置私服的仓库,由于setting.xml中没有repositories的配置标签需要使用profile定义仓库
  216.  
    <profile>
  217.  
    <!--profile的id-->
  218.  
    <id>dev</id>
  219.  
    <repositories>
  220.  
    <repository>
  221.  
    <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
  222.  
    <id>nexus</id>
  223.  
    <!--仓库地址,即nexus仓库组的地址-->
  224.  
    <url>http://localhost:8081/nexus/content/groups/public/</url>
  225.  
    <!--是否下载releases构件-->
  226.  
    <releases>
  227.  
    <enabled>true</enabled>
  228.  
    </releases>
  229.  
    <!--是否下载snapshots构件-->
  230.  
    <snapshots>
  231.  
    <enabled>true</enabled>
  232.  
    </snapshots>
  233.  
    </repository>
  234.  
    </repositories>
  235.  
    <pluginRepositories>
  236.  
    <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
  237.  
    <pluginRepository>
  238.  
    <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
  239.  
    <id>public</id>
  240.  
    <name>Public Repositories</name>
  241.  
    <url>http://localhost:8081/nexus/content/groups/public/</url>
  242.  
    </pluginRepository>
  243.  
    </pluginRepositories>
  244.  
    </profile>
  245.  
     
  246.  
    使用profile定义仓库需要激活才可生效
  247.  
     
  248.  
    pom.xml中会在effective pom 即给计算机看的中自动生成
  249.  
    <repositories>
  250.  
    <repository>
  251.  
    <releases>
  252.  
    <enabled>true</enabled>
  253.  
    </releases>
  254.  
    <snapshots>
  255.  
    <enabled>true</enabled>
  256.  
    </snapshots>
  257.  
    <id>nexus</id>
  258.  
    <url>http://localhost:8081/nexus/content/groups/public/</url>
  259.  
    </repository>
  260.  
    <repository>
  261.  
    <snapshots>
  262.  
    <enabled>false</enabled>
  263.  
    </snapshots>
  264.  
    <id>central</id>
  265.  
    <name>Central Repository</name>
  266.  
    <url>https://repo.maven.apache.org/maven2</url>
  267.  
    </repository>
  268.  
    </repositories>
  •  

这里写图片描述 
settings.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5.  
    <localRepository>D:\Repositories</localRepository>
  6.  
    <pluginGroups>
  7.  
    </pluginGroups>
  8.  
    <proxies>
  9.  
    </proxies>
  10.  
    <servers>
  11.  
     
  12.  
    <server>
  13.  
    <id>releases</id>
  14.  
    <username>admin</username>
  15.  
    <password>admin123</password>
  16.  
    </server>
  17.  
    <server>
  18.  
    <id>snapshots</id>
  19.  
    <username>admin</username>
  20.  
    <password>admin123</password>
  21.  
    </server>
  22.  
    </servers>
  23.  
     
  24.  
    <mirrors>
  25.  
     
  26.  
    <mirror>
  27.  
    <id>nexus-aliyun</id>
  28.  
    <mirrorOf>central</mirrorOf>
  29.  
    <name>Nexus aliyun</name>
  30.  
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
  31.  
    </mirror>
  32.  
    </mirrors>
  33.  
     
  34.  
    <profiles>
  35.  
     
  36.  
    <profile>
  37.  
    <!--profile的id-->
  38.  
    <id>dev</id>
  39.  
    <repositories>
  40.  
    <repository>
  41.  
    <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
  42.  
    <id>nexus</id>
  43.  
    <!--仓库地址,即nexus仓库组的地址-->
  44.  
    <url>http://localhost:8081/nexus/content/groups/public/</url>
  45.  
    <!--是否下载releases构件-->
  46.  
    <releases>
  47.  
    <enabled>true</enabled>
  48.  
    </releases>
  49.  
    <!--是否下载snapshots构件-->
  50.  
    <snapshots>
  51.  
    <enabled>true</enabled>
  52.  
    </snapshots>
  53.  
    </repository>
  54.  
    </repositories>
  55.  
    <pluginRepositories>
  56.  
    <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
  57.  
    <pluginRepository>
  58.  
    <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
  59.  
    <id>public</id>
  60.  
    <name>Public Repositories</name>
  61.  
    <url>http://localhost:8081/nexus/content/groups/public/</url>
  62.  
    </pluginRepository>
  63.  
    </pluginRepositories>
  64.  
    </profile>
  65.  
     
  66.  
    </profiles>
  67.  
    <activeProfiles>
  68.  
    <activeProfile>dev</activeProfile>
  69.  
    </activeProfiles>
  70.  
    </settings>

applicationContext-service.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:context="http://www.springframework.org/schema/context"
  5.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  7.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans.xsd
  9.  
    http://www.springframework.org/schema/context
  10.  
    http://www.springframework.org/schema/context/spring-context.xsd
  11.  
    http://www.springframework.org/schema/aop
  12.  
    http://www.springframework.org/schema/aop/spring-aop.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
    <!-- 事务管理器 -->
  16.  
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  17.  
    <property name="sessionFactory" ref="sessionFactory"></property>
  18.  
    </bean>
  19.  
    <!-- 事务通知 -->
  20.  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
  21.  
    <tx:attributes>
  22.  
    <tx:method name="save*" propagation="REQUIRED"/>
  23.  
    <tx:method name="insert*" propagation="REQUIRED"/>
  24.  
    <tx:method name="update*" propagation="REQUIRED"/>
  25.  
    <tx:method name="delete*" propagation="REQUIRED"/>
  26.  
     
  27.  
    <tx:method name="get*" read-only="true"/>
  28.  
    <tx:method name="*" propagation="REQUIRED"/>
  29.  
    </tx:attributes>
  30.  
    </tx:advice>
  31.  
    <!-- aop -->
  32.  
    <aop:config>
  33.  
    <aop:pointcut id="pointcut" expression="execution(* my.*.*(..))" />
  34.  
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
  35.  
    </aop:config>
  36.  
    <!-- service -->
  37.  
    <bean id="customerService" class="ssh.service.CustomerServiceImpl">
  38.  
    <property name="customerDao" ref="customerDao"></property>
  39.  
    </bean>
  40.  
    </beans>

applicationContext.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:context="http://www.springframework.org/schema/context"
  5.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  7.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans.xsd
  9.  
    http://www.springframework.org/schema/context
  10.  
    http://www.springframework.org/schema/context/spring-context.xsd
  11.  
    http://www.springframework.org/schema/aop
  12.  
    http://www.springframework.org/schema/aop/spring-aop.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
    <!-- service -->
  16.  
    <import resource="classpath:applicationContext-web.xml"/>
  17.  
    <!-- *表示jar包中的配置文件 -->
  18.  
    <import resource="classpath*:applicationContext-service.xml"/>
  19.  
    <import resource="classpath*:applicationContext-dao.xml"/>
  20.  
    </beans>

xxx.hbm.xml

  1.  
    <hibernate-mapping>
  2.  
     
  3.  
    <class name="my.ssh.domain.Customer" table="cst_customer">
  4.  
    <id name="cust_id" column="cust_id">
  5.  
    <!-- 6种主键生成策略:indentity[自增1],native,sequence,uuid,increment,assigned -->
  6.  
    <generator class="native"/>
  7.  
    </id>
  8.  
     
  9.  
    <property name="cust_name" column="cust_name"/>
  10.  
    <property name="cust_user_id" column="cust_user_id"/>
  11.  
     
  12.  
    </class>
  13.  
     
  14.  
    </hibernate-mapping>

hibernate.cfg.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE hibernate-configuration PUBLIC
  3.  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.  
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5.  
     
  6.  
    <hibernate-configuration>
  7.  
     
  8.  
    <session-factory>
  9.  
    <property name="dialect">
  10.  
    org.hibernate.dialect.Oracle10gDialect
  11.  
    </property>
  12.  
     
  13.  
    <property name="show_sql">true</property>
  14.  
    <property name="format_sql">true</property>
  15.  
    <property name="hbm2ddl.auto">none</property>
  16.  
    <!-- 懒加载,配合web.xml中配置的 openSessionInViewFilter -->
  17.  
    <property name="hibernate.enable_lazy_load_no_trans">true</property>
  18.  
    <!--校验模式 JPA java persistent api-->
  19.  
    <property name="javax.persistence.validation.mode">none</property>
  20.  
     
  21.  
    <!-- 加载映射文件-->
  22.  
    <mapping resource="my/ssh/domain/Customer.hbm.xml"></mapping>
  23.  
     
  24.  
    </session-factory>
  25.  
    </hibernate-configuration>
  •  

applicationContext-dao.xml

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:context="http://www.springframework.org/schema/context"
  5.  
    xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
    xmlns:tx="http://www.springframework.org/schema/tx"
  7.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans.xsd
  9.  
    http://www.springframework.org/schema/context
  10.  
    http://www.springframework.org/schema/context/spring-context.xsd
  11.  
    http://www.springframework.org/schema/aop
  12.  
    http://www.springframework.org/schema/aop/spring-aop.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
    <!-- dataSource -->
  16.  
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  17.  
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  18.  
    <property name="jdbcUrl" value="jdbc:mysql:///maven_day01"></property>
  19.  
    <property name="user" value="root"></property>
  20.  
    <property name="password" value="123"></property>
  21.  
    </bean>
  22.  
    <!--sessionFactory -->
  23.  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  24.  
    <property name="dataSource" ref="dataSource"></property>
  25.  
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
  26.  
    </bean>
  27.  
     
  28.  
    <!-- 以后:Dao都需要继承HibernateDaoSupport,注入sessionFactory -->
  29.  
    <bean id="customerDao" class="my.ssh.dao.CustomerDaoImpl">
  30.  
    <property name="sessionFactory" ref="sessionFactory"/>
  31.  
    </bean>
  32.  
     
  33.  
    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
  34.  
    <property name="sessionFactory"></property>
  35.  
    </bean> -->
  36.  
     
  37.  
    </beans>

dao-test

  1.  
    @SuppressWarnings(value="all")
  2.  
    public class CustomerTest {
  3.  
    @Test
  4.  
    public void test() {
  5.  
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-dao.xml");
  6.  
    CustomerDao dao = (CustomerDao) ac.getBean("customerDao");
  7.  
    List<Customer> list = dao.findAll();
  8.  
    System.out.println(list.size());
  9.  
    }
  10.  
    }
posted @ 2020-06-28 08:44  学菜狗  阅读(457)  评论(0)    收藏  举报