科猫网项目总结(基于SSM框架)
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext( 可以用来获取Spring容器中已初始化的bean)的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
classpath:spring/applicationContext.xml也可以简化为/WEB-INF/applicationContext.xml
<servlet-mapping>
<servlet-name>springmvc</servlet-name> 需要与上面名称相对应
<url-pattern>*.action</url-pattern> 哪些路径要走springmvc
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file> 指定起始页
</welcome-file-list>
<?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: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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 使用注释方式 -->
<mvc:annotation-driven/>
<!-- 自动扫描Controller-->
<context:component-scan base-package="controller"/>
<!-- 配置一个springMVC视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value= ""/>
<property name="suffix" value= ".jsp"/>
</bean>
<!-- 返回json的处理 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</beans>
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <context:component-scan base-package="dao"/>
<context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 初始化连接数量; -->
<property name="initialSize" value="0" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="20" /> <!-- 最大空闲连接数 -->
<property name="maxIdle" value="20" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="0" />
<!-- 最大等待时长 -->
<property name="maxWait" value="60000" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超过时间限制多长; -->
<property name="removeAbandonedTimeout" value="180"/> <!-- 数据源连接参数配置; -->
<property name="username" value="${db.username}"/>
<property name="url" value="${db.url}"/>
<property name="password" value="${db.password}"/>
<property name="driverClassName" value="${db.driver}"/> </bean> <!-- 配置SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
<property name="mapperLocations">
<list>
<value>classpath:config/*.xml</value>
</list>
</property>
</bean> <!-- 自动扫描mapper接口,注入sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* dao.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config> <!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="isValid" propagation="REQUIRED" read-only="true"/>
<tx:method name="add" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice> </beans>
1.自动实例化对象包的指定(需要改动)
<!-- 开启自动初始化标签:添加Component就可以自动将类初始化 -->
<context:component-scan base-package="dao"></context:component-scan>
为dao的类前面添加注解:
@Component
表示自动创建实例,而controller的类也是需要自动创建实例的,但是其由springMVC来完成了(添加了@Controller)
2.数据库信息文件的指定
<context:property-placeholder location="classpath:db.properties" />
表示在src目录下的db.properties文件(需要改动)
3.数据源dataSource使用dbcp
4.配置SessionFactory
<!-- 配置SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
<property name="mapperLocations">
<list>
<value>classpath:config/*.xml</value>
</list>
</property>
</bean>
<!-- 自动扫描mapper接口,注入sqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* dao.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config> <!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="isValid" propagation="REQUIRED" read-only="true"/>
<tx:method name="add" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
attributes标签中用REQUIRED,表示运行到isValid方法的时候有事务就运行事务,没事务就创建事务。
4.mybatis-cfg.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"></properties>
<!-- 对事务的管理和连接池的配置 --> <environments default="development"> <environment id="kcat"> <transactionManager type="JDBC" /> 事务管理器的类型 <dataSource type="POOLED"> 连接池的方式 <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <!-- mapping 文件路径配置 --> <mappers> <!--<mapper resource="javastudy/userMapper.xml"/> -->
<!-- <mapper class="javastudy.CourseMapper"/> -->
<mapper class="mapper.BangMapper"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.BangMapper">
<select id="get" resultType="model.Bang">
select * from bang
</select>
</mapper>
package controller; import java.io.IOException;
import java.util.ArrayList; import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody; import dao.BangDAO;
import model.Bang; @Controller
public class BangController {
BangDAO dao; public BangDAO getDao() {
return dao;
}
@Resource
public void setDao(BangDAO dao) {
this.dao = dao;
} @ResponseBody
public ArrayList<Bang> get() throws IOException{
ArrayList<Bang> list=dao.get();
return list;
} } 3.dao包
一般程序都是用模型层与数据库进行交互,而dao层则用于程序对数据库的操作,所以认为dao层属于模型层。
也有这样的看法,把dao层看做MVC框架之外的单独的一层,称之为数据持久层 package dao; import java.util.ArrayList; import javax.annotation.Resource; import org.springframework.stereotype.Component; import mapper.BangMapper;
import model.Bang; @Component
public class BangDAO {
BangMapper bangMapper; public BangMapper getBangMapper() {
return bangMapper;
}
@Resource
public void setBangMapper(BangMapper bangMapper) {
this.bangMapper = bangMapper;
}
public ArrayList<Bang> get(){
ArrayList<Bang> list=bangMapper.get();
return list;
}
} 4.mapper包
mapper包用来放置DAO接口
package mapper; import java.util.ArrayList; import model.Bang; public interface BangMapper {
public ArrayList<Bang> get();
}
5.model包
放置模型类,可以看做表
package model; public class Bang {
private int id;
private String videoName;
private int videoType;
private String videoUrl;
private String videoImage;
private String videoExplain;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getVideoName() {
return videoName;
}
public void setVideoName(String videoName) {
this.videoName = videoName;
}
public int getVideoType() {
return videoType;
}
public void setVideoType(int videoType) {
this.videoType = videoType;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getVideoImage() {
return videoImage;
}
public void setVideoImage(String videoImage) {
this.videoImage = videoImage;
}
public String getVideoExplain() {
return videoExplain;
}
public void setVideoExplain(String videoExplain) {
this.videoExplain = videoExplain;
}
@Override
public String toString() {
return "Bang [id=" + id + ", videoName=" + videoName + ", videoType=" + videoType + ", videoUrl=" + videoUrl
+ ", videoImage=" + videoImage + ", videoExplain=" + videoExplain + "]";
} }
package utility; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtils {
public static SqlSession openSession() throws IOException
{
String resource = "mybatis-cfg.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in,"kcat"); //这里要对应mybatis-cfg.xml的environment id
return sessionFactory.openSession();
}
}
科猫网项目总结(基于SSM框架)的更多相关文章
- 一款基于SSM框架技术的全栈Java web项目(已部署可直接体验)
概述 此项目基于SSM框架技术的Java Web项目,是全栈项目,涉及前端.后端.插件.上线部署等各个板块,项目所有的代码都是自己编码所得,每一步.部分都有清晰的注释,完全不用担心代码混乱,可以轻松. ...
- 基于SSM框架的JavaWeb通用权限管理系统
- - ->关注博主公众号[C you again],获取更多IT资源(IT技术文章,毕业设计.课程设计系统源码,经典游戏源码,HTML网页模板,PPT.简历模板,!!还可以投稿赚钱!!,点击查 ...
- Java基于ssm框架的restful应用开发
Java基于ssm框架的restful应用开发 好几年都没写过java的应用了,这里记录下使用java ssm框架.jwt如何进行rest应用开发,文中会涉及到全局异常拦截处理.jwt校验.token ...
- 01-电子商城项目介绍及ssm框架搭建
1.B2C电商项目功能及架构 1.1功能列表 1.2系统架构(soa架构) 2.后台管理系统工程搭建及测试 ypMall,ypMall-manager-web ypMall为父项目,管理子项目的jar ...
- 【项目笔记】完成一个基于SSM框架的增删改查的模块后总结的问题
最近为了准备新工作重新摸出了SSM框架,同时从0学习了JQuery,终于用一周做完了一个包括增删改查的模块(主要是属性太多了,其中一个类50+,复制粘贴耗时). 从中特意记下了几个遇到的问题,总结一下 ...
- 从项目中理解SSM框架
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- java官网门户源码 SSM框架 自适应-响应式 freemarker 静态模版引擎
来源:http://www.fhadmin.org/webnewsdetail3.html 前台:支持(5+1[时尚单页风格])六套模版,可以在后台切换 官网:www.fhadmin.org 系统介绍 ...
- 如何搭建maven项目和搭建ssm框架
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- 使用IDEA 创建Maven项目,外加SSM框架
使用idea 新创建项目 然后 新创建 java .resources 文件夹...... 图上是项目结构 java文件夹下的 文件夹 命名规范 com.nf147(组织名)+ oukele(作者) ...
随机推荐
- .net 简体转换繁体实例,繁体转换简体 Encode.dll、下载
在项目中先引用Encode.dll 下面是下载地址: Encode.dll ChineseConverter.dll 1.html页面代码 <%@ Page Language="C# ...
- lol人物模型提取(二)
两个dds文件怎么导入到一个模型上呢?这模型又不能拆开. 一开始我想的是用两个材质球来完成,一个材质球对应一个dds文件,然而行不通. 一个材质球对应两个dds文件还不太会弄,于是我想着干 ...
- win7 php连接远程oracle
<?php /* 先下载oracle客户端 下载地址 http://www.oracle.com/technetwork/topics/winx64soft-089540.html 下载如下三个 ...
- Jenkins系列-Jenkins忘记密码的修复方法
找回 admin 用户的密码后,可以登录系统修改其他用户的密码. 1. Jenkins 目录结构 Jenkins 没有使用数据库,所有的信息都保存在 JENKINS_HOME 目录下的文件中.其中 J ...
- 初学LINUX版本的选择
1.用于企业环境:建议使用商业版本,例如Red Hat的RHEL或者是Novell的SuSE都是很不错的选择!毕竟企业的环境强调的是永续的经营,你可不希望网管人员走了之后整个机房的主机都没有人管理吧! ...
- 统计VS2013中有效行数
将鼠标放在解决方案处,按下ctrl+shift+F b*[^:b#/]+.*$(带前面的using)^b*[^:b#/]+.*$
- java 文件操作知识点
1.每个文件以一个文件路径和文件名称进行表示,在不同的操作系统环境下,文件路径的表示形式是不一样的,例如在Windows操作系统中一般的表示形式为C:\windows\system,而Unix上的表示 ...
- Python 源码剖析(六)【内存管理机制】
六.内存管理机制 1.内存管理架构 2.小块空间的内存池 3.循环引用的垃圾收集 4.python中的垃圾收集 1.内存管理架构 Python内存管理机制有两套实现,由编译符号PYMALLOC_DEB ...
- (转)java +libsvm 安装与测试:
libsvm 用SVM实现简单线性分类 (转自:http://www.cnblogs.com/freedomshe/archive/2012/10/09/2717356.html) 0. 下载lib ...
- 【BZOJ5329】【SDOI2018】战略游戏(圆方树,虚树)
[BZOJ5329][SDOI2018]战略游戏(圆方树,虚树) 题面 BZOJ 洛谷 Description 省选临近,放飞自我的小Q无心刷题,于是怂恿小C和他一起颓废,玩起了一款战略游戏. 这款战 ...