909422229_IDEA+maven+Spring+SpringMVC+Mybatis环境整合

要有梦想,即使遥远:


部分主要代码已发,还有一些service与Dao没有发,我相信不是什么大问题,主要是配置文件的配置比较重要,

其他没什么,哈哈,有问题留言。感谢来访,欢迎关注。


源码留下:

http://download.csdn.net/download/a909422229/9970501

项目结构


pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wsh</groupId>
    <artifactId>ssm</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <!-- spring 版本号 -->
        <spring.version>4.0.2.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis-version>3.2.6</mybatis-version>
        <!-- 日志版本 -->
        <slf4j-version>1.7.7</slf4j-version>
        <log4j-version>1.2.17</log4j-version>
        <!-- Junit版本号-->
        <Junit-version>4.5</Junit-version>
        <!-- mysql驱动版本号 -->
        <mysql-driver.version>5.1.29</mysql-driver.version>
    </properties>
    <dependencies>
        <!--导入spring依赖的jar-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis-version}</version>
        </dependency>
        <dependency>
            <!-- mybatis-spring -->
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 添加mysql驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-driver.version}</version>
        </dependency>
        <!-- 添加数据库连接池依赖 -->
        <!--<dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.2.2</version>
        </dependency>-->
        <!--druids数据连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>


        <!-- 添加fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <!-- 添加日志相关jar-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-version}</version>
        </dependency>

        <!-- 其他JSON,springmvc配置文件中json绑定可能会用到 -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!--其他工具包-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${Junit-version}</version>
            <scope>test</scope>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- mybatis的自动生成dao.xml的插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
            </plugin>
        </plugins>
        <!--配置Maven resource文件 过滤 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>web-ssm</finalName>
    </build>
</project>

web.xml

<?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"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext_annotation.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 声明配置文件位置 给 前端控制器  -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc.xml 

<?xml version="1.0" encoding="GBK"?>
<!-- 
   配置文件约束:dtd  document type definition  
              xsd xml schema definition
   nsname space 命名空间 每一个schema的唯一标识
   作用:告知spring,生产哪些组件     
 -->
<beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"  
      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-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/mvc
                          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
   <!-- 要扫描的包,包中的类里的注解会被发现
        use-default-filters 默认为true
        true=只要类上包含  @Controller  @Service  @Repository   @Component其中一个,则类会被扫描
        false=不扫描任何类
   
    -->
    
   <context:component-scan base-package="com.wsh" use-default-filters="false">
      <!-- 只扫描 类上有  @Controller注解的-->
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
   <!-- 注册注解开发驱动,使得,扫描到的注解可以被解析 -->
   <mvc:annotation-driven>
      <mvc:message-converters>
         <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
               <list>
                  <!-- content-type -->
                  <value>application/json</value>
               </list>
            </property>
         </bean>
      </mvc:message-converters>
   </mvc:annotation-driven>
   <!-- 视图解析器:解析视图 
       解析行为:1.捕获后端的返回值 : xx
              2.为返回值拼接前缀 和 后缀  =  /xx.jsp
   -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/"></property>
      <property name="suffix" value=".jsp"></property>
   </bean>
   <!-- 
      有此配置,会额外追加一个映射=(/**):(DefaultServletHttpRequestHandler)
      /**= /a  /b  /a/b/c   /a/b/c.html   /a/b/c.jpg  /a/b/c.xxx
      handler在最后匹配请求=请求到达此handler时,会被转向对应的静态资源
    -->
   <mvc:default-servlet-handler/>
</beans>

Spring-Context.xml(spring配置文件)


<?xml version="1.0" encoding="GBK"?>
<!-- 
   配置文件约束:dtd  document type definition  
              xsd xml schema definition
   nsname space 命名空间 每一个schema的唯一标识
   作用:告知spring,生产哪些组件     
 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context" 
      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-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
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.2.xsd">
                          
   <!-- 获取路径下的数据库连接文件 -->
   <context:property-placeholder location="classpath:jdbc.properties"/>
   <!-- DataSource -->
   <!--<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
      <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
      <property name="username" value="hr"></property>
      <property name="password" value="hr"></property>
      &lt;!&ndash; 最大连接数 &ndash;&gt;
      <property name="maxActive" value="3"></property>
      &lt;!&ndash; 最小链接数 &ndash;&gt;
      <property name="minIdle" value="1"></property>
      &lt;!&ndash; 初始数量 &ndash;&gt;
      <property name="initialSize" value="1"></property>
      &lt;!&ndash; 超时时长  毫秒&ndash;&gt;
      <property name="maxWait" value="3000"></property>
   </bean>
   
   <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass" value="oracle.jdbc.OracleDriver"></property>
      <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
      <property name="user" value="hr"></property>
      <property name="password" value="hr"></property>
      &lt;!&ndash; 最大连接数 &ndash;&gt;
      <property name="maxPoolSize" value="3"></property>
      &lt;!&ndash; 最小链接数 &ndash;&gt;
      <property name="minPoolSize" value="1"></property>
      &lt;!&ndash; 初始数量 &ndash;&gt;
      <property name="initialPoolSize" value="1"></property>
      &lt;!&ndash; 超时时长  毫秒&ndash;&gt;
      <property name="checkoutTimeout" value="3000"></property>
   </bean>-->
   
   <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
      <!-- 基本属性  可以通过url,自动获取driverclass -->
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
      <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="${jdbc.init}" />
      <property name="minIdle" value="${jdbc.min}" /> 
      <property name="maxActive" value="${jdbc.max}" />
      <!-- 配置获取连接等待超时的时间 -->
      <property name="maxWait" value="${jdbc.maxWait}" />
      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
      <property name="timeBetweenEvictionRunsMillis" value="60000" />

      <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
      <property name="minEvictableIdleTimeMillis" value="300000" />
     <!-- 链接对象 有效验证 sql语句 -->
      <property name="validationQuery" value="SELECT 1 from dual" />
      <!-- 链接空闲时是否测试 -->
      <property name="testWhileIdle" value="true" />
      <!-- 链接检出时是否测试 -->
      <property name="testOnBorrow" value="false" />
      <!-- 链接返回池时是否测试 -->
      <property name="testOnReturn" value="false" />
   </bean>
   
   <!--Mybatis SqlSessionFactory工厂整合到Spring-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <!-- 连接池 -->
      <property name="dataSource" ref="druid"></property>
      <!-- mapper注册 -->
      <property name="mapperLocations">
         <list>
            <value>classpath:mybatis/*-mapper.xml</value>
         </list>
      </property>
      <!-- 可选:别名 
          定义了实体的包,则在mapper文件中使用实体时,直接写类名即可
      -->
      <property name="typeAliasesPackage" value="com.wsh.entity"></property>
   </bean>
   <!-- 
      MapperScannerConfigurer:构建DAO实现类,并将对象纳入工厂
      1.找到所有DAO接口
      2.根据提供的mappersqlSession,构建DAO的实现类
      3.将实现类的对象放入工厂
      4.并且每个DAO实现类对象在工厂中的ID:对应接口类名的首字母小写=userDAO
    -->
    
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!-- DAO接口文件位置:找到了所有需要定制实现类的DAO接口 -->
      <!-- value中可以有多个DAO的接口路径,用 ‘,’隔开  -->
      <!-- <property name="basePackage" value="com.c72.dao,com.c73.dao"></property> -->
      <property name="basePackage" value="com.wsh.dao"></property>
      <!-- dao对应的mapper文件      SqlSession  -->
      <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
      <!-- 如果工厂中只有一个SqlSessionFactory,则此设置可以省略 -->
      <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> -->
   </bean>

   <!-- 并不是真正的事务管理器,但是,其掌握着事务的管理逻辑 -->
   <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="druid"></property>
   </bean>
   <!-- 告知spring,哪些包中的类里有注解需要解析 扫描 -->
   <!-- <context:component-scan base-package="com.c72,com.c73,a.b.c"></context:component-scan> -->
   <context:component-scan base-package="com.wsh" use-default-filters="true">
      <!-- 只是不扫描 类上有  @Controller注解的-->
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
   <!-- 注册事务注解驱动,明确@Transactional切入的是哪个事务管理器 -->
   <tx:annotation-driven transaction-manager="tx"/>
   
</beans>

     
     

BookDaoImpl-Mapper.xml(MybatisMapper文件)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- namespace,指定要实现的接口 全限定名 -->
<mapper namespace="com.c72.dao.UserDAO">
	<!-- 添加 -->
	<insert id="insertUser" parameterType="User" >
		<selectKey resultType="java.lang.Integer" keyProperty="id" order="BEFORE">
			select user_seq.nextval from dual
		</selectKey>
		insert into t_user(u_id,username) values(#{id},#{name})
	</insert>
	
	<select id="queryUserByID" resultType="User">
		select user_id as id,username as name from ssm_user where user_id=#{id}
	</select>
</mapper>





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

75888丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值