springmvc+spring+mybatis的整合(推荐新手看看)

下面开始整合(由于有点多就不一一介绍了)

配置sqlMapConfig.xml(空文件即可)

version="1.0" encoding="UTF-8" ?>
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

configuration>

applicationContext-dao.xml


version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<context:property-placeholder location="classpath:jdbc.properties" />


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>


<bean class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

<property name="dataSource" ref="dataSource" />
bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="cn.chuang.mapper" />
bean>
beans>

jdbc.properties(配置数据库信息)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

log4j.properties(配置日志信息)


# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

applicationContext-service.xml

version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<context:component-scan base-package="cn.chuang.service" />
beans>

applicationContext-trans.xml(配置事务管理)

version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<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="add*" propagation="REQUIRED" />
<tx:method name="create*" 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:method name="query*" propagation="SUPPORTS" read-only="true" />
tx:attributes>
tx:advice>


<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.chuang.service.*.*(..))" />
aop:config>

beans>

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-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="cn.chuang.controller" />


<mvc:annotation-driven />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/" />

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

web.xml


version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>boot-crmdisplay-name>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>


<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext-*.xmlparam-value>
context-param>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>


<filter>
<filter-name>encodingfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>


<servlet>
<servlet-name>boot-crmservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>



<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>boot-crmservlet-name>

<url-pattern>/url-pattern>
servlet-mapping>
web-app>

测试Dao

编写Bean

public class Bean {
private String id;
private String name;
   private String sort;
 get/set...
}

 

编写mapper.xml

version="1.0" encoding="UTF-8" ?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.chuang.mapper.Bean">

mapper>

完整图

springmvc+spring+mybatis的整合(推荐新手看看)_第1张图片

完整的pom(拷贝请看仔细)



xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

com.chuang
SSM
1.0-SNAPSHOT
war

SSM Maven Webapp

http://www.example.com

 
      4.2.4.RELEASE
      3.2.7
      1.7.12
      2.4.2
      1.1.2
      4.8.1
 


 
     
         
              junit
              junit
              ${spring.junit}
         

     

 

 
     
          javax.servlet
          servlet-api
          2.5
          provided
     

     
          javax.servlet.jsp
          jsp-api
          2.0
          provided
     

     
     
          org.springframework
          spring-core
          ${srping.version}
     

     
          org.springframework
          spring-jdbc
          ${srping.version}
     

     
          org.springframework
          spring-context
          ${srping.version}
     

     
          org.springframework
          spring-tx
          ${srping.version}
     

     
          org.springframework
          spring-context-support
          ${srping.version}
     

     
          org.springframework
          spring-aop
          ${srping.version}
     

     
          org.springframework
          spring-web
          ${srping.version}
     

     
          org.springframework
          spring-webmvc
          ${srping.version}
     

     
          org.aspectj
          aspectjweaver
          1.8.4
     

     
     
          org.mybatis
          mybatis
          ${mybatis.version}
     

     
          org.mybatis
          mybatis-spring
          1.2.2
     

     
     
          mysql
          mysql-connector-java
          5.1.8
          runtime
     

     
     
          com.alibaba
          druid
          1.0.9
     

     
     
          jstl
          jstl
          ${jstl.version}
     

     
          taglibs
          standard
          ${jstl.version}
     

     
     
          org.slf4j
          slf4j-api
          ${slf4j.version}
     

     
          org.slf4j
          slf4j-log4j12
          ${slf4j.version}
     

     
     
          commons-fileupload
          commons-fileupload
          1.2.2
     

     
          commons-io
          commons-io
          1.3.2
     

     
          commons-codec
          commons-codec
          1.6
     

     
     
          org.json
          json
          20131018
     

     
          com.fasterxml.jackson.core
          jackson-core
          ${jackson.version}
     

     
          com.fasterxml.jackson.core
          jackson-databind
          ${jackson.version}
     

     
          com.fasterxml.jackson.core
          jackson-annotations
          ${jackson.version}
     


     
     
          taglibs
          standard
          1.1.2
     


     
     
          org.apache.commons
          commons-text
          1.6
     




 

 
     
         
              org.apache.maven.plugins
              maven-compiler-plugin
              3.0
             
                 
                  1.8
                  1.8
             

         

     

     
         
              src/main/java
             
                  **/*.properties
                  **/*.xml
             

              false
         

     

 


 

你可能感兴趣的:(springmvc+spring+mybatis的整合(推荐新手看看))