Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合

项目环境背景:

操作系统:win7

JDK:1.7

相关依赖包,截图如下:

项目目录截图:

com.sgl.controller:存放Controller相关类

在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Controller 标记一个类是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的方法参数灵活的获取到.

com.sgl.model:存放与数据库对应的对象模型

com.sgl.dao:放一些处理model类的接口类和接口实现类

com.sgl.mapper:除了SqlMapper之外,其他的mapper与对应的DAO是一样的,都是一些接口。SqlMapper是其他mapper的父类。

com.sgl.mapping:存放对应model的所有数据库操作

com.sgl.service:业务逻辑处理

 

下面是所有用到的配置文件介绍:

第一个是spring 的配置文件applicationContext-common.xml

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

    <!--base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 -->
    <context:component-scan base-package="com.sgl" />

    <!--配置 dateSource 数据源 。 这里使用spring管理mybatis的数据库相关配置,所以在mybatis.xml中就不需要配置了 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/electDB</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>123</value>
        </property>
    </bean>

    <!-- 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-Spring 
        中,则使用 SqlSessionFactoryBean 来替代。 -->

    <!-- 配置sqlSessionFactionBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-- 指定数据源 ,dataSource为 上面配置的dataSource -->
        <property name="dataSource" ref="dataSource" />

        <!--configLocation属性指定mybatis的核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis.xml" />

        <!-- 如果 MyBatis映射器 XML文件在和映射器类相同的路径下不存在,那么另外一个需要 配置文件的原因就是它了。使用这个配置,有两种选择。第一是手动在 
            MyBatis 的 XML 配 置文件中使用<mappers>部分来指定类路径。第二是使用工厂 bean 的 mapperLocations 属 
            性。 mapperLocations 属性使用一个资源位置的 list。 这个属性可以用来指定 MyBatis 的 XML 映射器文件的位置。 它的值可以包含 
            Ant 样式来加载一个目录中所有文件, 或者从基路径下 递归搜索所有路径 -->
        <property name="mapperLocations">
            <list>
                <!-- 表示在com.sgl.mapper包或以下所有目录中,所有以.xml结尾的文件 -->
                <value>classpath:com/sgl/mapping/*.xml</value>
            </list>
        </property>
    </bean>
    

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sgl.mapper" />
    </bean>
     <!--创建数据映射器,数据    射器必须为接口-->  
    <bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.sgl.mapper.ElectUserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="BATCH" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />


</beans>
View Code

第二个是mybatis的配置文件,由于spring 已经将其数据库相关连接配置管理了,这mybatis中需要做的事情比减少。我这里基本没有做什么处理。只是映射一下model的别名。

<?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>
    <!-- 别名列表 -->
    <typeAliases>
        <typeAlias type="com.sgl.model.ElectUser" alias="electuser" />
    </typeAliases>
    
    
</configuration>
View Code

第三个是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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" 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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">    
    <!-- 注解探测器  启动时扫描所有的controller -->
    <context:component-scan base-package="com.sgl" />
    
    
    <!-- annotation默认的方法映射适配器 -->
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean id="handlerAdapter"
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <!-- 定义视图解释器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

第四个是mybatis的映射配置文件

<?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">
<!-- 处理electuser所有的数据库操作 -->
<mapper namespace="com.sgl.mapper.ElectUserMapper">
    <resultMap type="electuser" id="ElectUserResultMap">
        <id property="id" column="id" />
        <result property="username" column="username" />
        <result property="password" column="password" />
        <result property="realname" column="realname" />
        <result property="email" column="email" />
        <result property="cellphone" column="cellphone" />
        <result property="sex" column="sex" />
        <result property="birthday" column="birthday" />
        <result property="regdate" column="regdate" />
        <result property="picpath" column="picpath" />
        <result property="roleid" column="roleid" />
        <result property="statue" column="statue" />
    </resultMap>
    <!-- 取所有的用户 -->
    <select id="getAllElectUser" resultType="list" resultMap="ElectUserResultMap">
        select * from electuser
    </select>
    <!-- 添加一条用户记录    electuser是在mybatiss中配置的别名-->
     <insert id="addElectUser" parameterType="electuser">
        insert into
        electuser(id,username,
        password,realname,email,cellphone,sex,birthday,regdate,picpath,roleid,statue)
        values(#{id}, #{username},
        #{password},#{realname},#{email},#{cellphone},#{sex},#{birthday},#{regdate},,#{picpath},#{roleid},#{statue})
    </insert> 


</mapper>

最后一个是web.xml配置文件。作为网站的入口,在这里需要将以上的一些配置文件进行加载,管理。网站的初始化等工作

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>


    <!-- 加载spring 容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 设置Spring容器加载配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>
    <!-- 设置 Spring核心控制器 -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- 编码过滤器解决系统编码问题 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

接下来将里面的一些类、接口做一些介绍。

model里面的就不做说明了,就是简单的实体类,与数据库对应字段就OK。

dao:

这里的accountMapper与applicationContext-common.xml里面 的对应

  com.sgl.mapper:

sqlmapper是所有mapper接口的父类接口,在这里说明一下,SqlMapper里面可以定义一些模板方法,也可以不定义,什么都不写。只是作为一个父类接口做逻辑上的关系。

com.sgl.service:

 

 Controller:

至此所有的代码完毕:在浏览器输入:http://localhost:端口/项目名称/Info/list.do,进行查看你的结果就行了。

posted @ 2015-08-31 09:22  爪哇小生  阅读(503)  评论(0编辑  收藏  举报