JAVA--Maven创建聚合工程

﹥>﹥吖頭↗ 提交于 2020-01-08 21:16:39

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

--------Maven创建聚合工程---------

创建父工程:

SSM_parent:在pox.xml中导入jar包,让父工程统一管理

<?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.bdit</groupId>
  <artifactId>SSM_Parent_Web_day01</artifactId>
  <version>1.0-SNAPSHOT</version>
    <modules>
        <module>SSM_DAO</module>
      <module>SSM_Service</module>
      <module>SSM_Web</module>
    </modules>
    <!--指定父工程的打包方式-->
  <!--maven的父工程packaging标签的值必须为pom-->
  <packaging>pom</packaging>
  <name>Maven</name>
  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org/</url>
  <inceptionYear>2001</inceptionYear>
  <distributionManagement>
    <site>
      <id>website</id>
      <url>scp://webhost.company.com/www/website</url>
    </site>
  </distributionManagement>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>10</maven.compiler.source>
    <maven.compiler.target>10</maven.compiler.target>
    <!-- 对整个项目 的jar包版本进行统一的管理
   1.properties这里面是对所有的版本进行了定义
   2.dependencyManagement:做了jar包版本的管理,
   3.子项目需要什么jar包我们就可以只引用该jar包就行
   -->
    <!--fu父工程对JAR包进行定义-->
    <junit.version>4.12</junit.version>
    <mybatis.version>3.5.3</mybatis.version>
    <spring-context.version>5.1.6.RELEASE</spring-context.version>
    <mybatis-spring.version>2.0.3</mybatis-spring.version>
    <spring-web.version>5.1.6.RELEASE</spring-web.version>
    <spring-webmvc.version>5.1.6.RELEASE</spring-webmvc.version>
    <javax.servlet-api.version>4.0.1</javax.servlet-api.version>
    <spring-test.version>5.1.6.RELEASE</spring-test.version>
    <spring-jdbc.version>5.1.6.RELEASE</spring-jdbc.version>
    <spring-tx.version>5.1.6.RELEASE</spring-tx.version>
    <aspectjweaver.version>1.9.5</aspectjweaver.version>
    <aopalliance.version>1.0</aopalliance.version>
    <aspectjrt.version>1.9.5</aspectjrt.version>
    <mysql-connector-java.version>8.0.13</mysql-connector-java.version>
    <druid.version>1.1.21</druid.version>
    <javax.annotation-api.version>1.3.2</javax.annotation-api.version>
    <cglib.version>3.3.0</cglib.version>
    <log4j.version>1.2.17</log4j.version>
    <taglibs-standard-impl.version>1.2.5</taglibs-standard-impl.version>
    <taglibs-standard-jstlel.version>1.2.5</taglibs-standard-jstlel.version>
    <taglibs-standard-spec.version>1.2.5</taglibs-standard-spec.version>
    <taglibs-standard-compat.version>1.2.5</taglibs-standard-compat.version>
    <jackson-core.version>2.10.1</jackson-core.version>
    <jackson-databind.version> 2.10.1</jackson-databind.version>
    <jackson-annotations.version> 2.10.1</jackson-annotations.version>
    <fastjson.version>1.2.62</fastjson.version>
    <commons-fileupload.version>1.4</commons-fileupload.version>
  </properties>

  <!--版本锁定配置-->
  <!--2.dependencyManagement:做了jar包版本的管理-->
  <dependencyManagement>
    <dependencies>
      <!--Junit单元测试的Jar包-->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
      </dependency>
      <!--MYbatis架包-->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>

      <!--spring 的核心包-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-context.version}</version>
      </dependency>

      <!--Spring Mybatis整合的包-->
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
      </dependency>
      <!--SpringMvc的核心包-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-web.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-webmvc.version}</version>
      </dependency>
      <!--Servlet API的包-->
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${javax.servlet-api.version}</version>
        <scope>provided</scope>
      </dependency>
      <!--Spring测试的包-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring-test.version}</version>
        <scope>test</scope>
      </dependency>
      <!--jdbc 和事务的依赖包-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-jdbc.version}</version>
      </dependency>
      <!--jdbc事物的包-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-tx.version}</version>
      </dependency>
      <!--AOP的依赖包用于测试-->
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectjweaver.version}</version>
        <scope>runtime</scope>
      </dependency>
      <!--AOP的依赖包-->
      <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>${aopalliance.version}</version>
      </dependency>
      <!--AOP的依赖包用于测试-->
      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectjrt.version}</version>
      </dependency>
      <!--Mysql数据数据库的包-->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version>
      </dependency>
      <!--连接池的包-->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <!--@Resource 所在的包-->
      <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>${javax.annotation-api.version}</version>
      </dependency>
      <!--基于子类的动态代理类-->
      <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>${cglib.version}</version>
      </dependency>
      <!--Log4j日志信息的架包-->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>
      <!--jstl架包-->
      <dependency>
        <groupId>org.apache.taglibs</groupId>
        <artifactId>taglibs-standard-impl</artifactId>
        <version>${taglibs-standard-impl.version}</version>
      </dependency>
      <!--jstl架包-->
      <dependency>
        <groupId>org.apache.taglibs</groupId>
        <artifactId>taglibs-standard-jstlel</artifactId>
        <version>${taglibs-standard-jstlel.version}</version>
        <scope>provided</scope>
      </dependency>
      <!--jstl架包-->
      <dependency>
        <groupId>org.apache.taglibs</groupId>
        <artifactId>taglibs-standard-spec</artifactId>
        <version>${taglibs-standard-spec.version}</version>
      </dependency>
      <!--jstl架包-->
      <dependency>
        <groupId>org.apache.taglibs</groupId>
        <artifactId>taglibs-standard-compat</artifactId>
        <version>${taglibs-standard-compat.version}</version>
      </dependency>
      <!--导入Json解析的工具包-->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-core.version}</version>
      </dependency>
      <!--导入Json解析的工具包-->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-databind.version}</version>
      </dependency>
      <!--导入Json解析的工具包-->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-annotations.version}</version>
      </dependency>
      <!--导入 fastJSON jar包-->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
      </dependency>
      <!--文件上传组件jar包-->
      <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>${commons-fileupload.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>


  <!--把项目中所有放入依赖放入父工程中-->
  <dependencies>
    <!--Junit单元测试的Jar包-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <!--MYbatis架包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <!--spring 的核心包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>

    <!--Spring Mybatis整合的包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
    <!--SpringMvc的核心包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
    </dependency>
    <!--Servlet API的包-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <!--Spring测试的包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!--jdbc 和事务的依赖包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>
    <!--jdbc事物的包-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
    </dependency>
    <!--AOP的依赖包用于测试-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!--AOP的依赖包-->
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
    </dependency>
    <!--AOP的依赖包用于测试-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
    </dependency>
    <!--Mysql数据数据库的包-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--连接池的包-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
    </dependency>
    <!--@Resource 所在的包-->
    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
    </dependency>
    <!--基于子类的动态代理类-->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
    </dependency>
    <!--Log4j日志信息的架包-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
    </dependency>
    <!--jstl架包-->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-impl</artifactId>
    </dependency>
    <!--jstl架包-->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-jstlel</artifactId>
      <scope>provided</scope>
    </dependency>
    <!--jstl架包-->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-spec</artifactId>
    </dependency>
    <!--jstl架包-->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-compat</artifactId>
    </dependency>
    <!--导入Json解析的工具包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
    </dependency>
    <!--导入Json解析的工具包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>
    <!--导入Json解析的工具包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
    </dependency>
    <!--导入 fastJSON jar包-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </dependency>
    <!--文件上传组件jar包-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
    </dependency>
  </dependencies>
</project>

创建SSM-DAO子工程

SMM_DAO:在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">
    <parent>
        <artifactId>SSM_Parent_Web_day01</artifactId>
        <groupId>com.bdit</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SSM_DAO</artifactId>
    <!--指定打包的方式-->
    <packaging>jar</packaging>

</project>

DAO层的SQL映射文件

<?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.bdit.dao.MapperUser">
     <!--查询所有的操作-->
    <select id="findAll" resultType="com.bdit.VO.User">
        select * from user
    </select>

    <!--根据id去查询-->
    <select id="findByid" resultType="com.bdit.VO.User" parameterType="int">
        select  *  from user where id=#{id}
    </select>

    <!--保存信息-->
    <insert id="insert" parameterType="com.bdit.VO.User">
        insert  into user(userName,gender,address)values(#{userName},#{gender},#{address})
    </insert>

    <!--修改信息-->
    <update id="update" parameterType="com.bdit.VO.User">
        update user set userName=#{userName},gender=#{gender},address=#{address} where id=#{id}
    </update>

    <!--删除信息:根据用户的id去删除-->
    <delete id="delete" parameterType="com.bdit.VO.User">
        delete  from user where id=#{id}
    </delete>

<!--    &lt;!&ndash;模糊查询&ndash;&gt;-->
<!--   <select id="findName" parameterType="java.lang.String" resultType="com.model.User">-->
<!--       select * from user  where username like '%${value}%'-->
<!--   </select>-->

<!--    &lt;!&ndash;使用聚合函数查询表中的总记录数&ndash;&gt;-->
<!--    <select id="totalcont"  resultType="java.lang.Integer">-->
<!--        select  count(id) from user-->
<!--    </select>-->

<!--    <select id="finaAlll" resultMap="Map">-->
<!--        select  *  from  user-->
<!--    </select>-->
<!--    <resultMap id="Map" type="com.model.VOUser">-->
<!--    <id column="id"  property="stuId"/>-->
<!--    <result column="username" property="username"/>-->
<!--    <result column="age" property="stuage"/>-->
<!--    <result column="address" property="stuaddress"/>-->
<!--    </resultMap>-->
</mapper>

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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:ab.properties"></context:property-placeholder>
    <!--自动扫描指定的包-->
    <context:component-scan base-package="com.bdit">
        <!--Spring就不在扫描@Contrellor的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--阿里的数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>

    <!--配置mybatis的Session工场-->
    <bean id="sessionFactoryBean"   class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载 mybatis 的配置文件 SqlMapConfig.xml-->
        <property name="configLocation" value="classpath:SqlMapConfig.xml "/>
    </bean>

    <!--配置自动扫描所有的 Mapper 接口和文件-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.bdit.dao"/>
    </bean>

</beans>

DAO的SqlMapConfig.xml文件:里面什么都不用写,容器会自动扫描

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

</configuration>

DAO层的测试类:测试你的配置是否成功:

public class MapperUserSpring {
    @Test
    public void MapperTest(){
        ApplicationContext  context=new ClassPathXmlApplicationContext("classpath:applicationContext-dao.xml");
        MapperUser  mapperUser=context.getBean(MapperUser.class);
        List<User>list=mapperUser.findAll();
        for(User  user:list){
            System.out.println(user);
        }

    }

创建:SSM-Service子工程;

Service层的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">
    <parent>
        <artifactId>SSM_Parent_Web_day01</artifactId>
        <groupId>com.bdit</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SSM_Service</artifactId>
    <!--打包方式为jar包-->
    <packaging>jar</packaging>
    <!--SSM-DAO依赖-->
    <dependencies>
        <dependency>
            <groupId>com.bdit</groupId>
            <artifactId>SSM_DAO</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Service接口以及Service的实现类

package com.bdit.Service;

import com.bdit.VO.User;

import java.util.List;

//编写业务层接口
public interface IUserServiceDao{
    //查询所有的信息
    public List<User> findAll();
    //根据id去查询
    public User findByid(Integer id);
    //添加的方法
    public int insert(User user);
    //更新的方法
    public int update(User user);
    //删除的方法
    public int delete(Integer id);
}
package com.bdit.Service.impl;
import com.bdit.Service.IUserServiceDao;
import com.bdit.VO.User;
import com.bdit.dao.MapperUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceimpl implements IUserServiceDao {

    @Autowired
    private MapperUser userDao;

    public void setUserDao(MapperUser userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> findAll(){
        return userDao.findAll();
    }

    @Override
    public User findByid(Integer id) {
        return userDao.findByid(id);
    }

    @Override
    public int insert(User user) {
        return  userDao.insert(user);
    }

    @Override
    public int update(User user) {
        return  userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return  userDao.delete(id);
    }
}

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

    <!--导入持久层的配置文件-->
    <import resource="classpath:applicationContext-dao.xml"></import>
    <context:component-scan base-package="com.bdit.Service"></context:component-scan>

    <!--配置Spring的事物管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--事务通知的配置和事务管理器对象的引用-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--<tx:attributes>用来配置事务的属性标签-->
        <tx:attributes>
            <!--<tx:method>用来指定方法的名称-->
            <tx:method name="findAll*" read-only="true" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>
            <tx:method name="findByid*" read-only="true" isolation="REPEATABLE_READ" propagation="SUPPORTS"/>
            <tx:method name="insert*"  read-only="false" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <tx:method name="update*" read-only="false"  isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <tx:method name="delete*" read-only="false"  isolation="REPEATABLE_READ" propagation="REQUIRED"/>
            <!--如果这样配置的话针对整个Dao接口的实现类都有效,我们一般分来配置-->
            <tx:method name="*" read-only="false" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 配置AOP切面-->
    <aop:config>
        <!-- 配置AOP切入点-->
        <aop:pointcut id="txPointcut" expression="execution(* com.bdit.Service.impl.*.*(..))"/>
        <!--配置事务的通知和切入点的关联关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
</beans>

Service层测试代码:

public class ServiceTest {
    @Test
    public void TestSpring(){
        ApplicationContext  context=new ClassPathXmlApplicationContext("classpath:applicationContext-Service.xml");
        IUserServiceDao  serviceDao=context.getBean(IUserServiceDao.class);
        List<User>list=serviceDao.findAll();
        for(User user:list){
            System.out.println(user);
        }
    }
}

准备SSM_Web层的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>

  <!--引入父工程-->
  <parent>
    <artifactId>SSM_Parent_Web_day01</artifactId>
    <groupId>com.bdit</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.bdit</groupId>
  <artifactId>SSM_Web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SSM_Web Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>10</maven.compiler.source>
    <maven.compiler.target>10</maven.compiler.target>
  </properties>
  <!--引入Servlice-->
   <dependencies>
     <dependency>
       <groupId>com.bdit</groupId>
       <artifactId>SSM_Service</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
   </dependencies>

</project>

准备SSM_Web层的: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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <!--导入Service的配置文件-->
    <import resource="classpath:applicationContext-Service.xml"></import>

</beans>

准备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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      https://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/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!--配置 springmvc 要扫描的包-->
    <context:component-scan base-package="com.bdit">
        <!--指定扫描包的规则,SpringMVC只扫描包含@Controller的类-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--自动加载处理器映射器、处理器适配器-->
    <!--引用自定义的类型转换器-->
    <mvc:annotation-driven>

    </mvc:annotation-driven>

   <!--配置视图解析器-->
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--视图所在的位置,也就是前缀-->
        <property name="prefix" value="/WEB-INF/pags/"/>
        <!--所使用视图技术的后缀名-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--在 springmvc 中设置,静态资源不要过滤
     location 表示路径,mapping 表示文件或过滤规则,**表示该目录下的文件以及子文件夹-->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>


</beans>

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">
  <!--啥时候加载Spring的配置文件-->
  <!--手动指定Spring的配置文件-->
  <!--相当于配置了一个上下文的初始化参数-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--配置Spring提供的监听器,用于启动服务时加载Spirng的配置文件,
  该监听器只能加载/WEB-INF/目录中名称为applicationContext.xml中的文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置 Spring mvc 框架的编码过滤器-->
  <filter>
    <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--HiddentHttpMethodFilter过滤器来实现RESTFul 风格-->
  <filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--配置 spring mvc 的核心控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置初始化参数,用于读取 springmvc 的配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
    <!--配置 servlet 实例创建的节点,应用加载时就创建-->
    <load-on-startup>0</load-on-startup>
  </servlet>
  <!--配置映射的路径-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

准备SSM-Web层的前端控制器:

package com.bdit.controller;

import com.bdit.Service.impl.UserServiceimpl;
import com.bdit.VO.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceimpl userService;

    @RequestMapping("/list")  //查询用户信息的方法
    public ModelAndView user(){
        ModelAndView  mav=new ModelAndView();
        List<User> list=userService.findAll();
        //指定响应的客户端
        mav.addObject("list",list);
        //指定要跳转的逻辑视图
        mav.setViewName("list");
        return mav;
    }
    //添加用户信息的方法

    @RequestMapping("/insert")
    public String insert(User  user){
        int i=userService.insert(user);
        if(i>0){
            return "forward:/WEB-INF/pags/success.jsp";
        }else{
            return "error";
        }
    }

    //编辑用户的信息
    @RequestMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Integer  id){
        ModelAndView  Mav=new ModelAndView();
        User  user=userService.findByid(id);
        //指定响应的客户端
        Mav.addObject("user",user);
        Mav.setViewName("edit");
        return Mav;
    }

    //用户信息的修改页面
    @RequestMapping("/update")
    public String update(User  user){
        int  i=userService.update(user);
        if(i>0){
            return "redirect:/user/list";
        }else{
            return  "error";
        }
    }
    //用户信息的修改页面
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        int  i=userService.delete(id);
        if(i>0){
            return "redirect:/user/list";
        }else{
            return  "error";
        }
    }
}

-----准备CRUD的前端页面:------

添加查询的页面:index.jsp  -----insert.jsp

<%@page contentType="text/html; charset=utf-8" language="java" %>
<html>
<body>
<h2>Hello World 恭喜你SSM配置成功!</h2>

<a href="user/list" style="color:red">点击======>查询用户的信息</a>
<hr style="width: 10px; border:20px">

<form action="user/insert" method="post">
    用户名:<input type="text" name="userName" style="background: aqua"><br>
    性@别:<input type="radio" name="gender" value="男">男
    <input type="radio" name="gender" value="女">女<br>
    住@址:<input type="text" name="address" style="background: aqua"><br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;
    <input type="submit" value="提交">
    <input type="submit" value="重置">
</form>

</body>
</html>

展示的页面list.jsp

<%--
  Created by IntelliJ IDEA.
  User: Lenovo-T410
  Date: 2020/1/7
  Time: 18:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>用户信息的查询页面</title>
</head>
<body>
<h2 style="color: green"align="center">用户信息的展示页面</h2>
<table width="100%" border="100%" style="background: blanchedalmond">
    <tr align="center">
        <th>用户ID</th>
        <th>用户名</th>
        <th>性别</th>
        <th>住址</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${list}" var="user">
        <tr align="center">
            <td>
               ${user.id}
            </td>
            <td>
                ${user.userName}
            </td>
            <td>
                ${user.gender}
            </td>
            <td>
                ${user.address}
            </td>
            <td>
                <a href="${pageContext.request.contextPath}/user/edit/${user.id}">编辑</a>
                <a href="${pageContext.request.contextPath}/user/delete/${user.id}">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

编辑的页面:edit.jsp

<%--
  Created by IntelliJ IDEA.
  User: Lenovo-T410
  Date: 2020/1/7
  Time: 20:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>用户信息的编辑页面</title>
</head>
<body>
<h2>用户信息的编辑页面</h2>

<form action="${pageContext.request.contextPath}/user/update" method="post">
    用户名:<input type="text" name="userName" value=${user.userName}><br>
    性别:
  <c:if test="${user.gender=='男'}">
      <input type="radio" name="gender" value="男" checked>男
      <input type="radio" name="gender" value="女">女<br>
  </c:if>
  <c:if test="${user.gender=='女'}">
      <input type="radio" name="gender" value="男" checked >男
      <input type="radio" name="gender" value="女"  checked >女<br>
  </c:if>

    住址:<input type="text" name="address" value="${user.address}"><br>
    <input type="submit" value="修改">
    <input type="hidden" name="id" value="${user.id}">
</form>

</body>
</html>

错误信息的提示页面:error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>用户的信息添加失败,服务器正在忙!!!!</h2>
</body>
</html>

----------------------------整合Mybatis-----Spring----SpringMVC的聚合工程完活---------------------

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!