SSM框架搭建

先將Spring和Mybatis整合java

1.加入 mybatis 的 jar 包和配置文件: 實際上須要配置的就是 settings 的部分。web

<?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>
      <!-- settings標籤能夠設置多個屬性值 -->
      <settings>
            <!-- setting標籤負責每個屬性的設置 -->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
      </settings>
</configuration>
 
2. 加入 Spring 的 jar 包和配置文件
 
以前是在 mybatis-config.xml 獲取 sqlSessionFactory 和掃描 mapper 接口
可是如今 mybatis-config.xml 只有 settings 
因此在 bean.xml 中配置得到 sqlSessionFactory 和掃描 mapper 接口
Spring具體配置參見上篇《Spring+SpringMVC的整合》
<context:component-scan base-package="com.neuedu">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 加載外部屬性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0 數據源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="user" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
      <property name="driverClass" value="${jdbc.driver}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>

<!-- 配置事務管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 開啓基於註解的事務 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="comboPooledDataSource"></property>
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 批量掃描mapper接口包 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.neuedu.sm.mapper"></property>
</bean>

 

3.Test 測試spring

public class TestEmployeeMapper {
      private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
      @Test
      public void test() {
            EmployeeMapper bean = ioc.getBean(EmployeeMapper.class);
            Employee employee = bean.getEmpById(1);
            System.out.println(employee);
      }
}

 

以上Spring+Mybatis的配置基本完畢sql


 

將 SpringMVC整合到上面的項目中
 
1.配置SpringMVC 配置文件
<context:component-scan base-package="com.neuedu" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/views/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

<!-- 處理靜態資源 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

 

2.建立相應的Controller、Serviceexpress

 

 3.在Controller層獲取頁面傳來的數據,調用Service層方法【SpringMVC】mybatis

 Service層調用Mapper接口,【Mybatis】mvc

 Mapper接口中有相應的方法,與sql映射文件相對應【Mybatis】app

相關文章
相關標籤/搜索