Spring+SpringMVC+MyBatis+Maven整合

最近看見網(wǎng)上整理的Spring+SpringMVC+MyBatis+Maven太low,正好今天有時(shí)間,于是整理了下守呜,那么開始吧!

1、使用的工具

  • STS下載地址 http://www.springsource.org/downloads/sts-ggts
  • Tomcat
  • JDK
  • Mybatis generator
  • 若使用Eclipse開發(fā)(需要配置Maven,Maven配置好后原杂,在命令行下輸入mvn -v,顯示下圖代表配置成功)


2、Generator的結(jié)構(gòu)

  • 首先得有數(shù)據(jù)庫(kù)表
  • src文件夾為生成模型的位置(dao,mapping,model)
  • generator.xml配置


  • mybatis-generator-core-1.3.2.jar核心包
  • 生成語句復(fù)制您机,右鍵在此處打開命令行(注意路徑不能用中文)
  • 生成結(jié)果如下圖所示


3穿肄、打開Eclipse新建Maven項(xiàng)目

  • 如圖紅色箭頭所示,選擇默認(rèn)的空間际看,next


  • 最后Finish

  • 最后生成項(xiàng)目結(jié)構(gòu)如下圖所示
  • 項(xiàng)目建立后被碗,首先我會(huì)把編碼改成utf-8,省的以后有麻煩。
  • 你會(huì)發(fā)現(xiàn)項(xiàng)目目錄沒有顯示出來仿村,那么我們選擇屬性如圖
  • 這時(shí)你會(huì)發(fā)現(xiàn)目錄還是沒有變化,那么接下來我們?cè)谡{(diào)試兴喂,默認(rèn)是J2SE-1.5
  • 那么我們?cè)诰庉嬛付ㄒ幌滤遥鐖D
  • 這時(shí)在回過頭來看項(xiàng)目目錄,以完整衣迷。

4畏鼓、項(xiàng)目建立完畢,引入Spring和MyBatis需要的jar包

  • 打開pom.xml
  • 如果引入的時(shí)候壶谒,記不住groupId版本號(hào)云矫,其實(shí)在Eclipse里面有一個(gè)如圖所示,當(dāng)然這種方法不好(請(qǐng)看下一種)

  • 那么我們選擇手寫汗菜,打開瀏覽器輸入http://search.maven.org/
  • 記住spring核心包spring-core
  • mybatis
  • mybatis-spring
  • mysql-connector-java
  • junit
  • 數(shù)據(jù)源druid
  • org.aspectj
  • 記住后面還有jar包需要加入

pom.xml代碼


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dc_zaichi.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>0.2.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.11</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.26</version>
        </dependency>
    </dependencies>
    <build>
    </build>
</project>

5让禀、配置文件

  • spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

    <!-- 引入屬性文件 -->
    <context:property-placeholder location="classpath:config.properties" />

    <!-- 自動(dòng)掃描(自動(dòng)注入) -->
    <context:component-scan base-package="dc_zaichi.service" />


</beans>
  • spring-mybatis
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

    <!-- JNDI方式配置數(shù)據(jù)源 -->
    <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

    <!-- 配置數(shù)據(jù)源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />

        <!-- 初始化連接大小 -->
        <property name="initialSize" value="0" />
        <!-- 連接池最大使用連接數(shù)量 -->
        <property name="maxActive" value="20" />
        <!-- 連接池最大空閑 -->
        <property name="maxIdle" value="20" />
        <!-- 連接池最小空閑 -->
        <property name="minIdle" value="0" />
        <!-- 獲取連接最大等待時(shí)間 -->
        <property name="maxWait" value="60000" />

        <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接陨界,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一個(gè)連接在池中最小生存的時(shí)間巡揍,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分鐘 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 -->
        <property name="logAbandoned" value="true" />

        <!-- 監(jiān)控?cái)?shù)據(jù)庫(kù) -->
        <!-- <property name="filters" value="stat" /> -->
        <property name="filters" value="mergeStat" />
    </bean>

    <!-- myBatis文件,這步很重要 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動(dòng)掃描entity目錄, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:dc_zaichi/mapping/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dc_zaichi.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- 配置事務(wù)管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 注解方式配置事物 -->
     <tx:annotation-driven transaction-manager="transactionManager" />


    <!-- 攔截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* dc_zaichi.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>


    <!-- 配置druid監(jiān)控spring jdbc -->
    <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>dc_zaichi.service.*</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    </aop:config>

</beans>
  • log4j.properties文件
log4j.rootLogger=DEBUG,Console,File

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]%m%n

log4j.appender.File=org.apache.log4j.RollingFileAppender 
log4j.appender.File.File=mybatis.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
  • config.properties配置文件

#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=sypro
#jdbc_password=sypro

#hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/dc_zaichi?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=helei

#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
#jdbc_username=sa
#jdbc_password=123456

#hibernate.dialect=org.hibernate.dialect.DerbyDialect
#driverClassName=org.apache.derby.jdbc.EmbeddedDriver
#validationQuery=SELECT 1
#jdbc_url=jdbc:derby:sy;create=true
#jdbc_username=sypro
#jdbc_password=sypro

