SpringMVC+Spring+Mybatis整合配置

1、Maven依赖文件: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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.nercita.ltxx</groupId>
  <artifactId>ssm.sample</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssm</name>
  <url>http://maven.apache.org</url>

  <properties>
        <mysql.connector.version>5.1.21</mysql.connector.version>
        <spring.version>4.1.6.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <velocity.version>1.6.1</velocity.version>
        <velocity.tools.version>2.0</velocity.tools.version>
        <mock.version>2.0.8</mock.version>
        <mybatis.version>3.3.1</mybatis.version>
        <mybatis-spring.version>1.2.2</mybatis-spring.version>
  </properties>
  <dependencies>
        <!-- velocity -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>${velocity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>${velocity.tools.version}</version>
        </dependency>

        <!-- jsp support -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version> <!-- just used the latest version, make sure you use the one you need -->
            <scope>provided</scope>
        </dependency>

        <!-- test support -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- mybatis support -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>

        <!-- Spring support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

  </dependencies>
  <build>
    <finalName>ssm</finalName>
  </build>
</project>

2、web.xml

web.xml规定了有哪些显式的servlet,规定了最顶层的Context环境,是整个的纲领。它同时规定了对于哪些URL的请求归哪些servlet处理,哪些URL使用哪种编码方式。需要注意的是,对于没有人为指定配置文件的servlet,会自动在WEB-INF文件夹下找servletName-servlet.xml的文件作为其配置。

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

  <display-name>SSM Web Application</display-name>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
        <!--指定Context的配置文件-->
  </context-param>

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>

    <servlet>
        <servlet-name>ssm</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/webmvc-config.xml</param-value>
		</init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ssm</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3、springMVC配置:webmvc-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!--处理对静态资源的访问-->
	<mvc:resources location="/common/"   mapping="/common/**"/>
	<mvc:resources location="/images/"   mapping="/images/**" />
	<mvc:resources location="/js/"       mapping="/js/**" />
	<mvc:resources location="/css/"      mapping="/css/**" />
	<mvc:resources location="/styles/"   mapping="/styles/**" />
	<mvc:resources location="/htm/"      mapping="/htm/**" />

        <import resource="view-controller.xml" />

        <context:annotation-config/>

	<context:component-scan base-package="com.zmx.ssm" use-default-filters="false">
		 <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
	</context:component-scan>

    <!--规定模板文件的类型和位置-->
    <bean id="velocityConfigurer"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="templates/" />
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
                <prop key="output.encoding">utf-8</prop>
            </props>
        </property>
    </bean>
    <!--配置附加工具,以及将后缀为vm的文件交给下面的Resolver处理-->
    <bean id="velocityViewResolver"
        class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm" />
        <property name="contentType" value="text/html;charset=utf-8" />
        <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>
    </bean>

</beans>

4、applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	                    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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
	                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath*:/application.properties" />

    <context:spring-configured /> 

    <!-- 注解bean及依赖注入 -->
	<context:component-scan base-package="com.zmx.ssm">
		<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
	</context:component-scan>

    <!--数源设置-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${db.driverClass}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--规定了扫描mapper文件的路径-->
        <property name="mapperLocations" value="classpath*:mapper/*.xml" />
        <!--指定了Mybatis配置文件的路径-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!--这一段让我们的MapperInterface能够最终在Spring中生成Bean最后被AutoWired-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.zmx.ssm.**.mapper" />
    </bean>

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

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

5、Mybatis配置:mybatis-config.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>
    <typeAliases>
        <!--POJO 类的所在package,默认类名为其Alias-->
        <package name="com.zmx.ssm.user.domain"/>
    </typeAliases>
    <mappers>
        <!--映射文件所在的地方-->
        <mapper resource="UserMapper.xml" />
    </mappers>
</configuration>

6、velocity模板引擎:toolbox.xml

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
    <tool>
        <key>date</key>
        <scope>request</scope>
        <class>
            org.apache.velocity.tools.generic.DateTool
        </class>
        <parameter name="format" value="yyyy-MM-dd HH:mm:ss"/>
    </tool>
    <tool>
        <key>link</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.view.tools.LinkTool</class>
    </tool>
    <tool>
        <key>stringUtils</key>
        <scope>request</scope>
        <class>org.apache.velocity.util.StringUtils</class>
    </tool>
    <tool>
        <key>math</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.MathTool</class>
    </tool>
    <tool>
        <key>esc</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.generic.EscapeTool</class>
    </tool>
    <tool>
        <key>params</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
    </tool>
</toolbox>

7、Mybatis映射文件

8、使用Spring-test测试

package test.com.zmx.ssm.user;
import org.junit.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 org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.zmx.ssm.user.domain.User;
import com.zmx.ssm.user.mapper.UserMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
@Transactional
public class UserDaoTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testInsert() {
        User user=new User();
        user.setAccount("manager");
        user.setPassword("123456");
        userMapper.addUser(user);
    }

}

