多maven项目的搭建和依赖关系详细讲解

时间:2020-9-3 作者:admin


项目结构

多maven项目的搭建和依赖关系详细讲解

各工程介绍

1、父工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>SSM_lyk_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <modules>
        <module>../SSM_lyk_common</module>
        <module>../SSM_lyk_main</module>
        <module>../SSM_lyk_bean</module>
        <module>../SSM_lyk_manager</module>
        <module>../SSM_lyk_managerimpl</module>
    </modules>

</project>

2、实体类工程
1)实体类

public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String phone;
    public User(){
    }
    public User(String name, Integer age, String phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }
    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;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

2)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kang</groupId>
    <artifactId>SSM_lyk_bean</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <artifactId>SSM_lyk_parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../SSM_lyk_parent/pom.xml</relativePath>
    </parent>
</project>

3、接口工程
1)dao层

@Repository
public interface TestDao {
    void insert(User user);
}

2)service层

public interface TestService {
    void insert(User user);
}

3)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kang</groupId>
    <artifactId>SSM_lyk_manager</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--@Repository注解需要用到spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <!--将实体类工程依赖引入-->
        <dependency>
            <groupId>com.kang</groupId>
            <artifactId>SSM_lyk_bean</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

4、实现类工程
1)实现类

@Service
public class TestServiceimpl implements TestService {

    @Autowired
    private TestDao testdao;

    @Override
    public void insert(User user) {
        testdao.insert(user);
    }
}

2)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kang</groupId>
    <artifactId>SSM_lyk_managerimpl</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>SSM_lyk_parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../SSM_lyk_parent/pom.xml</relativePath>
    </parent>

<dependencies>
	<!--将接口工程依赖引入-->
    <dependency>
        <groupId>com.kang</groupId>
        <artifactId>SSM_lyk_manager</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.22</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.2.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.4</version>
    </dependency>
</dependencies>
</project>

5、controller/视图工程
多maven项目的搭建和依赖关系详细讲解

1)controller层

@Controller
public class Test {
    @Autowired
   private TestService testService;

    @RequestMapping("/insert")
    public String insert(){
        User user=new User("小白",18,"11012011911");
        testService.insert(user);
        return "success";
    }
}

2)jdbc.properties(druid连接池)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=false&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

3)spring.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--扫描依赖项目中的注解,也可以在依赖的项目中新建spring配置文件,对注解进行扫描,这边就不需要再扫描了-->
    <context:component-scan base-package="com.kang" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

    <context:property-placeholder location="classpath:config/db.properties"/>
    <bean id="DataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName"    value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="password"   value="${jdbc.password}"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="DataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:userMapper.xml"/>

    </bean>

    <!-- 配置扫描Dao接口包,实现Dao接口注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="configurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--指定sql映射文件/接口所在包-->
        <!--这里扫描的是依赖工程中的dao所在包-->
        <property name="basePackage" value="com.kang.*.dao"/>
    </bean>

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="DataSource"/>
    </bean>

    <!--事务-->
    <tx:advice id="txadvice">
    <tx:attributes>
        <!--如果当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。
        这是默认值这是默认值,表示使用底层数据库的默认隔离级别。对大部分数据库而言,通常这值就是
        遇到任何异常直接回滚-->
        <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
        <tx:method name="query" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>                <!--所以类型 com.kang下面的以service结尾类的所以方式所有参数-->
        <aop:advisor advice-ref="txadvice" pointcut="execution(* com.kang..*Service.*(..))"/>
    </aop:config>
</beans>

4)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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
<!--只扫描controller-->
    <context:component-scan base-package="com.kang" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--加载静态资源-->
    <mvc:default-servlet-handler/>
    <!--加载视图解析器-->
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--遇到任何错误都跳转到当前页面-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" id="resolver">
    <property name="exceptionMappings" >
        <props>
            <prop key="java.lang.Exception">error/error</prop>
        </props>
    </property>
</bean>

    <!--上传最大数和上传编码,延迟加载-->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver" p:defaultEncoding="UTF-8">
        <property name="maxUploadSize" value="2097152"/>
        <property name="resolveLazily" value="true"/>
    </bean>
</beans>

5)userMapper.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.kang.manager.dao.TestDao">
    <insert id="insert" parameterType="com.kang.bean.User">
        insert into t_user(name,age,phone) values (#{name},#{age},#{phone})
    </insert>
</mapper>

6)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SSM_lyk_main</artifactId>
    <packaging>war</packaging>

    <parent>
        <artifactId>SSM_lyk_parent</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../SSM_lyk_parent/pom.xml</relativePath>
    </parent>

    <!--将实现类工程依赖引入-->
    <dependencies>
        <dependency>
            <groupId>com.kang</groupId>
            <artifactId>SSM_lyk_managerimpl</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

7)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <!--过滤器-->
    <filter>
        <filter-name>encoding</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>
        <!--请求与响应编码都为utf-8-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
        <!--设置转发和重定向乱码,默认是转发,当设置了重定向就得在设置转发-->
        <!--<dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>-->
    </filter-mapping>

	<!--spring.xml默认加载不到时,通过监听器去加载-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--会话超时时间-->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

总结:


1、多maven项目之间,通过在pom.xml中引入对应项目的坐标实现交互。

2、maven项目运行时,默认会加载resources资源目录下的文件。如果dao层对应的mapper.xml文件在src目录下,则需要在pom.xml配置,让项目去src下加载mapper.xml。
多maven项目的搭建和依赖关系详细讲解
3、如果项目运行时,出现以下错误:
Error creating bean with name ‘test’: Unsatisfied dependency expressed through field ‘testService’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.kang.manager.service.TestService’ available。
注解也加了,相应的配置也有了。为什么会导致注入类失败呢。
可能是spring配置文件并未加载到。
解决:在web.xml配置中,配置监听器,监听加载spring.xml。

写作不易。。。既然来了,不妨点个关注,点个赞吧!!!

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。