Spring+SpringMVC+myBatis环境搭建

来源:互联网 发布:Servlet接收json gson 编辑:程序博客网 时间:2024/05/17 04:07
Spring+SpringMVC+myBatis环境搭建


第一步:引入Spring配置文件(放在src下)
第二步:引入jar包(20多个吧)
第三步:配置spring配置文件
注:以下配置包括Spring,都是基于注解的。
<?xml version="1.0" encoding="gbk"?>
<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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 


xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 


xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 


xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="   


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-


3.2.xsd   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-


3.2.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-


3.2.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd   


http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-


1.3.xsd   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-


3.2.xsd   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-


3.2.xsd   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-


3.2.xsd">
<!-- 声明数据源,并注入连接参数 -->
<bean id="ds" 
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql:///test?useUnicode=true&amp;characterEncoding=utf8"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="wangmeng"/>
</bean>
<!-- 声明SessionFactory 该工厂有spring提供 sqlSessionFactorybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource -->
<property name="dataSource" ref="ds"/>
<!-- Spring整合Mybatis它提供的这个工厂可以直接扫描mapper.xml因此这里需要注入要扫描的这些mapper.xml -->
<property name="mapperLocations" value="classpath:org/wangmeng/sql/*.xml"></property>
//这里是通配即该报下所有的xml文件都扫描
</bean>
<!-- 声明MapperSCannerConfigurer,该bean可以自动扫描指定包下所有的接口,并实现这些接口 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="org.wangmeng.dao"></property>
</bean>
<!-- 事物管理 -->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<tx:annotation-driven transaction-manager="tx"/>
<!-- 开启IOC组建扫描-->
<context:component-scan
base-package="org.wangmeng"/>
<!-- 开启Spring MVC注解 -->
<mvc:annotation-driven />




<!--配置视图组建ViewResolver-->
<bean id="viewResolver" 


class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-


INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


<!-- 设置aop实现方式 -->
<aop:aspectj-autoproxy/>
</beans>
第四步:配置web—xml
 <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:applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.form</url-pattern>
  </servlet-mapping>
第五步:Mybatis映射文件(mapper.xml)
0 0
原创粉丝点击