源码文件:
链接:https://pan.baidu.com/s/12Rhx5nW2qkLiA37p2VBzDA 密码:0mf4
一个超简单的ssm框架整合,适合对mybatis,sping和spingMVC有初步了解的伙伴观看。
总体思路:
a模型层:
创建pojo类(类似于vo类);
创建dao接口(数据库的操作方法);
创建dao的映射文件(数据库操作方法的实现)在mapper包下*.xml
b业务层:
无。业务层Service就是调用DAO类的,然后视图层的控制器Controller调用Service。
由于这是自己的第一次整合和程序较为简单,所以就暂时不写service了。
c视图层:
视图+控制器
控制器:Controller
视图,前端人员的工作:
d配置:sping配置放在资源文件夹下,spingMVC的配置放在Web-INF文件夹下。(在ssm组合框架中,mybatis的配置可以完全由Sping配置替代)
制作过程以及遇到的问题:
1导入所有的jar包。假如某个jar包缺失或缺失某些配置时,一般会这样提示
nested exception isjava.lang.NoClassDefFoundError:(所缺的包)
例如我缺失了一个数据池的包,运行程序时就会有如下提示
org.springframework.beans.factory.BeanCreationException: Error creating bean with name'dataSource' defined in class path resource [spring-cfg.xml]: Instantiation ofbean failed; nested exception isorg.springframework.beans.BeanInstantiationException: Failed to instantiate[org.apache.commons.dbcp.BasicDataSource]: No default constructor found;nested exception isjava.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
解决办法:导入相关的包
2
写pojo类,简单无问题
3
写dao接口,简单无问题
例如
packagecom.dao;
importjava.util.List;
importcom.pojo.News;
publicinterfaceNewsDAO {
publicList findAll();
publicintinsertNews(Newsnews);
publicintdeleteNews(intNewsID);
publicintupdateNews(Newsnews);
}
4
写dao接口的映射文件,文件里必须包含这段说明
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">、
例如:
"1.0"encoding="UTF-8"?>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.dao.NewsDAO">
"insertNews"parameterType="news">
insert into News(NewsTitle, NewsContent,NewsTime, AdminName) values(#, #, #,#)
"deleteNews"parameterType="int">
delete from News Where NewsID=#
"updateNews"parameterType="news">
update News set NewsTitle=#,NewsContent=#where NewsID=#
"findAll"resultType="news">
select * from News
5
写sping的配置(重要,请重点关注,框架的配置不对,将导致你头晕脑乱)
'1.0'encoding='UTF-8'?>
"user"class="com.pojo.User">
"news"class="com.pojo.News">
"url"value="jdbc:mysql://localhost:3306/webdb"/>
"username"value="root"/>
"password"value="mysql"/>
"maxActive"value="255"/>
"maxIdle"value="5"/>
"maxWait"value="10000"/>
"dataSource"ref="dataSource"/>
"mapperLocations"value="classpath:com/mapper/*.xml"/>
"typeAliasesPackage"value="com.pojo"/>
6
写控制器
首先讲一下我刚开始整合时,一种比较愚蠢的方式,几乎等价于于根本没使用sping的核心思想:“将对象的创建交给容器”(不使用注解获取bean,将使我每次使用数据库时都需要加载一次容器。代码异常臃肿),
例如获取所有新闻的操作:
packagecom.controller;
importjava.util.List;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.web.bind.annotation.RequestMapping;
importcom.dao.NewsDAO;
importcom.pojo.News;
//注释此类为控制器
@Controller
//注解在什么路径下执行此类,可有可无
@RequestMapping("/front")
publicclassNewsHandler {
//注解在什么路径下执行此方发,一个完整的路径就是“/front/allNews”@RequestMapping("/allNews")
publicString news(HttpServletRequestrequest) {
//通过sping的配置加载容器
ClassPathXmlApplicationContextctx=newClassPathXmlApplicationContext("spring-cfg.xml");
//声明dao接口,对数据库进行具体操作
NewsDAOnewsDAO;
//声明SqlSession类,用来获取dao接口的操作对象
SqlSessionsqlSession=null;
try{
//通过容器获取SqlSession
sqlSession=((SqlSessionFactory)ctx.getBean("sqlSessionFactory")).openSession();
//通过SqlSession获取dao接口操作对象
newsDAO=sqlSession.getMapper(NewsDAO.class);
//dao接口执行方法,操作数据库,获取数据
Listall=newsDAO.findAll();
//保存为session类型的属性
request.getSession().setAttribute("allNews",all);
}finally{
if(sqlSession!=null) {
sqlSession.close();
}
}
//关闭容器
ctx.close();
//进行跳转
return"/front/newsFrontList";
}
}
7
关于spingMVC的配置
"1.0"encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
"com.controller"/>
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
"prefix"value="/">
"suffix"value=".jsp">
8
web.xml的配置
"1.0"encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
"com.controller"/>
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
"prefix"value="/">
"suffix"value=".jsp">
9
写jsp界面
10
到此,一个简单的ssm组合框架就结束了,配置文件和ssm运行的流程及思想,大家要多花点时间去理解。关于前端的代码,就别看了,等将来有时间,我会使用最先进的模式(json+html+restful+css+jQuery)重新整理一下前端,再进行详细地解释。
11(对愚蠢的方式的改进)
对原有的sping的配置文件,增加以下内容
"org.mybatis.spring.mapper.MapperScannerConfigurer">
"basePackage"value="com.dao"/>
12
增加注解操作:@Repository @Resource
对dao接口,增加@Repository注解
packagecom.dao;
importjava.util.List;
importcom.pojo.News;
@Repository
publicinterfaceNewsDAO {
publicList findAll();
publicintinsertNews(Newsnews);
publicintdeleteNews(intNewsID);
publicintupdateNews(Newsnews);
}
13
对Controller类进行改进,增加@Resource,
packagecom.controller;
importjava.util.List;
importjavax.annotation.Resource;
importorg.springframework.web.bind.annotation.RequestMapping;
importcom.dao.NewsDAO;
importcom.pojo.News;
//注释此类为控制器
@Controller
//注解在什么路径下执行此类,可有可无
@RequestMapping("/front")
publicclassNewsHandler {
//注解在什么路径下执行此方发,一个完整的路径就是“/front/allNews”
@Resource
privateNewsDAOnewsDAO;
@RequestMapping("/allNews")
publicString news(HttpServletRequestrequest) {
Listall=newsDAO.findAll();
request.getSession().setAttribute("allNews",all);
return"/front/newsFrontList";
}
}
结束,谢谢观看。
网址(有效期10年): garys.top ,网站尚未整理
领取专属 10元无门槛券
私享最新 技术干货