`
xiegqooo
  • 浏览: 15583 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring_mvc + spring + mybaist 集成

阅读更多

1、maven 构建的依赖

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- log4j 日志记录 -->

<log4j.version>1.2.17</log4j.version>

<slf4j.version>1.7.5</slf4j.version>

<!-- spring 和 mybatis 通过 dbcp 对 mysql 数据库进行操作 -->

<mysql.connector.version>5.1.16</mysql.connector.version>

<mybatis.version>3.1.1</mybatis.version>

<mybatis.spring.version>1.1.0</mybatis.spring.version>

<commons.dbcp.version>1.2.2</commons.dbcp.version>

<commons.collections.version>3.2.1</commons.collections.version>

<spring.version>3.2.3.RELEASE</spring.version>

<!-- jetty -->

<jetty.server.version>9.0.4.v20130625</jetty.server.version>

<jetty.jsp2.version>7.2.2.v20101205</jetty.jsp2.version>

<glassfish.jsp2.version>2.1.v20100127</glassfish.jsp2.version>

<!-- 通用操作类 -->

<commons.taxi.version>1.0-SNAPSHOT</commons.taxi.version>

</properties>

 

<dependencies>

<!-- junit 3.8 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

 

<!-- mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

 

<!-- mybatis-spring -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>${mybatis.spring.version}</version>

</dependency>

 

<!-- mysql -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.connector.version}</version>

<type>jar</type>

</dependency>

 

<!--commons-dbcp,测试过程中,用1.4版的出现问题,而选择1.2.2版本的没问题 -->

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>${commons.dbcp.version}</version>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>${commons.collections.version}</version>

</dependency>

 

<!-- 用户常用类 -->

<dependency>

<groupId>com.hdsx.taxi.common</groupId>

<artifactId>common</artifactId>

<version>${commons.taxi.version}</version>

</dependency>

 

<!-- spring-web -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</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-beans</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

 

<dependency>

<groupId>javax.annotation</groupId>

<artifactId>jsr250-api</artifactId>

<version>1.0</version>

</dependency>

 

<!-- jetty 支持 jsp -->

<dependency>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-webapp</artifactId>

<version>${jetty.server.version}</version>

<scope>provided</scope>

</dependency>

 

<!-- 用户常用类 -->

<dependency>

<groupId>com.hdsx.taxi.common</groupId>

<artifactId>common</artifactId>

<version>${commons.taxi.version}</version>

</dependency>

 

</dependencies>

 

2、web.xml 项目配置

 

<servlet>

<servlet-name>spring-mvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:/spring.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring-mvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

3、spring 设置bean

<!-- 使用 注解  -->

<context:annotation-config />

 

<!-- 激活@Controller模式 -->

    <mvc:annotation-driven />

    

    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->

    <context:component-scan base-package="com.hdsx.taxi.oauth" />

    

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO --> 

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    

<!-- 定义视图 -->

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

        <property name="prefix">

            <value>/WEB-INF/jsp/</value>

        </property>

        <property name="suffix">

            <value>.jsp</value>

        </property>

    </bean>

    

<!-- 加载初始化设置 -->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>classpath:jdbc.properties</value>

</property>

</bean>

 

<!-- dbcp 数据池  -->

<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="20" />

<property name="maxWait" value="1000" />

<property name="removeAbandoned" value="false" />

<property name="removeAbandonedTimeout" value="120" />

<property name="defaultAutoCommit" value="true" />

</bean>

 

<!-- mybatis -->

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

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

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

</bean>

 

<!-- 事务管理 -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

</bean>

 

4、mybatis 

 

<configuration>

<!-- <typeAliases> -->

<!-- <typeAlias type="com.hdsx.taxi.oauth.pojo.Users" alias="users"/> -->

<!-- </typeAliases> -->

<mappers>

<mapper resource="mybatis/mapper/DevelopMapper.xml" />

</mappers>

</configuration>

 

<mapper namespace="DevelopMapper">

 

 <insert id="insertDevelopMapper" parameterType="com.hdsx.taxi.oauth.pojo.Develop">

  INSERT INTO `develop` (`id`,`project`,`username`,`password`,`back`,`state`)

VALUES (#{id},#{project},#{username},#{password},#{back},#{state})

 </insert>

 

 <select id="findByProject" resultType="com.hdsx.taxi.oauth.pojo.Develop">

 SELECT `id`,`project`,`username`,`password`,`back`,`state` FROM develop WHERE `project` = #{project}

 </select>

 

 <select id="findByUsername" resultType="com.hdsx.taxi.oauth.pojo.Develop">

 SELECT `id`,`project`,`username`,`password`,`back`,`state` FROM develop WHERE `username` = #{username}

 </select>

 

</mapper>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics