SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格


SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格


SSM整合之CRUD-Rest風格

1.功能點

1)、分頁

2)、數據校驗:jquery前端校驗+JSR303後端校驗

3)、ajax 校驗和請求

4)、Rest風格的URI;使用HTTP協議請求方式的動詞,來表示對資

源的操作(GET(查詢),POST(新增),PUT(修改),DELETE(刪除))。

2.技術點

1) 基礎框架-ssm(SpringMVC4.3.7+Spring4.3.7+MyBatis3.4.2)

2) 數據庫-MySQL

3) 前端框架-bootstrap快速搭建簡潔美觀的界面

4) 項目的依賴管理-Maven

5) 分頁-pagehelper

6) 逆向工程-MyBatis Generator

3.開發工具說明

JDK:1.8

Tomcat:8.0

MySQL:5.7

Eclipse:Mars2

Spring: spring4.3.7

Mybatis:mybatis3.4.2

Maven:maven3.3.9

二、SSM環境搭建

1.創建Maven 工程

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

打包方式為:war ,報錯是因為缺少web.xml

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

添加web.xml

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格


每次創建的時候maven 報錯或者JDK版本問題,這裡引入阿里雲的鏡像

下載jar包較快

在apache-maven-3.3.9\conf中的settings配置

<code>

<

mirror

>

<

id

>

alimaven

id

>

<

name

>

aliyun maven

name

>

<

url

>

http://maven.aliyun.com/nexus/content/groups/public/

url

>

<

mirrorOf

>

central

mirrorOf

>

mirror

>

/<code>

鎖定JDK1.7版本配置

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

在裡面配置

<code>

<

profile

>

<

id

>

jdk17

id

>

<

activation

>

<

activeByDefault

>

true

activeByDefault

>

<

jdk

>

1.7

jdk

>

activation

>

<

properties

>

<

maven.compiler.source

>

1.7

maven.compiler.source

>

<

maven.compiler.target

>

1.7

maven.compiler.target

>

<

maven.compiler.compilerVersion

>

1.7

maven.compiler.compilerVersion

>

properties

>

profile

>

/<code>

2. 導入SSM 所需jar包

Spring、Springmvc、Mybatis、數據庫連接池、驅動包、其他( jstl,servlet-api,Junit )

1)在pom.xml 中引入

訪問
https://mvnrepository.com/tags/maven 可以再這裡導入所需的jar包座標

<code>

<

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/xsd/maven-4.0.0.xsd"

>

<

modelVersion

>

4.0.0

modelVersion

>

<

groupId

>

com.stackit

groupId

>

<

artifactId

>

ssm-crud

artifactId

>

<

version

>

0.0.1-SNAPSHOT

version

>

<

packaging

>

war

packaging

>

<

name

>

ssm-crud

name

>

<

description

>

ssm-crud

description

>

<

dependencies

>

<

dependency

>

<

groupId

>

org.springframework

groupId

>

<

artifactId

>

spring-webmvc

artifactId

>

<

version

>

4.3.7.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework

groupId

>

<

artifactId

>

spring-jdbc

artifactId

>

<

version

>

4.3.7.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework

groupId

>

<

artifactId

>

spring-aspects

artifactId

>

<

version

>

4.3.7.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

org.mybatis

groupId

>

<

artifactId

>

mybatis

artifactId

>

<

version

>

3.4.2

version

>

dependency

>

<

dependency

>

<

groupId

>

org.mybatis

groupId

>

<

artifactId

>

mybatis-spring

artifactId

>

<

version

>

1.3.1

version

>

dependency

>

<

dependency

>

<

groupId

>

c3p0

groupId

>

<

artifactId

>

c3p0

artifactId

>

<

version

>

0.9.1

version

>

dependency

>

<

dependency

>

<

groupId

>

mysql

groupId

>

<

artifactId

>

mysql-connector-java

artifactId

>

<

version

>

5.1.41

version

>

dependency

>

<

dependency

>

<

groupId

>

javax.servlet.jsp.jstl

groupId

>

<

artifactId

>

jstl

artifactId

>

<

version

>

1.2

version

>

dependency

>

<

dependency

>

<

groupId

>

javax.servlet

groupId

>

<

artifactId

>

