zoukankan      html  css  js  c++  java
  • Spring+SpringMVC+mybatis整合以及注解的使用(三)

    1.包结构:

     2.spring配置:基本的DAO配置以及扫描Mapper(扫描出来的Mapper为首字母小写)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     8     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    10 
    11 
    12     <context:property-placeholder location="classpath:db.properties"/>
    13     
    14     <!-- 数据库连接池 -->
    15     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    16         destroy-method="close">
    17         <property name="driverClassName" value="${jdbc.driver}" />
    18         <property name="url" value="${jdbc.url}" />
    19         <property name="username" value="${jdbc.username}" />
    20         <property name="password" value="${jdbc.password}" />
    21         <property name="maxActive" value="10" />
    22         <property name="maxIdle" value="5" />
    23     </bean>
    24     
    25     <!-- Mybatis的工厂 -->
    26     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    27         <property name="dataSource" ref="dataSource"/>
    28         <!-- 核心配置文件的位置 -->
    29         <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    30     </bean>
    31 
    32     <!-- Mapper动态代理开发   扫描 -->
    33     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    34         <!-- 基本包 -->
    35         <property name="basePackage" value="cn.qlq.springmvc.mapper"/>
    36     </bean>
    37     
    38     <!-- 注解事务 -->
    39     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    40         <property name="dataSource" ref="dataSource"/>
    41     </bean>
    42     
    43     <!-- 开启注解 -->
    44     <tx:annotation-driven transaction-manager="transactionManager"/>
    45     
    46     
    47 </beans>

     3.mybatis配置(主要就一个定义别名)

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6     <!-- 设置别名 -->
     7     <typeAliases>
     8         <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
     9         <package name="cn.qlq.springmvc.pojo" />
    10     </typeAliases>
    11 
    12 </configuration>

    4.springMVC配置(主要就是配置扫描Service和Controller)

     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:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     9 
    10         
    11         
    12         <!-- 扫描@Controler  @Service   -->
    13         <context:component-scan base-package="cn.qlq"/>
    14         
    15         <!-- 处理器映射器 -->
    16 <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
    17         <!-- 处理器适配器 -->
    18 <!--         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
    19         <!-- 注解驱动 -->
    20         <mvc:annotation-driven/>
    21         
    22      
    23         
    24         <!-- 视图解释器 -->
    25         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    26             <property name="prefix" value="/WEB-INF/jsp/"/>
    27             <property name="suffix" value=".jsp"/>
    28         </bean>
    29         
    30    </beans>

    5.Service使用注解注入Mapper(主要就是自己添加service注释与内部注入mapper)

       5.1 接口(接口中不做任何声明)

    1 package cn.qlq.springmvc.service;
    2 import java.util.List;
    3 
    4 import cn.qlq.springmvc.pojo.Items;
    5 public interface ItemService {
    6     public List<Items> findAllItems();
    7 }

        5.2.Service实现类(自己添加service注释与内部注入mapper)

     1 package cn.qlq.springmvc.service;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import cn.qlq.springmvc.mapper.ItemsMapper;
     9 import cn.qlq.springmvc.pojo.Items;
    10 
    11 @Service
    12 public class ItemsServiceImpl implements ItemService {
    13 
    14     @Autowired
    15     private ItemsMapper itemsMapper;
    16     @Override
    17     public List<Items> findAllItems() {
    18         
    19         List<Items> selectByExampleWithBLOBs = itemsMapper.selectByExampleWithBLOBs(null);
    20         return selectByExampleWithBLOBs;
    21     }
    22 
    23 }

    6. 控制层(声明自己是Controller层与自动注入Service)

    package cn.qlq.springmvc.controller;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.HttpRequestHandler;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import cn.qlq.springmvc.pojo.Items;
    import cn.qlq.springmvc.service.ItemService;
    
    /**
     * 商品管理
     * 
     * @author lx
     *
     */
    @Controller
    public class ItemController {
        @Autowired
        private ItemService itemService;
        //入门程序 第一   包类 + 类包 + 方法名
        @RequestMapping(value = "/item/itemlist.action")
        public ModelAndView itemList(){
            //从Mysql中查询
            List<Items> list = itemService.findAllItems();
            
            ModelAndView mav = new ModelAndView();
            //数据
            mav.addObject("itemList", list);
            mav.setViewName("itemList");
            return mav;
        }
    
        
    }

    总结:Service层注入时只用在实现类上声明service。注入时候注入接口名字。可以用自动装配@Autowired,也可以用

    @Resource(name="baseDao")     
    private BaseDao baseDao; 
    这种格式注入对象,name就是扫描后的名字。
  • 相关阅读:
    科技公司网站
    jquery 设置元素内容html(),text(),val()
    jquery 相关class属性的操作
    jquery attr()和prop()方法的使用
    检测移动设备横竖屏
    设定程序在某个特定时刻执行
    js设计模式-建造者模式
    css自定义字体完美解决方案example
    css透明属性
    css3多列example
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/7214527.html
Copyright © 2011-2022 走看看