Spring+SpringMVC+Mybatis框架整合步驟

1. db.propertieslog4j.properties

這里是對數據庫的參數進行一些配置,為了防止硬編碼不利于系統的優化。(在與springmvc整合后,由spring管理)

2. SqlMapConfig.xml配置

只需定義別名

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 定義別名。配置批量掃描po包 -->
    <typeAliases>
        <!-- 批量別名定義。指定包路徑,mybatis自動掃描包下邊的pojo類,自動定義別名,別名默認為類名(首字母小寫或大寫) -->
        <package name="po" />
    </typeAliases>

</configuration>

解釋

<typeAliases>
        <!--  <typeAlias type="zq.mybatis.test1.User" alias="_User"/> -->    
        <package name="zq.mybatis.test1"/>
</typeAliases>

使用package設置別名的手該如何運用這個別名呢?很簡單,我指定了他的包名,那這個包下面的所有實體相當于已經被設置了別名,而這個別名實際上就是某一個實體自己的實體名。
在對應的映射文件中將使用別名:

<update id="updateUser" parameterType="User">   <!-- parameterType="User"就是使用了別名User -->
        update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getAllUser" resultType="User">      <!-- resultType="User"就是使用了別名User -->
    select * from users
</select>

3. applicationContext.xml

配置數據源、SqlsessionFactory、MapperScannerConfigurer(mapper掃描器)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-------------------------------1.dao層------------------------------->
    <!-- 1.配置數據源 -->
    <!-- 加載配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 數據庫連接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- 2.SqlsessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    </bean>

    <!--3.MapperScannerConfigurer:mapper的掃描器,將包下邊的mapper接口自動創建代理對象,
    自動創建到spring容器中,bean的id是mapper的類名(首字母小寫) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

<!-------------------------------2.service層------------------------------->
    <!--配置service的實現class-->
    <!-- 商品管理的service -->
    <bean id="itemsService" class="service.impl.ItemsServiceImpl"/>

<!-------------------------------3.transaction層------------------------------->
     <!-- 事務管理器 -->
     <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
     </bean>
     
     <!-- 通知 -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
     </tx:advice>
     
     <!-- aop -->
     <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* service.impl.*.*(..))"/>
     </aop:config>

</beans>

4. 使用myBatis-generator工具產生對應的mapper.java接口和mapper.xml實現

分別粘貼到cn.itcast.ssm.pocn.itcast.ssm.mapper包下

5. Junit測試

ItemsMapperTest.java

public class ItemsMapperTest {
    private ApplicationContext applicationContext;
    private ItemsMapper itemsMapper;

    @Before
    public void setUp() throws Exception {
        // 創建spring容器
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
    }

    // 根據主鍵查詢
    @Test
    public void testSelectByPrimaryKey() {
        Items items = itemsMapper.selectByPrimaryKey(1);
        System.out.println(items);
    }
}

6. po和mapper重構

繼續在po下增加ItemsCustom.javaItemsQueryVo.java
在mapper下增加ItemsMapperCustom.java、ItemsMapperCustom.xml

7. 完成service模塊

UserService.java

a

UserServiceImpl.java

b

8. 完成controller模塊

ItemsController.java

9. 配置springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 1.使用spring組件掃描 --><!-- 開啟注解掃描功能 -->  
    <context:component-scan base-package="controller" />

    <!-- 2.通過annotation-driven可以替代下邊的處理器映射器和適配器 -->
    <mvc:annotation-driven conversion-service="conversionService">
    </mvc:annotation-driven>

     <!--*注解處理器映射器 -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    </bean>
    <!-- *注解適配器 -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 在webBindingInitializer中注入自定義屬性編輯器、自定義轉換器 -->
        <property name="webBindingInitializer" ref="customBinder"></property>
    </bean>


    <!-- 3.配置視圖解析器ViewResolver。要求將jstl的包加到classpath -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

10. 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

<!-- 配置spring容器監聽器 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 前端控制器DispatcherServlet -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 加載springmvc配置 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 配置文件的地址 如果不配置contextConfigLocation,默認查找的配置文件名稱classpath下的:servlet名稱+"-serlvet.xml"即:springmvc-serlvet.xml -->
        <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

11. 寫jsp頁面

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容