您的位置:首页 > 编程语言 > Java开发

springmvc 和mybatis 整合(一)

2015-07-29 23:08 423 查看

springmvc和mybatis整合

1.1  需求

使用springmvc和mybatis完成商品列表查询。

1.2  整合思路

springmvc+mybaits的系统架构:(面试问题)






从后往前整合

Spring是一个javabean的ioc容器;

第一步:整合dao层

         mybatis和spring整合,通过spring管理mapper接口。

         使用mapper的扫描器自动扫描mapper接口在spring中进行注册。

 

第二步:整合service层

                  通过spring管理 service接口。

         使用配置方式将service接口配置在spring配置文件中。

         实现事务控制一般放在service中。

 

第三步:整合springmvc

         由于springmvc是spring的模块,不需要整合。

 

 

准备环境

 

数据库环境:mysql5.1

java环境:

jdk1.7.0_72

eclipse indigo

 

springmvc版本:spring3.2

 

所需要的jar包:

数据库驱动包:mysql5.1

mybatis的jar包

mybatis和spring整合包

log4j包

dbcp数据库连接池包

spring3.2所有jar包

jstl包

 

log4j.properties 开发阶段建议是有debug模式 log4j.rootLogger=DEBUG,

db.properties  数据库的链接参数,配置目的是 更好的和配置文件隔离

1.2.1         db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=XXXX
jdbc.password=xxXXXX

 

1.2.2         log4j.properties

# Global loggingconfiguration,建议开发环境中要用debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
 

 

 

参考:springmvc 和mybatis 的整合jar包我已上传资源

 

工程结构:


整合dao

mybatis和spring进行整合。

sqlMapConfig.xml

classpath 下创建sqlMapConfig.xml

mybatis自己的配置文件。

 

<?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>
    <!-- 全局的setting配置根据需要再加
     例如缓存和延迟加载 -->
 
    <!-- 配置别名 -->
    <typeAliases>
       <!-- 批量扫描别名 -->
       <package
name="cn.hpu.ssm.po"/>
    </typeAliases>
 
    <!-- 配置mapper由于使用spring和mybatis的整合包进行mapper扫描,这里就不配置了。遵循一些规则,mapper.xml

       mapper.java 文件同名且在一个文件目录 -->
</configuration>

 

applicationContext-dao.xml

classpath 下创建applicationContext-dao.xml

 

spring 和mybatis整合的配置文件

配置:

数据源

SqlSessionFactory

mapper扫描器  (注意:如果将<property
name="sqlSessionFactoryBeanName"
value="sqlSessionFactory">换为<property name="sqlSessionFactory"
ref="sqlSessionFactory"></property>则数据库的配置文件不起作用
)

 

<beans
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">       
    <!-- 扫描controller注解,多个包中间使用半角逗号分隔
-->
 

<!-- 加载配置文件db.properties文件中的key命名要有一定的特殊规则
-->
    <context:property-placeholder
location="classpath:db.properties"
/>
 
    <!-- 数据源,使用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="10"
/>
       <property
name="maxIdle"value="5"
/>
    </bean>
 
    <!--sqlsesstionFactory -->
 
    <bean
id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 加载mybatis的配置文件 -->
       <property
name="configLocation"value="classpath:mybatis/sqlMapConfig.xml"></property>
       <!-- 数据源 -->
       <property
name="dataSource"ref="dataSource"></property>
    </bean>
 
    <!-- mapper 的批量扫描,从mapper包中扫除mapper接口,自动创建代理对象并在spring的容器中注册规范:需要将mapper接口类名和mapper.xml映射文件名车个保持一致,且在同一个目录中
       上边的规范是前提 -->
    <bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <!-- 指定扫描的报名 sqlSessionFactoryBeanName
自动扫描来的mapper的bean的id为mapper(类名)首字母小写
           扫描多个包包之间用半角,号隔开 -->
       <property
name="basePackage"value="cn.hpu.ssm.mapper"></property>
       <property
name="sqlSessionFactoryBeanName"value="sqlSessionFactory">
       </property>
    </bean>

