Maven+Spring+SpringMVC+MyBatis搭建环境心得

本来没得什么问题 但是一到maven问题就多了,IDE都要出问题 各种bug不断啊
1.首先建立一个maven webapp项目:此处省略好多字
目录结构:
2.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/xsd/maven-4.0.0.xsd">
     <modelVersion >4.0.0 </modelVersion>

     <parent>
           <artifactId> qunar-supom-generic</ artifactId>
           <groupId> qunar.common</groupId >
           <version> 1.3.0</ version>
     </parent >

     <groupId >com.qunar.examing </groupId>
     <artifactId >examing-Thrid</artifactId >
     <version >0.0.1-SNAPSHOT </version>
     <packaging >war </packaging>

     <name >examing-second</ name>
     <url >http://tc.corp.quanr.com </url>

     <properties >
           <project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
           <bds.bds_mode> nexus</ bds.bds_mode>
     </properties >

     <dependencyManagement >
           <dependencies>
               <dependency>
                    <groupId> com.google.guava</groupId >
                    <artifactId> guava</artifactId >
                    <version> 17.0</ version>
               </dependency>
           </dependencies>
     </dependencyManagement >
     <dependencies >
           <!-- servlet api -->
           <dependency>
               <groupId> javax.servlet</groupId >
               <artifactId> servlet-api</artifactId>
               <scope> provided</ scope>
           </dependency>

           <!-- velocity -->
           <dependency>
               <groupId> org.apache.velocity</groupId >
               <artifactId> velocity</ artifactId>
           </dependency>
           <dependency>
               <groupId> velocity-tools</groupId >
               <artifactId> velocity-tools</artifactId >
               <version> 1.4</ version>
               <scope> runtime</ scope>
           </dependency>

           <!-- spring module -->
           <dependency>
               <groupId> org.springframework</groupId >
               <artifactId> spring-webmvc </artifactId>
           </dependency>
           <dependency>
               <groupId> org.springframework</groupId >
               <artifactId> spring-tx</artifactId >
           </dependency>
           <dependency>
               <groupId> org.springframework</groupId >
               <artifactId> spring-jdbc</artifactId >
           </dependency>
           <dependency>
               <groupId> org.springframework</groupId >
                <artifactId> spring-context-support</artifactId >
           </dependency>

           <!-- mybatis module -->
           <dependency>
               <groupId> org.mybatis</groupId >
               <artifactId> mybatis</artifactId >
           </dependency>
           <dependency>
               <groupId> org.mybatis</groupId >
               <artifactId> mybatis-spring </artifactId>
           </dependency>

           <!-- mysql driver -->
           <dependency>
                <artifactId> mysql-connector-java </artifactId>
               <groupId> mysql</groupId >
           </dependency>
           <dependency>
               <groupId> org.apache.tomcat</groupId >
               <artifactId> tomcat-jdbc</artifactId>
           </dependency>

           <!-- common module -->
           <dependency>
               <groupId> org.codehaus.jackson</groupId >
               <artifactId> jackson-mapper-asl</artifactId >
           </dependency>
           <dependency>
               <groupId> org.slf4j</groupId >
               <artifactId> jcl-over-slf4j </artifactId>
           </dependency>
           <dependency>
               <groupId> ch.qos.logback</groupId >
               <artifactId> logback-classic </artifactId>
           </dependency>
           <dependency>
               <groupId> commons-digester</groupId >
               <artifactId> commons-digester</artifactId >
               <version> 1.8</ version>
               <scope> runtime</ scope>
               <exclusions>
                    <exclusion>
                         <groupId> commons-logging</groupId >
                         <artifactId> commons-logging</artifactId >
                    </exclusion>
               </exclusions>
           </dependency>
           <dependency>
               <groupId> com.google.guava</groupId >
               <artifactId> guava</artifactId >
           </dependency>
           <dependency>
               <groupId> commons-dbcp </groupId>
               <artifactId> commons-dbcp </artifactId>
           </dependency>

     </dependencies >

</project>




3.配置web.xml

<?xml version= "1.0" encoding ="UTF-8"?>
<web-app version= "2.5" 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_2_5.xsd" >
     <display-name >springmvc-chapter2</ display-name>
<!--配置DispatchServlet-->
     <servlet >
           <servlet-name> hello</ servlet-name>
           <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class >
          <!--表示DispatchServlet的位置-->
           <init-param>
                <param-name> contextConfigLocation</param-name >
                <param-value> classpath:/org/sh/ xml/hello-servlet.xml </param-value>
           </init-param>
           <load-on-startup> 1</ load-on-startup>
     </servlet >
      <!--配置Spring-->
     <context-param >
           <param-name> contextConfigLocation</param-name >
           <param-value>  
          classpath:/org /sh/xml/applicationContext.xml 
      </param-value >
     </context-param >
      <!--配置监听器-->
     <listener >
           <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
     </listener >

     <!-- POST中文乱码过滤器 -->
     <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 >
     <servlet-mapping >
           <servlet-name> hello</ servlet-name>
           <url-pattern> /</ url-pattern><!-- 捕获所有请求 -->
     </servlet-mapping >
</web-app>



4.src/main/resources/org/sh/xml中建立applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:p= "http://www.springframework.org/schema/p"
      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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
      default-autowire="byName" default-lazy-init= "false">

     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
           destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
           p:url= "jdbc:mysql://127.0.0.1:3306/test" p:username="root"        p:password="root"
           p:maxActive="10" p:maxIdle="10">
     </bean >
<!--配置事物管理-->
     <bean id="transactionManager"
           class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
           <property name= "dataSource" ref ="dataSource" />
     </bean >
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
           <!--dataSource属性指定要用到的连接池 -->
           <property name= "dataSource" ref ="dataSource" />
           <!--configLocation属性指定 mybatis的核心配置文件 -->
           <property name= "configLocation" value="classpath:/org/sh/xml/mybatis-config.xml" />
           <!-- 所有配置的 mapper文件 -->
           <property name= "mapperLocations" value="classpath:/org/sh/xml/mapper/user.xml" />
     </bean >
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
           <property name= "basePackage" value ="org.sh.dao" />
     </bean >
</beans>

MapperScannerConfigurer

没有必要在 Spring 的 XML 配置文件中注册所有的映射器。相反,你可以使用一个 MapperScannerConfigurer , 它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean。

要创建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代码:
 
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
           <property name= "basePackage" value ="org.sh.dao" />
     </bean >
</beans>


5.在src/main/resources/org/sh/xml中建立hello-servlet.xml

命名方式web.xml中DispatcherServlet的名加上 -servlet
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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">
     <mvc:annotation-driven /><!--打开注解功能-->
     <context:component-scan base-package="org.sh.controller" ></context:component-scan><!--自动扫描自定包中的类,-->
     <bean
           class= "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!--配置使用的视图 和 前缀和后缀-->
     <bean
           class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
           <property name= "prefix" value="/WEB-INF/jsp/" ></property>
           <property name= "suffix" value =".jsp"></ property>
     </bean >
</beans>




6.建立mybatis-config.xml

<?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 alias= "User" type ="org.sh.vo.User" />
     </typeAliases >
     <!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 -->
     <!-- <environments default="development"> <environment id="development">
          <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver"
          value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:// localhost:3306/test"/>
          <property name=" username" value="root"/> <property name="password" value="root"/>
          </dataSource> </environment> </environments> -->
     <!--<mappers> <mapper resource=" org/sh /xml/user.xml" /> </mappers> -->
</configuration>



建立好的资源目录如下图所示:


7.user.xml

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

<mapper namespace="org.sh.dao.IUserDAO" >
     <select id="selectUserById" parameterType="int" resultType="User" >
          select *
          from user
          where id =#{id}
     </select >
     <resultMap type="User" id= "resultListUser">
           <id column= "id" property ="id" />
           <result column= "userName" property ="userName" />
           <result column= "userAge" property ="userAge" />
           <result column= "userAddress" property="userAddress" />
     </resultMap >
     <select id="selectUsers" parameterType="String" resultMap="resultListUser" >
          select *
          from user
          where userName like #{userName}
     </select >
     <!-- 执行增加操作的SQL语句。id和parameterType 分别与IUserOperation接口中的addUser方法的名字和 参数类型一致。以#{name}的形式引用Student参数
          的name属性,MyBatis将使用反射读取Student参数 的此属性。#{name}中name大小写敏感。引用其他 的gender等属性与此一致。seGeneratedKeys设置
          为"true"表明要MyBatis获取由数据库自动生成的主 键;keyProperty="id"指定把获取到的主键值注入 到Student的id属性 -->
     <insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty= "id" >
          insert
          into user( username,nickname,email,address)
          values(#{ username},#{nickname},#{email},#{address});
     </insert >

     <!-- User 联合文章进行查询 方法之一的配置 (多对一的方式) -->
     <!-- <resultMap type="Article" id="resultUserArticleList"> <id property="id"
          column="aid" /> <result column="title" property="title" /> <result column="content"
          property="content" /> <association property="user" javaType="User"> <id column="id"
          property="id" /> <result column="userName" property="userName" /> <result
          column="userAddress" property="userAddress" /> </association> </resultMap>
          <select id="getUserArticle" parameterType=" int" resultMap="resultUserArticleList">
          select user.id,user.userName,user.userAddress,article.id aid,article.title,article.content
          from user,article where user.id = article.userid and user.id=#{id} </select> -->
</mapper>





7.继续 建立如下图的java文件


IUserDAO.java
public interface IUserDAO {
     public List<User> selectUsers() throws Exception;
     public void addUser(User u) throws Exception;
}
HelloController.java
package org.sh.controller;
@Controller
public class HelloController {
      //自动织入
     @Autowired     
     private IUserDAO userMapper;

      //映射路劲 也就是访问路劲
     @RequestMapping(value = { "/", "/hello" })
     public String addUser(Model model) {
          User user = new User();
          user. setId(1);
          user.setUsername( "苏浩");
          user.setNickname( "sh");
          user.setEmail( "727913675@qq.com");
          user.setAddress( "qd.tech");
           try {
               userMapper.addUser(user);
          } catch (Exception e) {
              e.printStackTrace();
          }
          System. out.println("hello world" );
          //返回视图
           return "addUser" ;
     }
}


然后在/WEB-INF/jsp/ 中建立addUser.jsp

将项目打war包,放入tomcat 启动tomcat war包自动解压。可以随意更改文件夹的名字

在浏览器中输入:http://localhost:8080/examing-Thrid-0.0.1-SNAPSHOT/  或者http://localhost:8080/examing-Thrid-0.0.1-SNAPSHOT/hello/(对于我这个demo而言)  效果如下:
ClassPathb表示当前路径






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值