springmvc和mybaties整合开发流程

来源:互联网 发布:手机数据损坏如何恢复 编辑:程序博客网 时间:2024/05/16 18:45

1.导入jar包
这里写图片描述

包括,数据库链接的包dhcp,spring-mvc,mybaties。
2.新建配置文件configure
db.properties,log4j.properties
3.逆向工程生成mapper和po类
4.编写sqlMapConfig.xml 文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 全局配置需要的时候再加<setting> --><!-- 批量扫描配置别名 --><typeAliases>    <package name="com.duola.po"/></typeAliases><!--这里我们不需要配置,我们可以利用spring和mybaties的整合包进行扫描但是需要遵守一些规则,mapper.xml和mapper.java名称相同 <map></map> --></configuration>

5.配置数据库applicationContext-dao.xml

<?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: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.0.xsd                        http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.0.xsd                        http://www.springframework.org/schema/aop                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!-- 加载数据库配置文件 -->    <context:property-placeholder location="classpath:db.properties"/>    <!-- 配置数据源 -->    <bean id="dataSource" class="org.apache.commons.dbcp2.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}" />                              </bean>    <!--配置session工厂 sqlSessionFactory -->        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />        <!-- value="classpath:mybaties/sqlMapConfig.xml" 文件路径不要写错,就没办法-->        <!-- <property name="configLocation" value="classpath:mybaties/sqlMapConfig.xml"/> -->    </bean>    <!-- mapper的扫描器 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.duola.mapper"></property>    <!--配置session工厂 sqlSessionFactory有个id叫sqlSessionFactory,如果使用sqlSessionFactory会造成 加载数据库配置文件不起作用 -->         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>    </bean></beans>

由于自己手抖写的bug:

driverClassName写成了driver<property name="driverClassName" value="${jdbc.driver}" />java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.sql.Driver] for property 'driver': no matching editors or conversion strategy found
<!-- value写成ref --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>java.lang.IllegalStateException: Cannot convert value of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] to required type [java.lang.String] for property 'sqlSessionFactoryBeanName': no matching editors or conversion strategy found

5.因为操作数据库所以要进行事物控制applicationContext-transaction.xml

<?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: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.0.xsd                        http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.0.xsd                        http://www.springframework.org/schema/aop                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!-- 事物管理 -->    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!-- 配置数据源 -->        <property name="dataSource" ref="dataSource"></property>    </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="delete*" propagation="REQUIRED" />            <tx:method name="update*" 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(* com.duola.service.imp.*.*(..))"/>    </aop:config></beans>
id="txAdvice"没写org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Configuration problem: Id is required for element 'advice' when used as a top-level tag

6.配置applicationContext-service.xml

在这里管理service,service不用加@service

<?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: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.0.xsd                        http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.0.xsd                        http://www.springframework.org/schema/aop                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                        http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <bean id="itemsService" class="com.duola.service.imp.ItemsServiceImp"></bean></beans>
  • 让配置文件加入到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>
  • 这里我的配置文件出现了问题

没写直接写的org.springframework.web.context.ContextLoaderListener,导致找不到service无法注入,因为就没找到service,applicationContext-service.xml没有添加成功。

  • 理解classpath的含义
    这里需要搞清楚classpath是什么,以及classpath:和classpath*有何区别:

    1. 首先 classpath是指 WEB-INF文件夹下的classes目录

    2. classpath 和 classpath* 区别:
      classpath:只会到你的class路径中查找找文件;
      classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

原创粉丝点击