9、小结

Spring常用的注解主要有以下几种:

  • @Component
  • @Service--业务层
  • @Respority--持久化层
  • @Controller--视图层

尽管这几种注解对应的意义不一样,但它们都有一个共同的作用,那就是让Spring找到并且最终生成对应的Bean。举个例子,我们在Service中使用接口UserDAO,但该接口的实例我们并没有指定使用哪一个,虽然我们写了对应的Impl Class。 通过注解生成Bean,Spring就能够在Service调用该接口的时候从容器里得到正确的实体对象。

时间: 2024-11-09 17:53:27

SpringMVC+Spring+Mybatis整合配置的相关文章

SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置

一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不能用,log也不知道能不能用,`(*∩_∩*)′哈哈,有点不负责任...... 直接上代码: 使用的eclipse和eclipse自带的maven,参考了网上的资料,有些代码是拷贝的,不过都自己测试过了.嗯,可以跑起来... 先上项目结构: 新建maven项目,选择web,然后配置pom: <pro

SSM(springmvc+spring+mybatis)整合过程

问题?SSM(springmvc+spring+mybatis)整合过程 一.SSM框架比较S2SH框架的优缺点比较 站在个人使用这两个框架的观点儿上来说的话,我觉得这个不能定死的说,常用的S2SH,SpringMVC等,还可以考虑jFinal. 首先说S2SH,这个是用的比较多的,特别是在中小型项目中,针对大型项目就不行了,是Struts和hibernate过于庞大,过于重量级,项目大了之后配置文件多的也是麻烦事,配置文件多了之后一方面不好操作,另外比较蛋疼的是没法断点调试,特别是对底层数据库

ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)

在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,下面就只贴出各层实现功能的代码: Jsp页面实现功能的js代码如下: <script> //用于捕获分类编辑按钮的 click 事件,并且根据返回值确定是否允许进入名称编辑状态 function beforeEditName(treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.

springmvc+spring+mybatis 整合

1.其实我也是菜鸟一枚,以前ssm不会,所以花了一段时间,学习了mybatis spring又进行了整合,后来又学习springmvc算是都看了看,今天就把整个搭建好的框架整理一下,和大家分享,如果错误希望指正…… 2.整个项目的目录结构: 3.有了整体结构之后我们一步一步整合来实现,整合步骤: (1).配置前端控制器,web.xml配置 (2).创建处理映射器 springmvc-server.xml 配置 (3).创建控制层 (4).业务逻辑层 (5).dao层 (6).sqlmapper

springmvc+spring+mybatis整合实例【转】

开发环境: System:Windows server 2003 WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5. IDE:eclipse.MyEclipse 6.5 Database:MySQL 开发依赖库: JavaEE5.Spring 3.0.5.Mybatis 3.0.2.myBatis-spring-1.0.0-rc2 1. 首先新建一个WebProject 命名为ssi,新建项目时,使用JavaEE5的lib库.然后手动添加需要的jar

springmvc+spring+mybatis整合实例

开发环境: System:Windows server 2003 WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5. IDE:eclipse.MyEclipse 6.5 Database:MySQL 开发依赖库: JavaEE5.Spring 3.0.5.Mybatis 3.0.2.myBatis-spring-1.0.0-rc2 参考百度文库:http://wenku.baidu.com/view/34559702a6c30c2259019e4e.

springmvc+spring+mybatis+mysql配置过程

环境:eclipse 项目目录: jar包: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http:/

springmvc+spring+mybatis整合项目

开发环境: System:Windows server 2003 WebBrowser:IE6+.Firefox3+ JavaEE Server:tomcat5. IDE:eclipse.MyEclipse 6.5 Database:MySQL 开发依赖库: JavaEE5.Spring 3.0.5.Mybatis 3.0.2.myBatis-spring-1.0.0-rc2 参考百度文库:http://wenku.baidu.com/view/34559702a6c30c2259019e4e.

JAVA springmvc+spring+mybatis整合

一.springmvc---controller  spring----service  mybatiss---dao pring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 因为springmvc和spring属于同一个公司不需要整合包,而mybaits和spring需要整合包. 二.jar包列表 pom文件依赖: 1 <dependencies> 2 <dependency> 3 <groupId>org