1.2.1    逆向工程生成po类及mapper(单表增删改查)

 



 

将生成的文件拷贝至工程中。

1.2.2    手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper

 

ItemsMapperCustom.xml(动态代理 
手动进行创建)

sql语句:每次应当先在mysql中写出sql语句

 

         SELECT* FROM items  WHERE items.name LIKE '%笔记本%'

 

<?xml
version="1.0"encoding="UTF-8"
?>
<!DOCTYPE
mapperPUBLIC
"-//mybatis.org//DTDMapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="cn.hpu.ssm.mapper.ItemsMapperCustom">
  <!-- 定义
sql 片段  
商品的查询条件-->
  <sql
id="query_items_where">
  <!-- 使用动态sql,通过if判断,满足条件进行sql拼接
       商品的查询条件需要通过ItemsQueryVo包装对象中itemsCustom属性传递
       ${}字符串的拼接
   -->
  <if
test="itemsCustom!=null">
  <if
test="itemsCustom.name!=nulland itemsCustom.name !=''">
       items.nameLIKE '%${itemsCustom.name}%'
  </if>
  </if>
  </sql>
  <!-- 商品列表查询
 parameterType传入包装类对象(包装了查询条件)
 resultType 建议使用扩展对象
   -->
  <select
id="findItemsList"parameterType="cn.hpu.ssm.po.ItemsQueryVo" 
resultType="cn.hpu.ssm.po.ItemsCustom">
  SELECT * FROM items
  <where>
  <include
refid="query_items_where"></include>
  </where>
  </select>
</mapper>

1.2.2.1      ItemsMapperCustom.java

 

//商品类表查询
public List<ItemsCustom> findItemsList (ItemsQueryVoitemsQueryVo)throws Exception;
Dao 层做完了

1.3  整合service

让spring管理service接口。

定义service接口

//商品类表查询
public
interface
ItemsService {
    public List<ItemsCustom> findItemsList (ItemsQueryVoitemsQueryVo)throws Exception;
 

service接口impl

public
class
ItemsServiceImpl  implements ItemsService{
    //因为使用了spring的扫描器,已经自己加载spring容器了 
mapper需要遵守规则)
    @Autowired
    private  ItemsMapperCustomitemsMapperCustom;

 
    public List<ItemsCustom> findItemsList(ItemsQueryVoitemsQueryVo)
           throws Exception {
    //通过ItemsMapperCustom查询数据库
       //itemsQueryVo从service直接传递到dao
       return
itemsMapperCustom.findItemsList(itemsQueryVo);
    }

 

在spring容器配置service(applicationContext-service.xml)

在classpath 下创建applicationContext-service.xml,文件中配置service。

 

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
 
<!-- 定义商品管理的service -->
<bean
id="itemsService"class="cn.hpu.ssm.service.impl.ItemsServiceImpl">
</bean>
 
</beans>

事务控制(applicationContext-transaction.xml)

在classpath 下创建applicationContext-transaction.xml中使用spring声明式事务控制方法。

<beans
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">       
    <!-- 扫描controller注解,多个包中间使用半角逗号分隔
-->
 

<!-- 声明式的事物配置 -->
       <!-- 定义一个事务管理器
           对mybatis操作数据库的控制,spring使用jdbc的事物控制
           dataSource:在 applicationContext-dao.xml中配置了
       -->
       <bean
id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!-- 配置数据源 -->
       <property
name="dataSource"ref="dataSource"></property>
       </bean>
       <!-- 通知 
通知给事务管理器-->
       <tx:advice
id="txAdvice"transaction-manager="transactionManager">
           <tx:attributes>
           <!-- 传播行为  
固定方法的前缀变相的规范程序员的编码 -->
           <tx:method
name="save*"propagation="REQUIRED"/>
           <tx:method
name="delete*"propagation="REQUIRED"/>
           <tx:method
name="insert*"propagation="REQUIRED"/>
           <tx:method
name="update*"propagation="REQUIRED"/>
           <tx:method
name="find*"propagation="SUPPORTS"
read-only="true"/>
           <tx:method
name="get*"propagation="SUPPORTS"
read-only="true"/>
           <tx:method
name="select*"propagation="SUPPORTS"
read-only="true"/>
           </tx:attributes>
       </tx:advice>
      
       <!-- aop 配置
       pointcut:切点
       -->
       <aop:config>
       <aop:advisor
advice-ref="txAdvice"pointcut="execution(* cn.hpu.ssm.service.impl.*.*(..))"/>
       </aop:config>

整合springmvc

springmvc.xml

创建springmvc.xml文件,配置处理器映射器、适配器、视图解析器。

<beans
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">       
<!-- 可以扫描controller、service、...
   这里让扫描controller,指定controller的包
    -->
   <context:component-scan
base-package="cn.itcast.ssm.controller"></context:component-scan>
  
     
   <!--注解映射器 -->
   <!-- <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
   <!--注解适配器 -->
   <!-- <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
  
   <!-- 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
   mvc:annotation-driven默认加载很多的参数绑定方法,
   比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
   实际开发时使用mvc:annotation-driven
    -->
   <mvc:annotation-driven></mvc:annotation-driven>
  
 
   <!-- 视图解析器
   解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
    -->
   <bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <!-- 配置jsp路径的前缀 -->
      <property
name="prefix"
value="/WEB-INF/jsp/"/>
      <!-- 配置jsp路径的后缀 -->
      <property
name="suffix"
value=".jsp"/>
   </bean>

 

配置前端控制器

在web.Xml中配置

<!-- 加载spring
的容器
  /WEB-INF/classes/spring/applicationContext-*.xml自动加载该格式的文件
  -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!--   前端控制器 -->
 <servlet>
     <servlet-name>springmvc</servlet-name>  

     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- contextConfigLocation
配置springMVC加载的配置文件配置文件重要配置映射器,适配器 -->
     <!-- 如果不配置 contextConfigLocation
,默认加载/WEB-INF/SERVLER-SERVLET.XML(SPRINGMVC.XMl)-->
     <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:spring/springmvc.xml</param-value>
     </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--
        第一种 *.action
访问以.action结尾由DispathcherServlet进行解析
        第二种 /
所有访问的地址都有DispathcherSerclet进行解析,对于静态的文件需要配置不让DispathcherSerclet进行解析
        是有此种方式可以实现RESful的风格url
        第三种 /*
这样配置不对,使用这种配置,最终需要转发给一个jsp页面是仍然会有DispathcherSerclet解析jsp地址,不能根据jsp找到handle
        -->
    <url-pattern>*.action</url-pattern>
   
</servlet-mapping>

1.3.1    编写Controller(就是Handler)

/*
 * 商品的Controller
 */
@Controller
public
class
ItemsController {
    //注解开发
    @Autowired
    private ItemsService
itemsService;//注入service
       //商品查询
       //一般建议方法名和url写成一样,方便维护
       //@RequestMapping实现queryItems方法和url进行映射一个方法对应一个url
       @RequestMapping("/queryItems")
       public ModelAndView queryItems()throws Exception{
            //调用service查找数据库,查询商品列表,使用静态数据模
          
                  List<ItemsCustom> itemsList=itemsService.findItemsList(null);                   

                  //返回ModelAndView

                  ModelAndView modelAndView=new ModelAndView();
                  //相当于requesr的setAttribut,在jsp页面中通过itemsList来取得数据
                  modelAndView.addObject("itemsList",itemsList);
                  //指定视图
                  //下边的路径在试图解析器中配置前缀和后缀
                  modelAndView.setViewName("items/itemsList");
 
                  //modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
                  return modelAndView;
          
       }

1.3.2    编写jsp



1.4  加载spring容器

 

将mapper  使用了扫描器、service  使用了声明式的配置方式、controller    使用了组建扫描   加载到spring容器中。

 

建议使用通配符加载上边的配置文件。

 

在web.xml中,添加spring容器监听器,加载spring容器。

 

<!-- 加载spring
的容器
  /WEB-INF/classes/spring/applicationContext-*.xml自动加载该格式的文件
  -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

摘自传智博客燕青老师的视频
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis springmvc