#jndiName=java:comp/env/dataSourceName

hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true

sessionInfoName=sessionInfo

uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached

6菌瘪、這樣我們已經(jīng)將Spring和mybatis完全整合了腮敌,接下來上代碼

  • 寫接口,查詢一個(gè)資產(chǎn)俏扩,肯定要返回一個(gè)Asset_Check
package dc_zaichi.service;

import dc_zaichi.model.Asset_Check;


public interface AssetCheckService {
    public   Asset_Check  getAssetCheckById(String id); 
    

}

  • 接下來寫實(shí)現(xiàn)類
package dc_zaichi.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dc_zaichi.dao.Asset_CheckMapper;
import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;
//AssetCheckServiceImpl把他放到spring上下文環(huán)境中
@Service("AssetCheckService")
public class AssetCheckServiceImpl implements AssetCheckService{
    
    //AssetCheckServiceImpl處理業(yè)務(wù)邏輯后糜工,業(yè)務(wù)邏輯調(diào)用數(shù)據(jù)庫(kù),就是dao,也就是mappper,要使用這個(gè)mapper录淡,一定要通過spring注入捌木,首先要有g(shù)et和set方法,那么如何將Asset_CheckMapper 注入到services里面呢赁咙?在set方法上加@Autowired注解

//當(dāng)然了钮莲,這個(gè)service還是一個(gè)普通的類免钻,因?yàn)闆]有放到spring上下文環(huán)境當(dāng)中,自動(dòng)掃描包后需要一個(gè)serviced的注解告訴掃描的那個(gè)地方崔拥,這個(gè)類是一個(gè)bean极舔。這樣spring上下文在啟動(dòng)的時(shí)候,就能加入到spring環(huán)境當(dāng)中了 
    
    private Asset_CheckMapper  asset_CheckMapper;

    public Asset_CheckMapper getAsset_CheckMapper() {
        return asset_CheckMapper;
    }
    //
    @Autowired
    public void setAsset_CheckMapper(Asset_CheckMapper asset_CheckMapper) {
        this.asset_CheckMapper = asset_CheckMapper;
    }
   public Asset_Check getAssetCheckById(String id) {
        // TODO Auto-generated method stub
        return asset_CheckMapper.selectByPrimaryKey(id);
    }

}


7链瓦、測(cè)試拆魏,因?yàn)槭?strong>Maven項(xiàng)目, 在src/test/java目錄下建立包名和測(cè)試類,通過junit測(cè)試

package mybatis.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;

public class TestMyBatis {
    @Test
    public void test1(){
        ApplicationContext ac= 
                new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
        AssetCheckService  bean = (AssetCheckService) ac.getBean("AssetCheckService");
    //這個(gè)bean就是實(shí)現(xiàn)類中的那個(gè)service注解
        Asset_Check assetCheckById = bean.getAssetCheckById("1");
        System.out.println(assetCheckById.getAcName());
        
    }
 
}

  • 打印如下

8慈俯、設(shè)想下渤刃,測(cè)試類中有一個(gè)測(cè)試方法我們可以這樣寫, 如果有100個(gè)測(cè)試方法贴膘,那么這個(gè)速度難以忍受卖子,因?yàn)閟pring在初始化的時(shí)候,即耗資源又耗時(shí)間刑峡。那么我們可以這樣寫

package mybatis.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;

public class TestMyBatis {
    private ApplicationContext ac;
    AssetCheckService  bean;
    
    @Before
    public void before(){
         
        ac=new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
        bean = (AssetCheckService) ac.getBean("AssetCheckService");
    }
    
    
    @Test
    public void test1(){
        Asset_Check assetCheckById = bean.getAssetCheckById("1");
        System.out.println(assetCheckById.getAcName());
        
    }
 
}
  

@Before的作用:在當(dāng)前這個(gè)類所有測(cè)試方法之前去執(zhí)行的這么一個(gè)方法洋闽。將所有初始化的東西放到這個(gè)放到這個(gè)方法之內(nèi)。那么只會(huì)執(zhí)行一次

到此突梦,說明spring和mybatis整合成功

接下里再看一種測(cè)試方法:添加spring-test的jar包依賴

package mybatis.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml",
        "classpath:spring-mybatis.xml" })
public class TestMyBatis {
    private AssetCheckService assetCheckService;

    public AssetCheckService getAssetCheckService() {
        return assetCheckService;
    }

    @Autowired
    public void setAssetCheckService(AssetCheckService assetCheckService) {
        this.assetCheckService = assetCheckService;
    }

    // private ApplicationContext ac;
    // AssetCheckService bean;

    // @Before
    // public void before(){
    //
    // ac=new ClassPathXmlApplicationContext(new
    // String[]{"spring.xml","spring-mybatis.xml"});
    // bean = (AssetCheckService) ac.getBean("AssetCheckService");
    // }
    //

    @Test
    public void test1() {
        Asset_Check assetCheckById = assetCheckService.getAssetCheckById("1");
        System.out.println(assetCheckById.getAcName());

    }

}

