配色: 字号:
CGB-SSM-01
2017-12-31 | 阅:  转:  |  分享 
  


SSM框架整合

Spring+SpringMVC+MyBatis





1. 技术环境说明 1-1

1.1. 服务端技术说明 1-1

1.2. 客户端技术说明 1-2

1.3. 技术架构说明 1-2

2. 技术整合 2-2

2.1. 创建MavenWeb项目 2-2

2.2. 整合SpringMVC 2-2

2.3. 整合JSON对象转换 2-5

2.4. 整合DRUID连接池对象 2-5

2.5. 整合MyBatis框架 2-7

2.6. 整合Log4J输出 2-8

3. 用户角色管理实践 3-9

3.1. 用户角色管理概述 3-9

3.2. 创建用户角色表 3-10

3.3. 创建角色实体SysRole 3-10

3.4. 创建角色DAO接口 3-12

3.5. 基于DAO接口创建映射文件 3-12

3.6. 创建角色Service接口及实现类 3-12

3.1. 基于Service创建单元测试 3-13

3.2. 创建角色Controller类 3-14

4. 总结 4-15

4.1. 重点和难点分析 4-15

4.2. 常见FAQ 4-15

技术环境说明

服务端技术说明

Web服务器:tomcat

数据库服务器:mysql

服务端技术框架:Spring+SpringMVC+Mybatis

……

客户端技术说明

HTML,CSS,JavaScript

JQuery

…..

技术架构说明



项目整体采用MVC分层架构







技术整合

创建MavenWeb项目



创建MavenWeb项目(项目名称CGB-JT-SYS-V1.0

项目设置(utf-8,targetruntimes,projectfacets)



整合SpringMVC



输入控制对象是直接与用户输入相关的对象



添加SpringMVC依赖





org.springframework

spring-webmvc

4.3.9.RELEASE







配置SpringMVC核心控制器(web.xml)





dispatcherServlet

org.springframework.web.servlet.DispatcherServlet



contextConfigLocation

classpath:spring-.xml



1





dispatcherServlet

.do





添加spring配置文件(spring-configs.xml)



将配置文件直接放置在的resources根目录


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

xmlns:p="http://www.springframework.org/schema/p"

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

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

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

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

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

xmlns:util="http://www.springframework.org/schema/util"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="

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

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

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

http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

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

http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

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

http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-4.3.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

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

http://www.springframework.org/schema/context/spring-context-4.3.xsd">





Springmvc配置

























测试



在com.jt.sys.controller编写控制器,验证MVC环境





@Controller

@RequestMapping("/role/")

publicclassRoleController{

@RequestMapping("listUI")

publicStringlistUI(){

System.out.println("listUI()");

return"sys/roles";

}

}



在浏览器输入http://localhost:8080/CGB-JT-SYS-V1.0/role/listUI.do检测是否能够到达控制器并跳转到了解

com.fasterxml.jackson.core

jackson-databind

2.8.5





在需要返回JSON对象的方法上使用@ResponseBody注解,例如



@RequestMapping("doTestJackson")

@ResponseBody

publicMapdoTestJackson(){

Mapmap=newHashMap();

map.put(“id”,100);

map.put(“name”,101);

returnmap;

}



配置好以后在浏览器中直接方法对应的地址,检测浏览器输出比C3P0,DBCP等更好.

mysql

mysql-connector-java

5.1.40





添加druid依赖



com.alibaba

druid

1.0.23





添加config.properties(src/main/resources目录)



driver=com.mysql.jdbc.Driver

url=jdbc:mysql:///test

username=root

password=root



配置DRUID(还是在spring的这个spring-configs.xml中配置)



配置DRUID数据源




destroy-method="close"init-method="init"lazy-init="true">













编写连接池单元测试

junit

junit

4.12





编写测试类



@Test

publicvoidtestPool(){

ApplicationContextctx=

newClassPathXmlApplicationContext("spring-configs.xml");

DruidDataSourcedataSource=(DruidDataSource)ctx.getBean("dataSource");

System.out.println(dataSource);

Assert.assertNotEquals(dataSource,null);

}



整合MyBatis框架

添加MyBatis依赖





org.mybatis

mybatis-spring

1.3.1







org.mybatis

mybatis

3.2.8







在比较新的版本中通常还需要如下两个依赖



org.springframework

spring-tx

4.3.9.RELEASE







org.springframework

spring-jdbc

4.3.9.RELEASE







在spring-configs.xmlmybatis配置,并添加如下内容











classpath:mapper/.xml















编写单元测试



@Test

publicvoidtestSessionFactory(){

ApplicationContextctx=

newClassPathXmlApplicationContext("spring-configs.xml");

ObjectsessionFactory=ctx.getBean("sqlSessionFactory");

System.out.println(sessionFactory);

Assert.assertNotEquals(sessionFactory,null);

}



整合Log4J输出

目的是实现mybatiSQL日志的输出便于调试跟踪(扩展实现)



添加log4J依赖:



log4j

log4j

1.2.17



添加配置文件(log4j.properties)



log4j.rootLogger=INFO,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d[%-5p]%c-%m%n



log4j.logger.com.mybatis3=DEBUG

log4j.logger.com.jt=DEBUG



添加配置文件(mybatis-config.xml)






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















修改配置文件spring-mybatis.xml添加mybatis-config.xml的配置…













classpath:mapper/.xml













用户角色管理实践

用户角色管理概述

京淘项目中有一个权限管理子系统,此系统中包含用户管理,角色管理,菜单管理,组织机构管理等,

具体这些模块业务关系,后续分析,本节课首先从技术角度实现SSM技术的基本整合,实现上以角色管理模块为切入点。

创建用户角色表

创建数据库

createdatabasejt_sysdefaultcharactersetutf8;

usejt_sys



CREATETABLE`sys_roles`(

`id`bigint(20)NOTNULLAUTO_INCREMENT,

`name`varchar(100)DEFAULTNULLCOMMENT''角色名称'',

`note`varchar(500)DEFAULTNULLCOMMENT''备注'',

`createdTime`datetimeDEFAULTNULLCOMMENT''创建时间'',

`modifiedTime`datetimeDEFAULTNULLCOMMENT''修改时间'',

`createdUser`varchar(20)DEFAULTNULLCOMMENT''创建用户'',

`modifiedUser`varchar(20)DEFAULTNULLCOMMENT''修改用户'',

PRIMARYKEY(`id`)

)ENGINE=InnoDBDEFAULTCHARSET=utf8COMMENT=''角色''

创建角色实体SysRole



publicclassSysRoleimplementsSerializable{

privatestaticfinallongserialVersionUID=-5225339701513043662L;

privateIntegerid;

privateStringname;

privateStringnote;

privateDatecreatedTime;

privateDatemodifiedTime;

privateStringcreatedUser;

privateStringmodifiedUser;

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetNote(){

returnnote;

}

publicvoidsetNote(Stringnote){

this.note=note;

}

publicDategetCreatedTime(){

returncreatedTime;

}

publicvoidsetCreatedTime(DatecreatedTime){

this.createdTime=createdTime;

}

publicDategetModifiedTime(){

returnmodifiedTime;

}

publicvoidsetModifiedTime(DatemodifiedTime){

this.modifiedTime=modifiedTime;

}

publicStringgetCreatedUser(){

returncreatedUser;

}

publicvoidsetCreatedUser(StringcreatedUser){

this.createdUser=createdUser;

}

publicStringgetModifiedUser(){

returnmodifiedUser;

}

publicvoidsetModifiedUser(StringmodifiedUser){

this.modifiedUser=modifiedUser;

}

@Override

publicStringtoString(){

return"SysRole[id="+id+",name="+name+",note="+note+",createdTime="+createdTime

+",modifiedTime="+modifiedTime+",createdUser="+createdUser+",modifiedUser="+modifiedUser

+"]";

}



}



这样的对象我们通常称之为1)intinsertObject(SysRoleentity);

2)SysRoledoFindSysRoleById(Integerid);

这样的对象编写时应注意:

1)实现Serializable接口,并提供版本ID

2)提供无参构造函数

3)提供属性以及对应的set/get方法,一般属性名要与

表中的字段名保持一致.

4)有选择性的重写toString()方法



创建角色DAO接口

数据访问层对象,负责数据的操作.

packagecom.jt.sys.dao;

importjava.util.List;

importcom.jt.sys.pojo.SysRole;



publicinterfaceSysRoleDao{

/查询所有角色信息/

ListfindPageObjects();

}

映射文件

文件存储在的

映射文件的名字为




resultType="com.jt.sys.pojo.SysRole">

select

fromsys_roles









创建角色Service接口及实现类



publicinterfaceSysRoleService{

ListfindPageObjects();

}





@Service

publicclassSysRoleServiceImplimplementsSysRoleService{

@Autowired

privateSysRoleDaosysRoleDao;

@Override

publicListfindPageObjects(){

//log

Listlist=sysRoleDao.findPageObjects();

//log

returnlist;

}

}



基于Service创建单元测试





publicclassTestRoleService{



privateClassPathXmlApplicationContextctx;

@Before

publicvoidinit(){

ctx=newClassPathXmlApplicationContext("spring-configs.xml");

}

@Test

publicvoidtestFindPageObjects(){

SysRoleServiceroleService=

ctx.getBean("sysRoleServiceImpl",SysRoleService.class);

Listlist=

roleService.findPageObjects();

System.out.println(list);

}

@After

publicvoiddestory(){

ctx.close();

}

}





创建角色Controller类



定义控制层类对象

packagecom.jt.sys.controller;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.ResponseBody;

importcom.jt.sys.pojo.SysRole;

importcom.jt.sys.service.SysRoleService;

@RequestMapping("/role/")

@Controller

publicclassSysRoleController{

@Autowired

privateSysRoleServicesysRoleService;

@RequestMapping("doFindPageObjects")

@ResponseBody

publicListdoFindPageObjects(){

Listlist=

sysRoleService.findPageObjects();

returnlist;//jackson,fastjson

}

}



启动tomcat访问url进行测试.





















1-9









献花(0)
+1
(本文系金银宝100首藏)