springMVC+spring+mybatis整合(maven)

1.

项目建好,pom文件弄好.

2.

首先整合spring+mybatis,我直接建了一个applicationContext.xml将mybatis和spring的一些配置都写在了里面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/task
      http://www.springframework.org/schema/task/spring-task-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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--扫描 -->
    <context:component-scan base-package="com.wuzw"/>
  <!--  引入数据库配置文件-->
    <context:property-placeholder location="classpath:c3p0.properties"/>

    <!-- 创建数据源 c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="${jdbc.driverClasss}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 配置sqlsessionFactor-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 将pojo包下的类以类名起别名-->
        <property name="typeAliasesPackage" value="com.wuzw.pojo"/>
        <!-- 引入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="mapperLocations" value="classpath:com/wuzw/mapper/*Mapper.xml"/>-->
    </bean>

       <!-- 自动扫描 如果在mapper.class和xml同名同包直接就能扫描出来,
       如果不同就要在sqlsessionFactory中配置mapperlocations来指定xml的位置
       -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wuzw.mapper"/>
    </bean>

    <!-- 配置事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置使用spring声明式编程 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

3

然后进行测试

4.

然后整合springmvc
<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

       <!-- 扫描controller-->
      <context:component-scan base-package="com.wuzw.controller"/>

       
<!--注解的适配器-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!--&lt;!&ndash;注解的映射器&ndash;&gt;-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!--使用mvc的注解驱动代替上面的注解适配器和映射器,实际开发使用此种方式-->
<mvc:annotation-driven/> <!--静态资源解析包括 :js、css、img、..--> <!--<mvc:resources mapping="/js/" location="/js/**"/>--> <!--<mvc:resources mapping="/css/" location="/css/**"/>--> <!--<mvc:resources mapping="/fonts/" location="/fonts/**"/>--> <!--<mvc:resources mapping="/images/" location="/images/**"/>--> <!--加载静态资源--> <mvc:default-servlet-handler/> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
</beans>
最后开tomcat测试


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值