zoukankan      html  css  js  c++  java
  • Spring+SpringMvc+Mybatis 框架的搭建(一)

    本文是因为实习结束后学习到了新的技术,想写下来和更多人交流。开发中遇到的问题我也会一一说明,希望有更多人可以互相探讨,加入到一起来。

    1. Spring+SpringMvc +Mybatis 的作用有哪些:

      Spring作为一个轻量级框架,开发使用已经有很多年了,很好用的底层框架。里面的IOC,AOP详细的我只能说会使用,不能给大家更详细的说明。

      Mybatis呢,主要是JDBC使用,连接数据库。它比起hibernate来说我觉得配置文件少了很多,不需要去配置什么数据库方言啊,乱七八糟的东西。

      SpringMvc感觉更多的是像一个Spring精简升级版,它里面的应该就是配置文件中的启动注解还有视图的配置吧。

     为什么要用这个框架而不是传统的Spring+Struts+Hibernate呢? 

      传统的SSH之前也学习过,首先hibernate虽然很好用可是配置太过于复杂,不如Mybatis简单明了(Mybatis的动态sql语句我觉得很实用);Struts呢太复杂!各种拦截器,处理缓存。真的配置都得配置好半天才可以。而SSM则简单了很多很多,适合小项目,自己联系时使用;

    2.项目的配置:

      首先我使用的工具:

      编码软件:Eclipse  服务器:Tomcat  数据库:Mysql

      2.1在Eclipse中建立web工程

           

      2.2添加相应的架包

        由于过多就不截图,自出省略......

      2.3 建立数据库表

       

     3.项目的开发

      3.1 首先我们需要将src中的po类至Service层写好 顺序为entity—dao—mapper—Service

      

      3.2 这里面需要注意的事项:

        3.2.1 CustomerMapper中的方法名需要和CustomerMapper.xml中的id一致;

        

          3.2.2  在写CustomerMapper.xml时 我们需要配置namespace 的路径是CustomerMapper.java的路径,在resultMap中需要说明JdbcType和javaType类型

      

       3.2.3 编写service即实现 

          service类代码如下: 

    1 package com.vivebest.service;
    2 
    3 import java.util.List;
    4 
    5 import com.vivebest.entity.Customer;
    6 
    7 public interface CustomerService {
    8 public List<Customer> getAllCustomer();
    9 }

             serviceImpl类代码如下

     1 package com.vivebest.service.impl;
     2 
     3 import java.util.List;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.stereotype.Service;
     6 import com.vivebest.dao.CustomerMapper;
     7 import com.vivebest.entity.Customer;
     8 import com.vivebest.service.CustomerService;
     9 
    10 @Service("customerService")
    11 public class CustomerServiceImpl implements CustomerService{
    12 
    13 @Autowired
    14 private CustomerMapper customerMapper;
    15 
    16 @Override
    17 public List<Customer> getAllCustomer() {
    18 // TODO Auto-generated method stub
    19 return customerMapper.getAllCustomer();
    20 }
    21 
    22 }

    需要用到@service 和@Autowired .其中@service代表标示为服务层,@Autowired是Spring中自动装配使用,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;

     

    4.配置文件的配置

      

      4.1  jdbc.properties :主要是用来配置数据库信息

     1 #DB
     2 dataSource.driverClassName=com.mysql.jdbc.Driver
     3 dataSource.url=jdbc:mysql://localhost:3306/?
     4 dataSource.username = 5 dataSource.password = 6 
     7 dataSource.initialSize  =  2
     8 dataSource.maxActive = 30
     9 dataSource.maxIdle = 2
    10 dataSource.minIdle = 2
    11 dataSource.maxOpenPreparedStatements = 150
    12 dataSource.validationQuery = SELECT 1 FROM DUAL
    13 dataSource.testWhileIdle = true
    14 dataSource.testOnBorrow = false
    15 dataSource.testOnReturn = false
    16 # u914du7f6eu95f4u9694u591au4e45u624du8fdbu884cu4e00u6b21u68c0u6d4buff0cu68c0u6d4bu9700u8981u5173u95edu7684u7a7au95f2u8fdeu63a5uff0cu5355u4f4du662fu6bebu79d2 
    17 dataSource.timeBetweenEvictionRunsMillis = 60000
    18 # u914du7f6eu4e00u4e2au8fdeu63a5u5728u6c60u4e2du6700u5c0fu751fu5b58u7684u65f6u95f4uff0cu5355u4f4du662fu6bebu79d2
    19 dataSource.minEvictableIdleTimeMillis = 300000
    20 # u6253u5f00PSCacheuff0cu5e76u4e14u6307u5b9au6bcfu4e2au8fdeu63a5u4e0aPSCacheu7684u5927u5c0f
    21 dataSource.poolPreparedStatements = true
    22 dataSource.maxPoolPreparedStatementPerConnectionSize = 20
    23 # u914du7f6eu76d1u63a7u7edfu8ba1u62e6u622au7684filter
    24 dataSource.filters = stat

      4.2 applicationContext.xml :主要是用来配置Spring和Mybatis

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     5     xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     
     7     xsi:schemaLocation="
     8      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     9      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    10      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
    11      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    12      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    13      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    14     <!-- 加载配置文件-->
    15     <context:property-placeholder location="classpath:jdbc.properties"/>
    16     <!-- 扫描控制包 -->
    17     <context:component-scan base-package="com.vivebest.service" />
    18     
    19     <!--   ***************以下是dataSource 和 Mybatis配置******************  -->
    20     <!-- dataSource -->
    21     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    22         <property name="driverClassName" value="${dataSource.driverClassName}"/>
    23         <property name="url" value="${dataSource.url}"/>
    24         <property name="username" value="${dataSource.username}" />
    25         <property name="password" value="${dataSource.password}" />
    26         <property name="initialSize" value="${dataSource.initialSize}"/>
    27         <property name="maxActive" value="${dataSource.maxActive}"/>
    28         <property name="maxWait" value="30000"/>
    29         <property name="maxIdle" value="${dataSource.maxIdle}"/>
    30         <property name="minIdle" value="${dataSource.minIdle}"/>
    31         <property name="validationQuery" value="${dataSource.validationQuery}"/>
    32         <property name="testWhileIdle" value="${dataSource.testWhileIdle}"/>
    33         <property name="testOnBorrow" value="${dataSource.testOnBorrow}"/>
    34         <property name="testOnReturn" value="${dataSource.testOnReturn}"/>
    35         <property name="timeBetweenEvictionRunsMillis" value="${dataSource.timeBetweenEvictionRunsMillis}" />
    36         <property name="minEvictableIdleTimeMillis" value="${dataSource.minEvictableIdleTimeMillis}" />
    37         <property name="poolPreparedStatements" value="${dataSource.poolPreparedStatements}" />
    38         <property name="maxPoolPreparedStatementPerConnectionSize" value="${dataSource.maxPoolPreparedStatementPerConnectionSize}" />
    39         <property name="filters" value="${dataSource.filters}" />
    40     </bean>
    41     
    42     <!-- define the SqlSessionFactory -->
    43     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    44         <property name="dataSource" ref="dataSource" />
    45         <property name="configLocation" value="classpath:mybatis-config.xml" />
    46         <!-- 要映射类的包路径 -->
    47         <!--  <property name="typeAliasesPackage" value="com.vivebest.erp.entity" /> -->
    48         <!-- 若无上条就需要有该配置 -->
    49         <property name="mapperLocations" value="classpath:com/vivebest/mapper/CustomerMapper.xml"></property>  <!-- 当配置文件在其他目录时 -->
    50     </bean>
    51     
    52     <!-- scan for mappers and let them be autowired -->
    53     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    54     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    55        <property name="basePackage" value="com.vivebest.dao"/>
    56     </bean>     
    57 
    58 </beans>

    需要注意的是:value=“” 填写的是你*Mapper.xml文件的位置(我的是dao和mapper不在一个包下)

    这样application就配置成功了。

     4.3 web.xml配置(主要为配置Spring)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3   <display-name>bankERP</display-name>
     4  <!-- Spring 服务层的配置文件 -->
     5     <context-param>
     6         <param-name>contextConfigLocation</param-name>
     7         <param-value>classpath:applicationContext.xml</param-value>
     8     </context-param>
     9 
    10     <!-- Spring 容器启动监听器 -->
    11     <listener>
    12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    13     </listener>
    14                   
    17     <!-- 配置spring核心servlet -->  
    18     <servlet>  
    19         <servlet-name>spring</servlet-name>  
    20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    21         <init-param>
    22               <param-name>contextConfigLocation</param-name>
    23               <param-value>classpath:spring-mvc.xml</param-value>
    24         </init-param>     
    25         <load-on-startup>1</load-on-startup>  
    26     </servlet>  
    27     
    28     <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->  
    29     <servlet-mapping>  
    30         <servlet-name>spring</servlet-name>  
    31         <url-pattern>*.do</url-pattern>  
    32     </servlet-mapping>  
    33     
    34     
    35     <!-- encode filter 支持中文转码  -->
    36     <filter>
    37         <filter-name>characterEncodingFilter</filter-name>
    38         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    39         <init-param>
    40             <param-name>encoding</param-name>
    41             <param-value>UTF-8</param-value>
    42         </init-param>
    43         <init-param>
    44             <param-name>forceEncoding</param-name>
    45             <param-value>true</param-value>
    46         </init-param>
    47     </filter>
    48     <filter-mapping>
    49         <filter-name>characterEncodingFilter</filter-name>
    50         <url-pattern>/*</url-pattern>
    51     </filter-mapping>
    52     57 </web-app>
  • 相关阅读:
    nginx last break等
    Jmeter
    nginx location规则
    解决性能瓶颈的一些思路
    spring 一些总结
    idea快捷键
    可读的jvm gc日志时间
    redis 搭建集群
    windows下使用Python来修改文件时间戳
    Python获得文件时间戳
  • 原文地址:https://www.cnblogs.com/warnerY/p/6069518.html
Copyright © 2011-2022 走看看