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就是掃描后的名字。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM