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

项目结构

多maven项目的搭建和依赖关系详细讲解_第1张图片

各工程介绍

1、父工程


<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.0modelVersion>
    <groupId>org.examplegroupId>
    <artifactId>SSM_lyk_parentartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <modules>
        <module>../SSM_lyk_commonmodule>
        <module>../SSM_lyk_mainmodule>
        <module>../SSM_lyk_beanmodule>
        <module>../SSM_lyk_managermodule>
        <module>../SSM_lyk_managerimplmodule>
    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


<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.0modelVersion>
    <groupId>com.kanggroupId>
    <artifactId>SSM_lyk_beanartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <artifactId>SSM_lyk_parentartifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
        <relativePath>../SSM_lyk_parent/pom.xmlrelativePath>
    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


<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.0modelVersion>
    <groupId>com.kanggroupId>
    <artifactId>SSM_lyk_managerartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.1.5.RELEASEversion>
            <scope>compilescope>
        dependency>

        
        <dependency>
            <groupId>com.kanggroupId>
            <artifactId>SSM_lyk_beanartifactId>
            <version>1.0-SNAPSHOTversion>
        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


<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.0modelVersion>
    <groupId>com.kanggroupId>
    <artifactId>SSM_lyk_managerimplartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <artifactId>SSM_lyk_parentartifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
        <relativePath>../SSM_lyk_parent/pom.xmlrelativePath>
    parent>

<dependencies>
	
    <dependency>
        <groupId>com.kanggroupId>
        <artifactId>SSM_lyk_managerartifactId>
        <version>1.0-SNAPSHOTversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>5.2.8.RELEASEversion>
    dependency>
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.46version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-ormartifactId>
        <version>5.2.8.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.5.5version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aopartifactId>
        <version>5.2.8.RELEASEversion>
    dependency>

    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>5.2.8.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.2.8.RELEASEversion>
    dependency>
    <dependency>
        <groupId>commons-collectionsgroupId>
        <artifactId>commons-collectionsartifactId>
        <version>3.2.2version>
    dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>druidartifactId>
        <version>1.1.22version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>2.0.5version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-aspectsartifactId>
        <version>5.2.7.RELEASEversion>
    dependency>
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.4version>
    dependency>
dependencies>
project>

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

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


<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">

    
    <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>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="configurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        
        <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>                
        <aop:advisor advice-ref="txadvice" pointcut="execution(* com.kang..*Service.*(..))"/>
    aop:config>
beans>

4)springmvc.xml


<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">

    <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/errorprop>
        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(映射文件)



<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


<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.0modelVersion>
    <artifactId>SSM_lyk_mainartifactId>
    <packaging>warpackaging>

    <parent>
        <artifactId>SSM_lyk_parentartifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
        <relativePath>../SSM_lyk_parent/pom.xmlrelativePath>
    parent>

    
    <dependencies>
        <dependency>
            <groupId>com.kanggroupId>
            <artifactId>SSM_lyk_managerimplartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
project>

7)web.xml


<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>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
        
        
    filter-mapping>

	
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring/spring.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:spring/springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
    <session-config>
        <session-timeout>30session-timeout>
    session-config>
    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
web-app>

总结:

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

2、maven项目运行时,默认会加载resources资源目录下的文件。如果dao层对应的mapper.xml文件在src目录下,则需要在pom.xml配置,让项目去src下加载mapper.xml。
多maven项目的搭建和依赖关系详细讲解_第3张图片
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。

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

你可能感兴趣的:(maven,mybatis,java,maven,spring)