1. 导入jar包冲突的解决

1.1 问题引入

例如:我们在pom.xml中导入

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

此时,由于根据传递依赖,spring-context依赖的jar包都将被导入到项目的工作空间,并且版本都锁定为5.0.2.RELEASE

查看jar包之间的依赖

可以看到各个jar包之间的依赖关系

问题来了,此时我们又需要导入spring-beans的坐标

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>

再次查看jar包依赖

可以看到spring-context和spring-beans都依赖于spring-core这个jar包,查看External Libraries中的情况:

惊奇的看到spring-core包的版本号被替换为4.2.4.RELEASE,这也是为了解决jar包冲突,项目中是不允许有两个一模一样的jar包存在,maven帮我们解决的冲突。

1.2 冲突的解决方案

maven工程要导入jar包坐标,就必须要考虑解决jar包冲突。

1.2.1 声明优先原则

在pom.xml中,哪个jar包坐标在靠上的位置,哪个jar包就先被声明,先声明jar包下的依赖可以先进入项目中,如上问题的引入。

1.2.2 路径近者优先原则

maven直接导入jar包的一些概念:
直接依赖:项目中直接导入的jar包,就是该项目的直接依赖jar包。
传递依赖:项目中没有直接导入的jar包,可以通过项目中直接依赖jar包传递到项目中去。
解决上述的冲突,如果我们想要版本号为5.0.2.RELEASE的spring-core jar包,而又不想移动坐标位置,使用路径近者优先原则:直接依赖的路径比传递依赖近,那么最终导入的jar包会优先近的使用直接依赖的jar包

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>

可以再声明一个直接依赖的jar包坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>

1.2.3 直接排除法(推荐使用)

除了使用路径近者优先原则解决冲突外,还有一种方法是我们更被推荐的,那就是直接排除法,还是同样解决上面的问题
,如果我们想要版本号为5.0.2.RELEASE的spring-core jar包,而又不想移动坐标位置,使用直接排除法:将4.2.4.RELEASE的版本号排除掉

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
  </dependency>

如下:

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
 </dependency>

而且排除的jar包可以不用写版本号

查看External Libraries中的情况:

2. pom文件中标签

	<!-- 统一管理jar包版本 -->
	<properties>
		<spring.version>5.0.2.RELEASE</spring.version>
		<slf4j.version>1.6.6</slf4j.version>
		<log4j.version>1.2.12</log4j.version>
		<shiro.version>1.2.3</shiro.version>
		<mysql.version>5.1.6</mysql.version>
		<mybatis.version>3.4.5</mybatis.version>
		<spring.security.version>5.0.1.RELEASE</spring.security.version>
	</properties>

	<!-- 锁定jar包版本 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-web</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-tx</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
				<version>${mybatis.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<!-- 项目依赖jar包 -->
	<dependencies>
		<!-- spring -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.8</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- log start -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- log end -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-config</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-core</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-taglibs</artifactId>
			<version>${spring.security.version}</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.9</version>
		</dependency>
	</dependencies>
	<!-- 添加tomcat7插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
			</plugin>
		</plugins>
	</build>

2.1 标签解释

<mark>标签说明:</mark>

  1. <properties>:作用是管理将所有的jar包版本抽取出来,便以管理。
  2. <dependencyManagement>:使用 <dependencyManagement>可以统一管理项目的版本号,确保各个项目的依赖版本一致,不用每个Module去都弄一个版本号,这样不利于管理,当需要变更版本号时,只需要在父容器中更新即可,必须要任何子项目的修改,如果某个Module需要一个特殊的版本号而父容器dependencyManagement中没有时,则就直接在自己的模块中dependencies声明一个即可,这个子类模块就会使用自己定义的依赖jar包,而不从继承父类
  3. dependencyManagement于dependencies标签的区别:
    1. 有dependencyManagement在的时候,所有不指定版本号的dependencies都会继承dependencyManagement的版本号。
    2. dependencyManagement只是声明依赖,并不导入依赖,所以子项目需要显示的指明需要的依赖,否则是不会从父项目中继承下来的;只有子项目中没有指定版本号才会从dependencyManagement继承下来,否则使用自己的版本号。

3. 准备ssm项目

3.1 项目目录结构

3.2 dao编写

  1. ItemDao接口
public interface ItemsDao {
   
    public Items findItemsById(Integer id);
}

  1. 资源文件下准备接口映射文件

ItemsDao.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.liuzeyu.dao.ItemsDao">
    <select id="findItemsById" parameterType="int" resultType="items">
        select * from items where id = #{id}
    </select>
</mapper>
  1. spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- dao配置开始-->
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///maven"/>
        <property name="username" value="root"/>
        <property name="password" value="809080"/>
    </bean>
    <!-- 配置生产sqlSessionFactory的对象 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.liuzeyu.domain"/>
    </bean>
    <!-- 配置创建接口代理对象 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.liuzeyu.dao"/>
    </bean>
<!-- dao配置结束-->
</beans>

3.3 service编写

  1. ItemServiceImpl接口实现
@Service
public class ItemServiceImpl implements ItemService {
   

    @Autowired
    private ItemsDao itemsDao;

    public Items findItemById(Integer id) {
   
        return itemsDao.findItemsById(id);
    }
}
  1. ItemService接口
public interface ItemService {
   

    public Items findItemById(Integer id);
}
  1. 在spring的配置文件中加入业务层的事物配置
<!-- service配置开始-->
    <!-- 配置service的组件扫描 -->
    <context:component-scan base-package="com.liuzeyu.service"/>
    <!-- aop:面向切面编程,切面=切入点+通知 配置service事务通知 -->
        <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
        <!-- 配置事务通知 -->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面-->
    <aop:config>
            <!-- 切入点表达式 -->
        <aop:pointcut id="pointcut" expression="execution(* com.liuzeyu.service.*.*(..))"/>
            <!-- +通知 = 变成切面 -->
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>
<!-- service配置结束-->

3.4 web编写

  1. 新建springmvc.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 组件扫描-->
    <context:component-scan base-package="com.liuzeyu.controller"/>
<!-- 配置处理器映射器和处理器适配器-->
    <mvc:annotation-driven/>
<!-- 配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
    </bean>

<!-- 放行静态资源,将静态资源交给HttpServlet,而不是DispatherServlet处理-->
    <mvc:default-servlet-handler/>
</beans>

  1. web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<!-- 配置处理post请求的编码过滤器-->
    <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>

<!-- 配置spring核心***-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 重新指定spring文件的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

<!-- 配置spring核心的servlet-->
    <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

  1. itemDetail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>    
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> 
	<form>
		<table width="100%" border=1>
			<tr>
				<td>商品名称</td>
				<td> ${item.name } </td>
			</tr>
			<tr>
				<td>商品价格</td>
				<td> ${item.price } </td>
			</tr>
			<tr>
				<td>生成日期</td>
				<td> <fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/> </td>
			</tr>
			<tr>
				<td>商品简介</td>
				<td>${item.detail} </td>
			</tr>
		</table>
	</form>
</body>
</html>
  1. controller控制器 DetaiController
@Controller
@RequestMapping("/items")
public class DetaiController {
   

    @Autowired
    private ItemService service;

    @RequestMapping("details")
    public String getDetail(Model model){
   
        Items item = service.findItemById(1);
        model.addAttribute("item",item);
        return "itemDetail";
    }
}
  1. 日志文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

3.5 实体类编写

Items 类

public class Items implements Serializable {
   

    private Integer id;
    private String name;
    private Double price;
    private String pic;
    private Date createtime;
    private String detail;
    
    ..................省略getter和setter
}

3.6 程序运行结果

4.采用maven分模块构建ssm项目

以下的代码均可以从ssm项目中拷贝,主要演示的时构建的过程。

4.1 创建工程和模块


父工程删除 src 和 target目录
创建完之后会在父工程的pom.xml文件下多了

<modules>
   <module>maven_dao</module>
   <module>maven_service</module>
   <module>maven_web</module>
</modules>

4.2 导入各个模块之间的依赖

maven-service依赖于maven-dao,在maven-service的pom.xml中添加

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven_dao</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

maven-web依赖于maven-service,在maven-web的pom.xml中添加

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven_service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

4.3 dao层模块

导入dao的代码,在maven-dao中

applicationContext-dao.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- dao配置开始-->
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///maven"/>
        <property name="username" value="root"/>
        <property name="password" value="809080"/>
    </bean>
    <!-- 配置生产sqlSessionFactory的对象 -->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.liuzeyu.domain"/>
    </bean>
    <!-- 配置创建接口代理对象 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.liuzeyu.dao"/>
    </bean>
<!-- dao配置结束-->
</beans>

4.3 service层模块

导入service层的代码,在maven-service中

applicationContext-service.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- service配置开始-->
    <!-- 配置service的组件扫描 -->
    <context:component-scan base-package="com.liuzeyu.service"/>
    <!-- aop:面向切面编程,切面=切入点+通知 配置service事务通知 -->
        <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
        <!-- 配置事务通知 -->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面-->
    <aop:config>
            <!-- 切入点表达式 -->
        <aop:pointcut id="pointcut" expression="execution(* com.liuzeyu.service.*.*(..))"/>
            <!-- +通知 = 变成切面 -->
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>
<!-- service配置结束-->
</beans>

applicationContext-service.xml会从dao层继承applicationContext-do.xml的dao层配置,注意也需要放spring文件夹中

4.4 web层模块

导入web层的代码,在maven-web中

applicationContext.xml导入了dao模块和service模块

<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <import resource="classpath:spring/applicationContext-service.xml"/>
    <import resource="classpath:spring/applicationContext-dao.xml"/>
</beans>

4.5 启动maven工程的三种方式

4.5.1 启动maven工程的父工程

找到maven视图

运行访问

4.5.2 启动maven工程的web模块

找到maven视图

报错错误:

这个错误时因为我们没有将dao层和service层打包成jar包放在本地仓库,导致web层找不到jar包,因为web层依赖于service的

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

service又依赖于dao模块
解决方法:
将dao层和service层打包成jar包放在本地仓库

重新运行测试

4.5.3 使用本地的tomcat启动maven工程

部署本地tomcat


运行测试

5. ***的运用

5.1 了解***

三种仓库:

  1. 本地仓库:用来存储从远程仓库或中央仓库下载的插件和 jar 包,项目使用一些插件或 jar 包,
    优先从本地仓库查找
    默认本地仓库位置在${user.dir}/.m2/repository,${user.dir}表示 windows 用户目录。
  2. 远程仓库(公司私有服务器):如果本地需要插件或者 jar 包,本地仓库没有,默认去远程仓库下载,远程仓库可以在互联网内也可以在局域网内。
  3. 中央仓库:在 maven 软件中内置一个远程仓库地址http://repo1.maven.org/maven2 ,它是中
    央仓库,服务于整个互联网,它是由 Maven 团队自己维护,里面存储了非常全的 jar 包,它包含了世界上大部分流行的开源项目构件。

5.2 如何将jar包上传至***

将jar包上传至snapshots,releases

  1. 使用IDEA工具
  2. maven配置settings.xml
    releases是发行版本,snapshots是快照版本,username和password必须和***的服务器账号密码对上,否则将爆出401错误
    <server>
    	<id>releases</id>
    	<username>admin</username>
    	<password>admin123</password>
    </server>
    <server>
    	<id>snapshots</id>
    	<username>admin</username>
    	<password>admin123</password>
    </server>
  1. IDEA的pom.xml配置
<distributionManagement>
    <snapshotRepository>
       <id>snapshots</id>
        <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
  </snapshotRepository>
  <repository>
        <id>releases</id>
        <url>http://localhost:8081/nexus/content/repositories/releases</url>
  </repository>
</distributionManagement>
  1. 上传,将maven_dao模块打包成jar包上传

    写在最后::releases一直上传不上去,爆出400错误,经查阅资料,应该是maven的版本呢nexus冲突导致的。

5.3 如何将jar包从***下到本地

配置maven的settings.xml

<profiles>
	<profile> 
		<!--profile的id -->
		<id>dev</id>
		<repositories>
			<repository> <!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
				<id>nexus</id> <!--仓库地址,即nexus仓库组的地址 -->
				<url>http://localhost:8081/nexus/content/groups/public/</url> <!--是否下载releases构件 -->
				<releases>
					<enabled>true</enabled>
				</releases> <!--是否下载snapshots构件 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
		<pluginRepositories> <!-- 插件仓库,maven的运行依赖插件,也需要从***下载插件 -->
			<pluginRepository> <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
				<id>public</id>
				<name>Public Repositories</name>
				<url>http://localhost:8081/nexus/content/groups/public/</url>
			</pluginRepository>
		</pluginRepositories>
	</profile>

  </profiles>
  -->
  	<activeProfiles>
		<activeProfile>dev</activeProfile>
	</activeProfiles>

此时如果遇到本地仓库没有jar包,就会从***上面下载到本地。

5.4 如何将jar包上传到本地

--安装第三方jar包到本地仓库

----进入jar包所在目录运行
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar
----打开cmd直接运行
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar

5.5 如何将jar包上传到***

--安装第三方jar包到***

--在settings配置文件中添加登录***第三方登录信息
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
----进入jar包所在目录运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
----打开cmd直接运行
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\授课资料\资料:maven【高级】\安装第三方jar包\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

参考黑马视频