servlet-api

artifactId

>

<

version

>

2.5

version

>

<

scope

>

provided

scope

>

dependency

>

<

dependency

>

<

groupId

>

junit

groupId

>

<

artifactId

>

junit

artifactId

>

<

version

>

4.12

version

>

<

scope

>

test

scope

>

dependency

>

dependencies

>

project

>

/<code>

引入完成

3. 引入前端框架bootstrap

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

<code>

<

script

type

=

"text/javascript"

src

=

"static/js/jquery-1.11.3.min.js"

>

script

>

<

link

rel

=

"stylesheet"

type

=

"text/css"

href

=

"static/bootstrap-3.3.7-dist/css/bootstrap.css"

/>

<

script

type

=

"text/javascript"

src

=

"static/bootstrap-3.3.7-dist/js/bootstrap.min.js"

>

script

>

/<code>

4.編寫ssm整合的配置文件

<code> 

<

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_2_5.xsd"

id

=

"WebApp_ID"

version

=

"2.5"

>

<

filter

>

<

filter-name

>

CharacterEncodingFilter

filter-name

>

<

filter-class

>

org.springframework.web.filter.CharacterEncodingFilter

filter-class

>

<

init-param

>

<

param-name

>

encoding

param-name

>

<

param-value

>

UTF-8

param-value

>

init-param

>

<

init-param

>

<

param-name

>

forceRequestEncoding

param-name

>

<

param-value

>

true

param-value

>

init-param

>

<

init-param

>

<

param-name

>

forceResponseEncoding

param-name

>

<

param-value

>

true

param-value

>

init-param

>

filter

>

<

filter-mapping

>

<

filter-name

>

CharacterEncodingFilter

filter-name

>

<

url-pattern

>

