Maven以及SpringMVC配置

2017.1.16

配置maven项目

eclipse新建maven project

next勾选create a simpleproject ----大概就是在新建工程的时候不要加一些乱七八糟的东西

GroupId = duck 这个名称也是自定义的

ArtifactId = 项目名称

Packaging 选择 war    ---------------------因为我们要创建的是web工程

Maven工程下包括一个配置文件 pom.xml

刚刚填写的信息可以在pom.xml文件里面看到

另外这个重要的pom文件还是用来管理 jar包的

Maven工程没办法自己导包 必须用pom文件管理jar包

新建的工程需要检查 webapp目录

这个路径是固定的 src/main/webapp

下面的子目录包括 WEB-INF

WEB-INF 目录用于存放web应用配置文件web.xml 以及页面

至此,还需要配置的是server runtime包 ----服务器包

右击工程---点击build path---在build path页面的libraries 页面下选择 add libraries

添加Server runtime   需要已经配置好的tomcat

缺少server runtime 所有jsp页面以及源码包会报错

这样一个web工程算是创建好了

添加SpringMVC框架

什么是框架?

一个工程需要层次分明的管理代码,以及代码重复使用的效率,框架是一个抽象的东西,更像一个说明书,清楚的记录了怎么安排我们的代码,另外,框架还能提供很多实用的接口,让开发变得方便快捷。

这就是为什么说程序员不重复造轮子,前辈已经开发好了可供使用的代码,后续的开发就是利用这些代码做一个真正实用的项目。

使用框架让我们知道该怎么安排前端到后台的代码,不使用框架也可以做到。差别就在,比如一个整洁的房间(使用框架),家具各就其位,我们从一开始就知道每张桌椅摆放位置。(不使用框架)家具可以随意摆放,看起来杂乱无章。

springmvc:

先用maven找到需要的包。

这里,直接打开pom文件的dependencies页面选择 add

可以直接搜索spring 

这里先添加一个包 spring-webmvc 在最下面

点击确认 保存pom文件

右击工程

选择maven选项下面的update project

更新pom新添加的jar包  maven会自动下载好 非常方便

然后就是配置spring框架了

参见以前的笔记:

2016/6/5

昨晚做的,今天上来记录;

找了很多百度的教程,各种眼花缭乱,最后在CSDN上发了求助帖,然后就在那个时候在CSDN上找到了一个视频:http://edu.csdn.net/course/detail/1516

对照视频,把web.xml && applicationContext.xml两个文件简单的配置了一下,总算是实现了最简单的spring框架;

下面附记这两个文件的代码:

<?xml version="1.0"encoding="UTF-8"?>
<web-app>
<servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*.do</url-pattern>
</servlet-mapping>

   <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
   </welcome-file-list>
</web-app>
<?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:tx="http://www.springframework.org/schema/tx"
    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.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.dcb.controller"></context:component-scan>
<--扫面controller所在的包 包名填上-->
    <bean
    <--jsp所在的文件夹映射。。。-->
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
</beans>

没那么复杂,可是为什么网上很多教程都附带了很多其他的东西,可能还有更简单的;end

关于applicationContext.xml 的路径

晚上在配置的时候

<init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
</init-param>

加上classpath*:

applicationContext可以放在resource目录下,如果没有这一句或者是classpath:并不能找到

spring框架找不到配置文件 启动会报错

applicationContext默认的路径是webapp目录下

弄完这些就可以写controller测试了

@Controller

@RequestMapping("test")

这两个注解都是可以用的

2017-1-22

在引入mybatis和mysql驱动jar的时候上面新建的maven工程报错:

dynamic web module 3.0requires 1.6

查看工程使用的jdk版本发现是java 1.5

尝试过手动更改—点开BuildPath 不过里面东西实在太多而且之后还有更改工程文件

找到另一个办法

在POM文件里加上

然后更新一下maven就可以完美解决jdk版本问题

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2017-1-30

在web.xml增加了新内容

    <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>

        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

字符编码

2017-2-23

http://blog.csdn.net/jiuqiyuliang/article/details/45132493

spring与Mybatis集成配置

2017-2-26

<context:property-placeholder location="classpath*:jdbc.properties"ignore-resource-not-found="true"/>

<bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close">
<property name="driverClassName"value="com.mysql.jdbc.Driver" />
<property name="url"value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username"value="root" />
<property name="password"value="123456" />
<property name="initialSize"value="1" />
<property name="maxActive"value="20" />
<property name="maxWait"value="60000"/>
<property name="maxIdle"value="30" />
<property name="minIdle"value="1" />
<property name="timeBetweenEvictionRunsMillis"value="60000"/>
<property name="minEvictableIdleTimeMillis"value="300000"/>
<property name="validationQuery"value="SELECT 'x'"/>
<property name="testWhileIdle"value="true"/>
<property name="testOnBorrow"value="false"/>
<property name="testOnReturn"value="false"/>
</bean>

     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource" />
         <!-- <property name="configLocation"value="classpath*:mybatis.xml" /> -->
     </bean>

     <!-- mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory 
          basePackage:指定sql映射文件/接口所在的包(自动扫描) 
     --> 

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="basePackage"value="paper"></property> 
        <property name="sqlSessionFactory"ref="sqlSessionFactory"></property> 
   </bean> 

   <!--DataSourceTransactionManagerdataSource:引用上面定义的数据源--> 
   <bean id="txManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource"ref="dataSource"></property> 
   </bean> 

   <!-- 使用声明式事务 transaction-manager:引用上面定义的事务管理器 --> 
<tx:annotation-driven transaction-manager="txManager" />

第一行,获取的配置文件jdbc.properties出错,没有办法引用例如 ${jdbc.Driver} ---待解决

因此将引用部分直接写上路径: 包括数据库URL用户名 密码

注意MapperScannerConfigurer的扫描路径path

Mapper.xml:

<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="paper.dao.TestMapper">

   <resultMap type="paper.dao.TestMapper" id="baseResult">
        <id column="id"property="id" jdbcType="INTEGER"/>
        <result column="uid"property="uid" jdbcType="VARCHAR"/>
   </resultMap>

    <select id="getTest" resultType="paper.model.TestModel"parameterType="paper.model.TestModel">
        select*
        from testdate
    </select>

</mapper>

这里可以使用resultMap或者resultType来返回结果集

过去mis使用的都是resultMap

第一次发现resultType 挺好用的:后面直接加对象的路径

具体细节还有待深入学习。。。

猜你喜欢

转载自blog.csdn.net/qq_29126023/article/details/78957870