zoukankan      html  css  js  c++  java
  • maven 集成spring ,mybatis

      maven工程分模块项目可以模块与模块之间的耦合度降低,各模块之间的联系就没有那么的紧密。通常情况下为了打包方便,建立一个父类的模块。这样打包发布的时候就不用一个个模块打包过去,只有父类的模块手动打包下就可以了,

     

    工程如下图所示:

     其中book-parent为父类模块,book-core持久层,book-service为服务层,book-web的可视化层 ,既分别对应mvc中的相应层。

    一、book-core

    1、添加包支持,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.0</modelVersion>
    
    	<artifactId>book-core</artifactId>
    	<packaging>jar</packaging>
    
    	<parent>
    		<groupId>com.myWebApp</groupId>
    		<artifactId>book-parent</artifactId>
    		<version>0.0.1-SNAPSHOT</version>
    		<relativePath>../book-parent/pom.xml</relativePath>
    	</parent>
    
    
    	<name>book-core</name>
    <!--由于使用父类因此配置如下-->
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    
    	<dependencies>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-core</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.apache.logging.log4j</groupId>
    			<artifactId>log4j-core</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>com.google.code.gson</groupId>
    			<artifactId>gson</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.javassist</groupId>
    			<artifactId>javassist</artifactId>
    		</dependency>
    
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    
    		</dependency>
    		<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-test</artifactId>
    			</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aspects</artifactId>
    
    		</dependency>
    
    		<dependency>
    			<groupId>org.apache.commons</groupId>
    			<artifactId>commons-dbcp2</artifactId>
    
    		</dependency>
    
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>javax.persistence</groupId>
    			<artifactId>persistence-api</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>com.google.code.gson</groupId>
    			<artifactId>gson</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    			<version>1.4</version>
    		</dependency>
    
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.mybatis.generator</groupId>
    				<artifactId>mybatis-generator-maven-plugin</artifactId>
    				<version>1.3.2</version>
    				<configuration>
    					<verbose>true</verbose>
    					<overwrite>true</overwrite>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    

      父类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.0</modelVersion>
    
    	<groupId>com.myWebApp</groupId>
    	<artifactId>book-parent</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>pom</packaging>
    
    	<name>book-parent</name>
    	<url>http://maven.apache.org</url>
    	<modules>
    		<module>../book-core</module>
    		<module>../book-service</module>
    		<module>../book-web</module>
    	</modules>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.hibernate</groupId>
    				<artifactId>hibernate-core</artifactId>
    				<version>4.2.1.Final</version>
    			</dependency>
    		
    			<dependency>
    				<groupId>org.apache.logging.log4j</groupId>
    				<artifactId>log4j-core</artifactId>
    				<version>2.0</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.slf4j</groupId>
    				<artifactId>slf4j-log4j12</artifactId>
    				<version>1.6.1</version>
    			</dependency>
    
    			<dependency>
    				<groupId>com.google.code.gson</groupId>
    				<artifactId>gson</artifactId>
    				<version>2.2.4</version>
    			</dependency>
    			
    
    			<dependency>
    				<groupId>javax.persistence</groupId>
    				<artifactId>persistence-api</artifactId>
    				<version>1.0.2</version>
    			</dependency>
    			<dependency>
    				<groupId>org.javassist</groupId>
    				<artifactId>javassist</artifactId>
    				<version>3.15.0-GA</version>
    			</dependency>
    
    			<!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> 
    				<version>2.0-beta9</version> </dependency> -->
    			<!-- <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> 
    				<version>0.8.0.RELEASE</version> </dependency> -->
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-core</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-context</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-test</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-aop</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-beans</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-orm</artifactId>
    				<version>4.1.3.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-aspects</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-web</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.apache.commons</groupId>
    				<artifactId>commons-dbcp2</artifactId>
    				<version>2.0.1</version>
    			</dependency>
    
    			<dependency>
    				<groupId>mysql</groupId>
    				<artifactId>mysql-connector-java</artifactId>
    				<version>5.1.6</version>
    			</dependency>
    
    			<dependency>
    				<groupId>junit</groupId>
    				<artifactId>junit</artifactId>
    				<version>4.11</version>
    				<scope>test</scope>
    			</dependency>
    
    			<dependency>
    				<groupId>com.google.code.gson</groupId>
    				<artifactId>gson</artifactId>
    				<version>2.2.4</version>
    			</dependency>
    
    
    
    
    			<!-- web jar -->
    
    			<!-- 依懒users-core -->
    			<!-- <dependency> <groupId>com.rui.user</groupId> <artifactId>users-core</artifactId> 
    				<version>0.0.1-SNAPSHOT</version> </dependency> -->
    
    
    			<dependency>
    				<groupId>javax.servlet</groupId>
    				<artifactId>javax.servlet-api</artifactId>
    				<version>3.0.1</version>
    				<scope>provided</scope>
    			</dependency>
    
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-webmvc</artifactId>
    				<version>4.0.6.RELEASE</version>
    			</dependency>
    
    			<!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> 
    				<version>4.0.6.RELEASE</version> </dependency> -->
    			<!-- <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> 
    				<version>1.9.13</version> </dependency> -->
    			<dependency>
    				<groupId>org.codehaus.jackson</groupId>
    				<artifactId>jackson-mapper-asl</artifactId>
    				<version>1.9.13</version>
    			</dependency>
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis</artifactId>
    				<version>3.2.4</version>
    			</dependency>
    
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis-spring</artifactId>
    				<version>1.2.2</version>
    			</dependency>
    
    
    			<dependency>
    				<groupId>javax.servlet.jsp.jstl</groupId>
    				<artifactId>jstl-api</artifactId>
    				<version>1.2</version>
    			</dependency>
    			<dependency>
    				<groupId>com.myWebApp</groupId>
    				<artifactId>book-core</artifactId>
    				<version>0.0.1-SNAPSHOT</version>
    			</dependency>
    			<dependency>
    				<groupId>com.myWebApp</groupId>
    				<artifactId>book-service</artifactId>
    				<version>0.0.1-SNAPSHOT</version>
    			</dependency>
    			
    		</dependencies>
    	</dependencyManagement>
    
    
    	<build>
    		<pluginManagement>
    			<plugins>
    				<plugin>
    					<groupId>org.apache.maven.plugins</groupId>
    					<artifactId>maven-source-plugin</artifactId>
    					<version>2.4</version>
    					<executions>
    						<execution>
    							<goals>
    								<goal>jar-no-fork</goal>
    							</goals>
    						</execution>
    					</executions>
    				</plugin>
    			</plugins>
    		</pluginManagement>
    	</build>
    </project>
    

      注意:有些包是多余的,

    2、实体类User代码如下:

    package com.mr.cheng.entity;
    
    public class User {
        private Integer id;
    
        private String descn;
    
        private String username;
    
        private String password;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getDescn() {
            return descn;
        }
    
        public void setDescn(String descn) {
            this.descn = descn == null ? null : descn.trim();
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username == null ? null : username.trim();
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password == null ? null : password.trim();
        }
    }
    

    3、User实体类对已的Dao与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.mr.cheng.dao.IUserDao" >
      <resultMap id="BaseResultMap" type="com.mr.cheng.entity.User" >
        <id column="Id" property="id" jdbcType="INTEGER" />
        <result column="descn" property="descn" jdbcType="VARCHAR" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
      </resultMap>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from users
        where Id = #{id,jdbcType=INTEGER}
      </delete>
      <insert id="insert" parameterType="com.mr.cheng.entity.User" >
        insert into users (Id, descn, username, 
          password)
        values (#{id,jdbcType=INTEGER}, #{descn,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, 
          #{password,jdbcType=VARCHAR})
      </insert>
      <update id="updateByPrimaryKey" parameterType="com.mr.cheng.entity.User" >
        update users
        set descn = #{descn,jdbcType=VARCHAR},
          username = #{username,jdbcType=VARCHAR},
          password = #{password,jdbcType=VARCHAR}
        where Id = #{id,jdbcType=INTEGER}
      </update>
      <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select Id, descn, username, password
        from users
        where Id = #{id,jdbcType=INTEGER}
      </select>
      <select id="selectAll" resultMap="BaseResultMap" >
        select Id, descn, username, password
        from users
      </select>
    </mapper>
    

      

    package com.mr.cheng.dao;
    
    import com.mr.cheng.entity.User;
    
    import java.util.List;
    
    import org.springframework.stereotype.Component;
    /**
     * 注意注解@Component必须有,也可以用Service或者Controller代替
     * @author zwc
     *
     */
    @Component
    public interface IUserDao {
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        User selectByPrimaryKey(Integer id);
    
        List<User> selectAll();
    
        int updateByPrimaryKey(User user);
    }
    

    4、spring-mybatis.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/tx
    	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 数据库连接配置 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver">
    		</property>
    		<property name="url" value="jdbc:mysql://localhost:3306/hibernate"></property>
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
    	<!-- spring集成MyBaits -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    		<property name="mapperLocations" value="classpath:com/mr/cheng/xml/*.xml" />
    	</bean>
    	<!-- 使dao与xml文件一一对应 ,注意dao接口必须使用注解 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.mr.cheng.dao" />
    		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    	</bean>
    	<!-- 添加注解支持 -->
    	<tx:annotation-driven />
    </beans>
    

    5、测试类如下:

    package com.mr.cheng.test;
    
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.mr.cheng.dao.IUserDao;
    import com.mr.cheng.entity.User;
    
    public class MyAppTest {
    	@Autowired
    	private IUserDao iUserDao;
    	
    	public void addUserTest(){
    		User user = new User();
    		user.setDescn("111");
    		user.setPassword("password");
    		user.setUsername("cheng");
    		iUserDao.insert(user);
    	}
    
    }
    

      

    二、book-service

    1、book-service是服务层,它对外提供接口。控制层不需要知道具体的实现,只有根据相应的接口就可以去选择他要想的服务。

    接口代码如下:

    package com.mr.cheng.biz;
    
    import java.util.List;
    
    import org.springframework.stereotype.Component;
    
    import com.mr.cheng.entity.User;
    
    @Component
    public interface UserBizDao {
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        User selectByPrimaryKey(Integer id);
    
        List<User> selectAll();
    
        int updateByPrimaryKey(User user);
    }
    View Code

    实现类:

    package com.mr.cheng.biz.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.mr.cheng.biz.UserBizDao;
    import com.mr.cheng.dao.IUserDao;
    import com.mr.cheng.entity.User;
    
    @Service
    public class UserBizDaoImpl implements UserBizDao {
        @Autowired
        private IUserDao iUserDao;
        public int deleteByPrimaryKey(Integer id) {
            iUserDao.deleteByPrimaryKey(id);
            return 0;
        }
    
        public int insert(User record) {
            iUserDao.insert(record);
            return 0;
        }
    
        public User selectByPrimaryKey(Integer id) {
            return iUserDao.selectByPrimaryKey(id);
        }
    
        public List<User> selectAll() {
            return iUserDao.selectAll();
        }
    
        public int updateByPrimaryKey(User user) {
            iUserDao.updateByPrimaryKey(user);
            return 0;
        }
    
    }
    View Code

    2、配置文件:

    spring-service如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        <tx:annotation-driven />
         <context:component-scan base-package="com.mr.cheng.biz.impl.*" />
    </beans>
    View Code

    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.0</modelVersion>
    
      <artifactId>book-service</artifactId>
      <packaging>jar</packaging>
      
      <parent>
          <artifactId>book-parent</artifactId>
          <groupId>com.myWebApp</groupId>
          <version>0.0.1-SNAPSHOT</version>
          <relativePath>../book-parent/pom.xml</relativePath>
      </parent>
    
      <name>book-service</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
            <groupId>com.myWebApp</groupId>
            <artifactId>book-core</artifactId>
        </dependency>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
      </dependencies>
      
            <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    View Code

    三、book-web模块

    1、book-web是web项目,在这个模块项目中他有页面以及相应的控制层组成。页面既是mvc中的视图 ,同事也包含了mvc中的控制层。

    控制层代码:

    package com.mr.cheng.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.mr.cheng.biz.UserBizDao;
    import com.mr.cheng.entity.User;
    
    @Controller
    @RequestMapping("/userController.do")
    public class UserController {
        @Autowired
        private UserBizDao userBizDao;
        @RequestMapping("/login")
        public String login(){
            User user = new User();
            user.setDescn("1111");
            user.setUsername("abc");
            user.setPassword("pass");
            userBizDao.insert(user);
            return "success";
        }
    }
    View Code

    spring-web配置:

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tool="http://www.springframework.org/schema/tool"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tool                        
                            http://www.springframework.org/schema/tool/spring-tool-4.0.xsd
                             http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        
    
        <context:component-scan base-package="com.mr.cheng.controller" />
    </beans>
    View Code

     spring-mvc配日志:

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tool="http://www.springframework.org/schema/tool"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tool                        
                            http://www.springframework.org/schema/tool/spring-tool-4.0.xsd
                             http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <!-- 启用spring mvc 注解 -->
        <!-- 为了让spring容器识别注解配日志 -->
        <context:annotation-config />
    <!--     <mvc:annotation-driven />
        <mvc:default-servlet-handler /> -->
        
    
        <!-- 注解扫描 -->
        <context:component-scan base-package="com.mr.cheng.controller" />
        <!-- <context:component-scan base-package="**.controller.**" /> -->
    
        <!-- 静态资源访问 -->
        <mvc:resources mapping="/front/**" location="/front/" />
    
        <!--页面视图层信息 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/"></property> <!--页面的前辍名 -->
            <property name="suffix" value=".jsp"></property> <!--页面的后辍名 -->
        </bean>
    <!--     <bean
            class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="order" value="0" />
        </bean>
            <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
    <mvc:annotation-driven/>
    </beans>
    View Code

    web.xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <description>hello</description>
        <display-name>rui Java Deploy V1.1.0</display-name>
    
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <!-- /WEB-INF/classes/ -->
            <param-value>config/properties/log4j.properties</param-value>
        </context-param>
    
        <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/config/spring/**/*.xml</param-value> 
            </context-param> -->
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
    <!--         classpath*:config/spring/spring-config.xml
          classpath*:config/spring/user-db.xml
          classpath*:config/spring/dao/user-dao.xml
          classpath*:config/spring/services/user-services.xml
          classpath*:config/spring/user-web.xml 
           classpath*:config/springmvc/spring-servlet.xml-->
           classpath*:config/spring-web.xml
           classpath*:config/spring-service.xml
           classpath*:config/spring-mybatis.xml
          </param-value>
        </context-param>
    
    
        <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:config/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMvc</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
        <!-- 编码 -->
        <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>
    
    
        <!-- 解决hibernate 懒加载异常 OpenSessionInViewFilter openSessionInView -->
        <!-- <filter>
            <filter-name>openSessionInView</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>openSessionInView</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> -->
        
        <!-- 监听 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>
    View Code
  • 相关阅读:
    12个思维管理工具:标杆分析法、麦肯锡7步分析法、SMART原则....
    Capturing Audio in Android Q
    闭环思维
    如何下载Google Play商店的apk?如何安装分割的apk ?
    强制删除文件 或 文件夹
    ToDesk ---- 个人免费 极致流畅的远程协助软件
    无法显示内挂(非内嵌)字幕-
    MKVToolNix 一款Matroska(.mkv)格式编辑工具,可以将超过16条音轨/字幕封装到一个.mkv文件中去
    Symantec Endpoint Protection(赛门铁克杀毒软件) 如何添加白名单避免被误删、误杀?
    二维码识别工具
  • 原文地址:https://www.cnblogs.com/chengAddress/p/4434781.html
Copyright © 2011-2022 走看看