ok

9诫舅、 導(dǎo)入springMVC的jar包,搜索spring-webmvc

  • springMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 自動(dòng)掃描controller包下的所有類宫患,使其認(rèn)為spring mvc的控制器 -->
    <context:component-scan base-package="dc_zaichi.controller" />

    <!-- 避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 啟動(dòng)Spring MVC的注解功能刊懈,完成請(qǐng)求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /><!-- json轉(zhuǎn)換器 -->
            </list>
        </property>
    </bean>

    <!-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上傳文件大小限制為31M娃闲,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>
</beans>
  • 接下來虚汛,我們建立一個(gè)包,和類如下圖
  • 建立Controller類如下圖所示
package dc_zaichi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import dc_zaichi.model.Asset_Check;
import dc_zaichi.service.AssetCheckService;

@Controller
// 加入到springmvc的上下文當(dāng)中
@RequestMapping("/assetCheckController")
// 這個(gè)東西就是訪問地址的一個(gè)配置
public class AssetCheckController {

    // 比如說執(zhí)行 http://127.0.0.1:8080/mybatis/assetCheckController.do 訪問此控制器

    private AssetCheckService assetCheckService;

    public AssetCheckService getAssetCheckService() {
        return assetCheckService;
    }

    @Autowired
    public void setAssetCheckService(AssetCheckService assetCheckService) {
        this.assetCheckService = assetCheckService;
    }

    @RequestMapping("/showAssetCheck")
    public String showAssetCheck(String id, HttpServletRequest request) {

        // http://localhost:8080/mybatis/assetCheckController.do?id=1
        // 后臺(tái)接的時(shí)候畜吊,接到這個(gè)id,并且去數(shù)據(jù)庫(kù)查找這個(gè)資產(chǎn)泽疆,也就是AssetCheckController訪問數(shù)據(jù)庫(kù),不能直接
        // 訪問玲献,得通過業(yè)務(wù)邏輯service
        // 訪問此控制器里的一個(gè)方法
        // http://127.0.0.1:8080/mybatis/assetCheckController.showAssetCheck.do

        Asset_Check assetCheckById = assetCheckService.getAssetCheckById(id);
        System.out.println(assetCheckById.getAcName());
        // 把a(bǔ)ssetCheckById存到Request中
        request.setAttribute("Asset_Check", assetCheckById);
        return "showAssetCheck";// 返回一個(gè)jsp,不用howAssetCheck.jsp,已經(jīng)在spring-mvc中配置好

    }

}


如何啟動(dòng)SpringMVC,修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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">
    <display-name>mybatis</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
    </context-param>
    <filter>
        <description>字符集過濾器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集編碼</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <description>spring監(jiān)聽器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止spring內(nèi)存溢出監(jiān)聽器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- spring mvc servlet -->
    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>spring mvc 配置文件</description>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置session超時(shí)時(shí)間殉疼,單位分鐘 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
</web-app>

10、頁(yè)面

11捌年、ok

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瓢娜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子礼预,更是在濱河造成了極大的恐慌眠砾,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件托酸,死亡現(xiàn)場(chǎng)離奇詭異褒颈,居然都是意外死亡柒巫,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門谷丸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堡掏,“玉大人,你說我怎么就攤上這事刨疼∪洌” “怎么了?”我有些...
    開封第一講書人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵揩慕,是天一觀的道長(zhǎng)亭畜。 經(jīng)常有香客問我,道長(zhǎng)迎卤,這世上最難降的妖魔是什么拴鸵? 我笑而不...
    開封第一講書人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮蜗搔,結(jié)果婚禮上宝踪,老公的妹妹穿的比我還像新娘。我一直安慰自己碍扔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開白布秕重。 她就那樣靜靜地躺著不同,像睡著了一般。 火紅的嫁衣襯著肌膚如雪溶耘。 梳的紋絲不亂的頭發(fā)上二拐,一...
    開封第一講書人閱讀 49,031評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音凳兵,去河邊找鬼百新。 笑死,一個(gè)胖子當(dāng)著我的面吹牛庐扫,可吹牛的內(nèi)容都是我干的饭望。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼形庭,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼铅辞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起萨醒,我...
    開封第一講書人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤斟珊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后富纸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囤踩,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡旨椒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了堵漱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片综慎。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖怔锌,靈堂內(nèi)的尸體忽然破棺而出寥粹,到底是詐尸還是另有隱情,我是刑警寧澤埃元,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布涝涤,位于F島的核電站,受9級(jí)特大地震影響岛杀,放射性物質(zhì)發(fā)生泄漏阔拳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一类嗤、第九天 我趴在偏房一處隱蔽的房頂上張望糊肠。 院中可真熱鬧,春花似錦遗锣、人聲如沸货裹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)弧圆。三九已至,卻和暖如春笔咽,著一層夾襖步出監(jiān)牢的瞬間搔预,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工叶组, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拯田,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓甩十,卻偏偏與公主長(zhǎng)得像船庇,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子侣监,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容