/*

url-pattern

>

filter-mapping

>

<

filter

>

<

filter-name

>

HiddenHttpMethodFilter

filter-name

>

<

filter-class

>

org.springframework.web.filter.HiddenHttpMethodFilter

filter-class

>

filter

>

<

filter-mapping

>

<

filter-name

>

HiddenHttpMethodFilter

filter-name

>

<

url-pattern

>

/*

url-pattern

>

filter-mapping

>

<

listener

>

<

listener-class

>

org.springframework.web.context.ContextLoaderListener

listener-class

>

listener

>

<

context-param

>

<

param-name

>

contextConfigLocation

param-name

>

<

param-value

>

classpath:applicationContext.xml

param-value

>

context-param

>

<

servlet

>

<

servlet-name

>

DispatcherServlet

servlet-name

>

<

servlet-class

>

org.springframework.web.servlet.DispatcherServlet

servlet-class

>

<

init-param

>

<

param-name

>

contextConfigLocation

param-name

>

<

param-value

>

classpath:springmvc.xml

param-value

>

init-param

>

servlet

>

<

servlet-mapping

>

<

servlet-name

>

DispatcherServlet

servlet-name

>

<

url-pattern

>

/

url-pattern

>

servlet-mapping

>

web-app

>

/<code>

5.配置springmvc.xml

<code> 

<

beans

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance"

xmlns

=

"http://www.springframework.org/schema/beans"

xmlns:context

=

"http://www.springframework.org/schema/context"

xmlns:aop

=

"http://www.springframework.org/schema/aop"

xmlns:tx

=

"http://www.springframework.org/schema/tx"

xmlns:mvc

=

"http://www.springframework.org/schema/mvc"

xsi:schemaLocation

=

"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"

>

<

context:component-scan

base-package

=

"com.stackit"

use-default-filters

=

"false"

>

<

context:include-filter

type

=

"annotation"

expression

=

"org.springframework.stereotype.Controller"

/>

context:component-scan

>

<

bean

class

=

"org.springframework.web.servlet.view.InternalResourceViewResolver"

>

<

property

name

=

"prefix"

value

=

"/WEB-INF/views/"

>

property

>

<

property

name

=

"suffix"

value

=

".jsp"

>

property

>

bean

>

<

mvc:default-servlet-handler

/>

<

mvc:annotation-driven

/>

/<code>

創建jdbc.properties

<code>

jdbc.driveClass

=com.mysql.jdbc.Driver

jdbc.jdbcUrl

=jdbc:mysql:///ssm_crud_agg

jdbc.user

=root

jdbc.password

=

123456

/<code>

6.配置applicationContext.xml

<code> 

<

beans

xmlns:xsi

=

"http://www.w3.org/2001/XMLSchema-instance"

xmlns

=

"http://www.springframework.org/schema/beans"

xmlns:context

=

"http://www.springframework.org/schema/context"

xmlns:aop

=

"http://www.springframework.org/schema/aop"

xmlns:tx

=

"http://www.springframework.org/schema/tx"

xmlns:mvc

=

"http://www.springframework.org/schema/mvc"

xsi:schemaLocation

=

"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"

>

<

context:component-scan

base-package

=

"com.stackit"

>

<

context:exclude-filter

type

=

"annotation"

expression

=

"org.springframework.stereotype.Controller"

/>

context:component-scan

>

<

context:property-placeholder

location

=

"classpath:jdbc.properties"

/>

<

bean

name

=

"dataSource"

class

=

"com.mchange.v2.c3p0.ComboPooledDataSource"

>

<

property

name

=

"jdbcUrl"

value

=

"${jdbc.jdbcUrl}"

>

property

>

<

property

name

=

"user"

value

=

"${jdbc.user}"

>

property

>

<

property

name

=

"password"

value

=

"${jdbc.password}"

>

property

>

bean

>

<

bean

name

=

"SqlSessionFactory"

class

=

"org.mybatis.spring.SqlSessionFactoryBean"

>

<

property

name

=

"dataSource"

ref

=

"dataSource"

/>

<

property

name

=

"configLocation"

value

=

"classpath:mybatis-config.xml"

/>

<

property

name

=

"mapperLocations"

value

=

"classpath:com/stackit/ssm/mapper/*.xml"

/>

bean

>

<

bean

class

=

"org.mybatis.spring.mapper.MapperScannerConfigurer"

>

<

property

name

=

"basePackage"

value

=

"com.stackit.ssm.mapper"

/>

bean

>

<

bean

name

=

"transactionManager"

class

=

"org.springframework.jdbc.datasource.DataSourceTransactionManager"

>

<

property

name

=

"dataSource"

ref

=

"dataSource"

/>

bean

>

<

aop:config

>

<

aop:pointcut

expression

=

"execution(* com.stackit.ssm..*.*(..))"

id

=

"pointcut"

/>

<

aop:advisor

advice-ref

=

"txAdvice"

pointcut-ref

=

"pointcut"

/>

aop:config

>

<

tx:advice

id

=

"txAdvice"

transaction-manager

=

"transactionManager"

>

<

tx:attributes

>

<

tx:method

name

=

"*"

/>

<

tx:method

name

=

"get*"

read-only

=

"true"

/>

<

tx:method

name

=

"find*"

read-only

=

"true"

/>

tx:attributes

>

tx:advice

>

beans

>

/<code>

7. 配置mybatis-config.xml

<code> 
 

PUBLIC

"-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd"

>

<

configuration

>

<

settings

>

<

setting

name

=

"mapUnderscoreToCamelCase"

value

=

"true"

/>

settings

>

configuration

>

/<code>

8.創建表

<code> 

CREATE

TABLE

`tbl_dept`

(

`dept_id`

int

(

11

)

NOT

NULL

AUTO_INCREMENT,

`dept_name`

varchar

(

255

)

DEFAULT

NULL

, PRIMARY

KEY

(

`dept_id`

) )

ENGINE

=

InnoDB

DEFAULT

CHARSET

=utf8

CREATE

TABLE

`tbl_emp`

(

`emp_id`

int

(

11

)

NOT

NULL

AUTO_INCREMENT,

`emp_name`

varchar

(

255

)

NOT

NULL

,

`gender`

char

(

1

)

DEFAULT

NULL

,

`email`

varchar

(

255

)

DEFAULT

NULL

,

`d_id`

int

(

11

)

DEFAULT

NULL

, PRIMARY

KEY

(

`emp_id`

),

KEY

`fk_emp_dept`

(

`d_id`

),

CONSTRAINT

`fk_emp_dept`

FOREIGN

KEY

(

`d_id`

)

REFERENCES

`tbl_dept`

(

`dept_id`

) )

ENGINE

=

InnoDB

DEFAULT

CHARSET

=utf8/<code>

9.使用mybatis的逆向工程生成對應的實體類和Mapper.xml接口

在pom.xml 中添加mybatis-generator 逆向工程的jar


<code> 

<

dependency

>

<

groupId

>

org.mybatis.generator

groupId

>

<

artifactId

>

mybatis-generator-core

artifactId

>

<

version

>

1.3.5

version

>

dependency

>

/<code>

在當前工程創建mbg.xml

<code> 
 

PUBLIC

"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"

>

<

generatorConfiguration

>

<

context

id

=

"DB2Tables"

targetRuntime

=

"MyBatis3"

>

<

commentGenerator

>

<

property

name

=

"suppressAllComments"

value

=

"true"

/>

commentGenerator

>

<

jdbcConnection

driverClass

=

"com.mysql.jdbc.Driver"

connectionURL

=

"jdbc:mysql:///ssm_crud_agg"

userId

=

"root"

password

=

"123456"

>

jdbcConnection

>

<

javaTypeResolver

>

<

property

name

=

"forceBigDecimals"

value

=

"false"

/>

javaTypeResolver

>

<

javaModelGenerator

targetPackage

=

"com.stackit.ssm.beans"

targetProject

=

".\src\main\java"

>

<

property

name

=

"enableSubPackages"

value

=

"true"

/>

<

property

name

=

"trimStrings"

value

=

"true"

/>

javaModelGenerator

>

<

sqlMapGenerator

targetPackage

=

"com.stackit.ssm.mapper"

targetProject

=

".\src\main\resources"

>

<

property

name

=

"enableSubPackages"

value

=

"true"

/>

sqlMapGenerator

>

<

javaClientGenerator

type

=

"XMLMAPPER"

targetPackage

=

"com.stackit.ssm.mapper"

targetProject

=

".\src\main\java"

>

<

property

name

=

"enableSubPackages"

value

=

"true"

/>

javaClientGenerator

>

<

table

tableName

=

"tbl_emp"

domainObjectName

=

"Employee"

>

table

>

<

table

tableName

=

"tbl_dept"

domainObjectName

=

"Department"

>

table

>

context

>

generatorConfiguration

>

/<code>

執行逆向工程

<code>

import

java.io.File;

import

java.io.IOException;

import

java.sql.SQLException;

import

java.util.ArrayList;

import

java.util.List;

import

org.mybatis.generator.api.MyBatisGenerator;

import

org.mybatis.generator.config.Configuration;

import

org.mybatis.generator.config.xml.ConfigurationParser;

import

org.mybatis.generator.internal.DefaultShellCallback;

public

class

MybatisMBGTest

{

public

static

void

main

(String[] args)

throws

Exception

{ List warnings =

new

ArrayList();

boolean

overwrite =

true

; File configFile =

new

File(

"mbg.xml"

); ConfigurationParser cp =

new

ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback =

new

DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator =

new

MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(

null

); } } 在Employee 中添加Department 生產getter和setter 在EmployeeMapper 中添加

List

selectByExampleWithDept

(EmployeeExample example)

;

Employee

selectByPrimaryKeyWithDept

(Integer empId)

;/<code>

在Employee 中添加Department 生產getter和setter

在EmployeeMapper 中添加

<code> 

List

selectByExampleWithDept(EmployeeExample example); Employee selectByPrimaryKeyWithDept(Integer empId);/<code>

三 測試Mapper-CRUD:

mybatis使用 批量操作的

<code> 

<

bean

name

=

"sqlSession"

class

=

"org.mybatis.spring.SqlSessionTemplate"

>

<

constructor-arg

name

=

"sqlSessionFactory"

ref

=

"SqlSessionFactory"

>

constructor-arg

>

<

constructor-arg

name

=

"executorType"

value

=

"BATCH"

>

constructor-arg

>

bean

>

/<code>
<code>

package

com.stackit.ssm.test;

import

java.util.UUID;

import

org.apache.ibatis.session.SqlSession;

import

org.junit.Test;

import

org.junit.runner.RunWith;

import

org.springframework.beans.factory.annotation.Autowired;

import

org.springframework.test.context.ContextConfiguration;

import

org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import

com.stackit.ssm.beans.Department;

import

com.stackit.ssm.beans.Employee;

import

com.stackit.ssm.mapper.DepartmentMapper;

import

com.stackit.ssm.mapper.EmployeeMapper; (SpringJUnit4ClassRunner

.

class

) @

ContextConfiguration

(

value

=

"classpath:applicationContext.xml"

)

public

class

MapperTest

{ DepartmentMapper departmentMapper; EmployeeMapper employeeMapper; SqlSession sqlSession;

public

void

testCRUD

()

{ EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper

.

class

)

;

for

(

int

i =

0

; i

1000

; i++) { String uuid = UUID.randomUUID().toString().substring(

0

,

5

)+i; mapper.insertSelective(

new

Employee(

null

, uuid,

"M"

, uuid+

"@atguigu.com"

,

1

)); } } }/<code>

Windows系統下MySQL5.7中文亂碼的解決

【描述】win10系統,Mysql5.7安裝版,出現中文亂碼問題,數據庫和數據庫表均已設置UTF-8編碼,但是依舊出現亂碼。

【經歷】上網查了,說要修改my.ini,但是在C:\Program Files\MySQL\MySQL Router 8.0\etc目錄下以及C:\Program Files\MySQL\MySQL Server 5.7目錄下均未找到該文件。

有一種方法:顯示隱藏的ProgramData文件夾,將C:\ProgramData\MySQL\MySQL Server 5.7下的my.ini複製到安裝目錄C:\Program Files\MySQL\MySQL Server 5.7下,然後運行my.ini裡面的提到的命令,試了幾下,也沒用,可能是自己不太會弄吧。

【方法】於是就直接修改了C:\ProgramData\MySQL\MySQL Server 5.7下的my.ini文件。

【步驟】

1、顯示隱藏文件夾

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

2、找到C:\ProgramData\MySQL\MySQL Server 5.7下的my.ini文件,右鍵用Notepad++打開

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

3、找到[mysql],在# default-character-set=的下一行添加default-character-set=utf8(圖中第67行)

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

4、找到[mysqld],在# character-set-server=的下一行添加character-set-server=utf8(圖中第102行)

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

5、保存文件,重啟數據庫,測試

【效果】

修改之前

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

修改之後

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

【變化】character_set_database和character_set_server的值由原來的latin1變為utf8

【說明】我的MySQL安裝好後,使用過一段時間,之前沒涉及中文字符(當然,之前每次新建數據庫時都會設置UTF-8編碼),後面發現有中文亂碼問題,經過一番折騰之後,無奈的我直接修改了隱藏的my.ini文件,修改好後,重新試了一下,不會亂碼了。

不過還有幾個編碼是gbk,而不是utf8(如圖所示),也試過在[client]中配置default-character-set=utf8和在[mysqld]中配置collation-server = utf8_general_ci init_connect='SET NAMES utf8',但是都沒用,那三個依舊還是gbk,不過目前看來,不會有影響。(我每次新建數據庫時都會設置UTF-8編碼,並且在連接數據庫時,會在url中加上?characterEncoding=utf-8)

在命令行中輸入 set names utf8;可以修改那3個"老頑童"

SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

具體此方法是否會有"後遺症"尚不清楚,不過目前來看確實解決了我的中文亂碼問題。

【建議】修改隱藏的my.ini之前,最好先備份一個,萬一改崩了,還有"後悔藥"。另外,折騰完之後,建議去掉勾選的"查看隱藏的項目",既然它們想藏起來就讓它們藏吧,哪天確實需要再用它們的時候,再召喚,萬一誤刪了這些"害羞"的重要文件就麻煩了。

四 頁面CRUD

查詢

• 1、訪問index.jsp頁面

• 2、index.jsp頁面發送出查詢員工列表請求

• 3、EmployeeController來接受請求,查出員工數據

• 4、來到list.jsp頁面進行展示

• 5、pageHelper分頁插件完成分頁查詢功能

• URI:/emp

在pom.xm中引入mybatis helper 分頁插件的jar 包

<code>

<

dependency

>

<

groupId

>

com.github.pagehelper

groupId

>

<

artifactId

>

pagehelper

artifactId

>

<

version

>

5.0.0

version

>

dependency

>

/<code>

在mybatis-config.xml 中配置

<code>

<

plugins

>

<

plugin

interceptor

=

"com.github.pagehelper.PageInterceptor"

>

plugin

>

plugins

>

/<code>
SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格

報錯:
java.lang.NoClassDefFoundError:
javax/servlet/SessionCookieConfig

需要servlet-api 需要3.0的支持

在pom.xml 中配置

<code> 

<

dependency

>

<

groupId

>

javax.servlet

groupId

>

<

artifactId

>

javax.servlet-api

artifactId

>

<

version

>

3.0.1

version

>

<

scope

>

provided

scope

>

dependency

>

/<code>

Spring提供虛擬測試

<code>package com.stackit.ssm.test;

import

java.util.List;

import

org.junit.Before;

import

org.junit.Test;

import

org.junit.runner.RunWith;

import

org.springframework.beans.factory.annotation.Autowired;

import

org.springframework.mock.web.MockHttpServletRequest;

import

org.springframework.test.context.ContextConfiguration;

import

org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import

org.springframework.test.context.web.WebAppConfiguration;

import

org.springframework.test.web.servlet.MockMvc;

import

org.springframework.test.web.servlet.MvcResult;

import

org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import

org.springframework.test.web.servlet.setup.MockMvcBuilders;

import

org.springframework.web.context.WebApplicationContext;

import

com.github.pagehelper.PageInfo;

import

com.stackit.ssm.beans.Employee; @

RunWith

(

SpringJUnit4ClassRunner

.

class

) @

ContextConfiguration

(

locations

=

{

"classpath:applicationContext.xml"

,

"classpath:springmvc.xml"

}) @

WebAppConfiguration

public

class

MVCTest

{ @

Autowired

WebApplicationContext

context;

MockMvc

mockMvc; @

Before

public

void initMockMvc() { mockMvc =

MockMvcBuilders

.webAppContextSetup(context).build(); } @

Test

public

void testPage()

throws

Exception

{

MvcResult

result = mockMvc.perform(

MockMvcRequestBuilders

.

get

(

"/emps"

).param(

"pn"

,

"5"

)).andReturn();

MockHttpServletRequest

request = result.getRequest();

PageInfo

info = (

PageInfo

) request.getAttribute(

"pageInfo"

);

System

.out.

println

(

"當前頁碼:"

+info.getPageNum());

System

.out.

println

(

"總頁碼:"

+info.getPages());

System

.out.

println

(

"總記錄數:"

+info.getTotal());

System

.out.

println

(

"在頁面需要連續顯示的頁碼"

); int[] nums = info.getNavigatepageNums();

for

(int i : nums) {

System

.out.

println

(i); }

System

.out.

println

(

"員工數據"

);

List

<

Employee

> list = info.getList();

for

(

Employee

employee : list) {

System

.out.

println

(employee.getEmpId() +

" ==== "

+ employee.getEmpName() +

" ==== "

+ employee.getDepartment().getDeptName()); } } }/<code>

C3P0 :java.sql.SQLException: No suitable driver

連接的四個參數有誤

<code>

<

dependency

>

<

groupId

>

taglibs

groupId

>

<

artifactId

>

standard

artifactId

>

<

version

>

1.1.2

version

>

dependency

>

/<code>

Maven項目出現錯誤:

java.lang.ClassNotFoundException: org.apache.taglibs.standard.tlv.JstlCoreTLV

錯誤的引入:

<code>

<

dependency

>

<

groupId

>

javax.servlet.jsp.jstl

groupId

>

<

artifactId

>

jstl

artifactId

>

<

version

>

1.2

version

>

dependency

>

/<code>

正確的引人:

<code>

<

dependency

>

<

groupId

>

javax.servlet

groupId

>

<

artifactId

>

jstl

artifactId

>

<

version

>

1.2

version

>

<

scope

>

runtime

scope

>

dependency

>

/<code>

---------------------

或者:

<code>

<

dependency

>

<

groupId

>

jstl

groupId

>

<

artifactId

>

jstl

artifactId

>

<

version

>

1.2

version

>

dependency

>

/<code>
SSM整合(Spring-SpringMVC-Mybatis) 之-Rest風格


分享到:


相關文章: