码迷,mamicode.com
首页 > 编程语言 > 详细

使用maven整合spring+springmvc+mybatis

时间:2018-09-16 15:56:01      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:character   tis   loader   port   release   div   resource   VID   err   

使用maven整合spring+springmvc+mybatis

开发环境:

    1.  jdk1.8

    2.  eclipse4.7.0 (Oxygen)

    3.  mysql 5.7

  在pom.xml文件中,引入我们需要的jar包:(按需引入jar包)

   <properties>
    <!-- 自定义的spring的版本号 -->
    <spring.version>4.3.18.RELEASE</spring.version>
    <!-- 自定义的mybaits的版本号 -->
    <mybatis.version>3.4.5</mybatis.version>
    <!-- 自定义的mybaits-spring的版本号 -->
    <mybatis.spring.version>1.3.1</mybatis.spring.version>
  </properties>
  
  <dependencies>
  <!-- springMVC的jar包,由于springMVC依赖 springBeans,springContext,springCore 所以这些jar包会被导入进来-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- spring-tx 事务 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- aspectjweaver aop 依赖该jar包-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
    
    
    
    <!-- servlet的jar包 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>  <!-- 编译时,不会被打包 -->
    </dependency>
    <!-- servlet.jsp-api -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- javax.servlet.jsp.jstl-api -->
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.2</version>
    </dependency>
    <!-- taglibs-standard-impl jstl的实现 -->
    <dependency>
        <groupId>org.apache.taglibs</groupId>
        <artifactId>taglibs-standard-impl</artifactId>
        <version>1.2.5</version>
    </dependency>
    
    

    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis.spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!--druid 连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
        <scope>compile</scope>
    </dependency>
    <!-- mysql的连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>

    <!--log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  
  
  </dependencies>

  在Maven dependencies中可以看到jar包,说明引入成功了

   技术分享图片

  定义目录结构:

  技术分享图片

  配置文件:

    db.properties

jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/db_ssm
jdbc_username=root
jdbc_password=123456

  mybatis-config.xml

<!--mybatis的总配置文件! -->
  <settings>
    <!--  配置mybatis的日志  -->
     <setting name="logImpl" value="LOG4J"/>
     <!--开启延迟加载  -->
     <setting name="lazyLoadingEnabled" value="true"/>
     <!--设置不要积极去加载  -->
     <setting name="aggressiveLazyLoading" value="false"/>
     <!--延迟加载的触发方法  -->
     <setting name="lazyLoadTriggerMethods" value="clone"/>
  </settings>
  <!-- 别名 -->
  <typeAliases>
      <package name="com.login.dao"/>
  </typeAliases>

 

  applicationContext-dao.xml

<!-- 
   spring 整合 mybatis 的配置
         配置数据库连接池
         配置sqlSessionFactory
         配置扫描器
         配置扫描所有的service类
 -->
 <!-- 加载db.properties文件 -->
 <context:property-placeholder location="classpath:db.properties"/>
 
 <!-- 配置数据库连接池 -->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <property name="driverClassName" value="${jdbc_driverClassName}"></property>
     <property name="url" value="${jdbc_url}"></property>
     <property name="username" value="${jdbc_username}"></property>
     <property name="password" value="${jdbc_password}"></property>
 </bean>
 
 <!-- 配置sqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据库连接池 -->
     <property name="dataSource" ref="dataSource"></property>
    <!-- mapper文件 -->
     <property name="mapperLocations">
     <!--加载所有的mapper文件  -->
      <list>
          <value>classpath:mappers/*.xml</value>
      </list>
     </property>    
    <!-- mybatis的配置文件 -->
     <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
 </bean>
 
 <!-- 配置mapper扫描器 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.login.dao"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
 </bean>
 <!--扫描所有的service  -->
 <context:component-scan base-package="com.login.service"></context:component-scan>

  applicationContext-tx.xml

<!-- 配置spring的事务
          mybatis采用的是spring中的jdbc中的事务管理(配置事务管理器)
          事务增强
          aop切面
  -->
  <!--事务管理  -->
  <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <!-- 这里的DataSource是引用的applicationContext dao文件中的 -->
     <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 增强 -->
  <tx:advice id="tx_advice" transaction-manager="tx" >
    <tx:attributes>
        <!-- 事务传播规则 -->
         <tx:method name="save*" propagation="REQUIRED"/>
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="delete*" propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
         <tx:method name="do*" propagation="REQUIRED"/>
         
         <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
         <tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
         <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
         <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
         <tx:method name="count*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>
  
  <!-- Aop 切面 
       proxy-target-class="false"  是默认的 =====》 默认使用jdk动态代理 基于接口
       proxy-target-class="true"   =============》使用cglib动态代理 基于类
  -->
  
  <aop:config  proxy-target-class="false">
    <aop:advisor advice-ref="tx_advice" pointcut="execution(* com.login.service.*Service.*(..) )"/>
  </aop:config>

  springmvc.xml

<!--
      springmvc的配置文件
                  扫描handler对象
                  配置注解的处理器映射器和处理器适配器
                  配置内部资源视图解析器
                  处理不了的静态资源交给tomcat
   -->
   
   <context:component-scan base-package="com.login.web.controller"></context:component-scan>
   
   <mvc:annotation-driven></mvc:annotation-driven>
 
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   
      <property name="prefix" value="/"></property>
      
      <property name="suffix" value=".jsp"></property>
   
   </bean>
   
   <mvc:default-servlet-handler/>

  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>forceRequestEncoding</param-name>
         <param-value>true</param-value>
  </init-param>
  <init-param>
         <param-name>forceResponseEncoding</param-name>
         <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- web IOC容器  -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 加载监听器 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置spring mvc的前端控制器 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 本地的上下文配置   加载springmvc的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动容器的时候加载 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

  配置完成后,写一个简单的用户登录操作:

@Controller
public class UserController {
    
    @Autowired
    private IUserService service;
    
    @RequestMapping(path="/login")
    public String login(String username,String password,Model m) {
        
        User user = service.login(username, password);
        
        m.addAttribute("user", user);
        
        return "success";
        
    }

}
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;
    
    @Override
    public User login(String username, String password) {
        
        //参数验证是否为空
        
        //参数判断--》账号,密码是否正确
        
        return  mapper.getUserByUsername(username);
    }

}
 <mapper namespace="com.login.dao.UserMapper">
  
  <select id="getUserByUsername"  resultType="com.login.entity.User">
  
      SELECT * FROM user WHERE username = #{username}
  
  </select>
  
  </mapper>

  然后启动tomcat:

技术分享图片

技术分享图片

 总结: 使用maven可以让我们不用担心找不到jar包。maven同时也为我们规定好了一些目录结构,方便我们的开发,但是maven的强大远不如此。让我们一起来学习吧!希望以上这些可以帮到需要帮助的朋友!

 

使用maven整合spring+springmvc+mybatis

标签:character   tis   loader   port   release   div   resource   VID   err   

原文地址:https://www.cnblogs.com/start-21/p/9655865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!