Mybatis+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" 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">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</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>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PostEncoding</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>
</filter>
<filter-mapping>
<filter-name>PostEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>changMethod</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>changMethod</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name>MIPO_CRM</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、jdbc.properties(Mysql)
#config
mySQLdriver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/crm
user=root
pwd=
三、spring.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 扫包 -->
<context:component-scan base-package="com.wql.service"/>
<context:component-scan base-package="com.wql.dao"/> <!-- 引用jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${mySQLdriver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${pwd}"/>
</bean> <!--获取sqlSesssionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/wql/vo/*.xml"/>
<property name="typeAliasesPackage" value="com.wql.vo"/>
</bean> <!-- 获取SqlSessionTemplate -->
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean> <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 使用事务的注解模式 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
四、springMVC.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 扫包 -->
<context:component-scan base-package="com.jun.controller"/> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 访问静态资源 使用tomcat的默认的servlet-->
<mvc:annotation-driven/> <mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/imgs/" mapping="/imgs/**"/>
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/easyui/" mapping="/easyui/**"/>
<mvc:resources location="/My97DatePicker/" mapping="/My97DatePicker/**"/> </beans>
下面贴一下模型层、服务层代码,看一下在模型层是如何通过注解模式获取spring.xml中的SqlSessionFactory实例的以及在服务层如何通过注解模式达到事务配置的。
1.模型层
BaseDaoImpl.java 从下面代码可看出在BaseDaoImpl类中是通过@Autowired标识符装载spring.xml中的SqlSessionFactory实例的。
public class BaseDaoImpl<T> {
private Class clazz;
public BaseDaoImpl(){
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
clazz = (Class)type.getActualTypeArguments()[0];
}
@Autowired
protected SqlSessionTemplate session;
/**
* 添加
* @param obj
*/
public boolean insert(Object obj){
return session.insert(clazz.getName()+".insert" , obj)>0;
}
/**
* 修改
*/
public boolean update(Object obj){
return session.update(clazz.getName()+".update",obj)>0;
}
/**
* 删除
*/
public boolean delete(Serializable id){
return session.delete(clazz.getName()+".delete",id)>0;
}
/**
* findbyId
*/
public T findById(Serializable id){
return session.selectOne(clazz.getName()+".findById", id);
}
/**
* findAll
*/
public List<T> findAll(Map map){
return session.selectList(clazz.getName()+".findAll", map);
}
/**
* list
* @return
*/
public List<T> list(){
return session.selectList(clazz.getName()+".list");
}
/**
* 分页
* @param curPage 当前页
* @param pageRows 每页的条数
* @param map
* @return
*/
public Page getPageData(long curPage , long pageRows ,Map map){
Page page = new Page();
long count = session.selectOne(clazz.getName()+".getCount",map);//获取数据的总条数
long allPage = (count+pageRows -1)/pageRows;//根据每页的行数+总条数获取总页数
//设置当前页
if(curPage < 1)curPage = 1;//如果当前页小于1则设当前页的值为1
if(allPage > 0 && curPage > allPage ) curPage = allPage;//如果当前页大于总页数则设当前页为总页数
map.put("start", (curPage -1)* pageRows);//设置查询的起始值
map.put("stop", pageRows);//设置返回的最大条数——即每页的行数
//获取数据
List<T> dataList = session.selectList(clazz.getName()+".getDataList",map);
page.setCurPage(curPage);//当前页
page.setPageRows(pageRows);//每页的行数
page.setPageNum(allPage);//总页数
page.setDataList(dataList);//数据
return page;
}
}
OrdergoodDaoImpl.java
@Repository
public class OrdergoodDaoImpl extends BaseDaoImpl<Ordergood>{ /**
* 显示所有订单商品表(根据id查询)
* @return
*/
public List<Ordergood> listOrdergood(Map map){
return super.findAll(map);
} /**
* 添加订单商品表
* @param ordergoods
* @return
*/
public boolean addOrdergood(List<Ordergood> ordergoods){
return session.insert(Ordergood.class.getName()+".addOrdergood", ordergoods)>0;
} }
2.服务层
FunctionService.java 从下面代码可看出在服务层是通过@Transactional标识符来配置事务的。
@Service
@Transactional
public class FunctionService { @Autowired
private FunctionDaoImpl functionDao;
/**
* 所有的权限
* @return
*/
public List<Function> listFunction(){
return functionDao.listFunction();
}
/**
* 父权限查询
* @param map
* @return
*/
public List<Function> listParentFunction(){
return functionDao.listParentFunction();
}
/**
* 子权限查询
* @param map
* @return
*/
public List<Function> listChildFunction(int parentid){
return functionDao.listChildFunction(parentid);
}
}
Mybatis+SpringMVC的项目环境搭建的更多相关文章
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- mac OS X下Java项目环境搭建+IntelliJ IDEA Jrebel插件安装与破解+Office 2016破解版安装
一.mac OS X下Java项目环境搭建 因为某些原因新入手了台最新版的MacBook Pro,意味着今天要花一天时间安装各种软件以及项目环境搭建╮(╯▽╰)╭ 项目环境搭建步骤: 1.安装jdk ...
- 第一周博客之二---OA项目环境搭建及开发包部署
OA项目环境搭建 一个项目想要能够在开发人员打包好项目包之后进行测试,就必须进行项目测试环境的搭建,要根据开发工程师的开发环境采用不同的测试环境,以下只是浅谈下Java项目OA(办公自动化平台)的环境 ...
- vue项目ide(vue项目环境搭建)
一.先介绍一下我接下来要做的项目 项目:ide可视化工具 技术应用: Vue2.0(js框架):https://cn.vuejs.org/ ElementUi(饿了吗ui框架基于vue的):http: ...
- MyBatis实例教程--开发环境搭建
MyBatis实例教程--开发环境搭建 准备工作: 1.mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包 ...
- react 开发 PC 端项目(一)项目环境搭建 及 处理 IE8 兼容问题
步骤一:项目环境搭建 首先,你不应该使用 React v15 或更高版本.使用仍然支持 IE8 的 React v0.14 即可. 技术选型: 1.react@0.14 2.bootstrap3 3. ...
- Vue 项目环境搭建
Vue项目环境搭建 ''' 1) 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 2) 换源安装cnpm >: npm install -g cnp ...
- Django项目: 项目环境搭建 ---- 一、创建django项目
项目环境搭建 一.创建django项目 1.创建python虚拟环境 在虚拟机上创建python虚拟环境,因为实际项目部署,实在linux mkvirtualenv -p /usr/bin/pytho ...
- React全家桶打造共享单车后台管理系统项目_第1篇_项目环境搭建_首页编写
1.项目介绍 项目github地址:https://github.com/replaceroot/React-manageSystem 项目整体架构: 课程大纲: 第一章:React基础知识 ...
随机推荐
- CSS3 动画记
css3 动画 在CSS3中可以通过animation创建复杂的动画序列,像transition属性一样用来控制CSS的属性实现动画效果. animation实现动画效果主要由两个部分组成. 通过类似 ...
- Bootstrap插件之Carousel轮播效果(2015年-05月-21日)
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...
- mysql中文乱码的一点理解
我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B. 如果你也遇到了这个问题,咱 ...
- Linux 命令 - jobs: 显示后台作业的状态信息
命令格式 jobs [-lnprs] [jobspec ...] jobs -x command [args] 命令参数 -l 额外显示作业的进程 ID. -n 只列出状态发生变化的进程. -p 只列 ...
- 详解Win2003 IIS6.0 301重定向带参数的问题(转摘)
网站更换域名,把旧域名用301指到新域名来. 从iis中设置url永久转向就可以,看上去很容易,用了一会儿才发现,参数都没有带上. 从微软网站上找到如下说明,果然好使: 重定向参考 (IIS 6. ...
- (转)IDG副总裁楼军:顶级VC青睐什么样的创业者
学习能力是创业者的第一能力 创业者首先要有格局观和很强的学习能力. 具体什么意思?比如说去年IDG投了一个做C2C平台的海淘项目,创始人之前其实是帮他爱人做海淘代购.他爱人是一个代购买手,赚得还不错, ...
- (转)DockPanel的一点点改进
1.当双击Tab时,原先是直接把当前Tab所表示的这个窗体,从主窗体的框架上分离现来,成为一个浮动的窗体.这不是我想要的,我把它改成了双击关闭.在DockPaneStripBase的WndProc方法 ...
- css 笔记——设置禁用中文输入法
ime-mode ime-mode的语法解释如下: ime-mode : auto | active | inactive | disabled 取值: auto : 默认值.不影响IME的状态.与 ...
- Ubuntu gedit 折叠插件
Ubuntu Kylin 14.04 gedit - Version 3.10.4 (as same as all version of gedit 3.x ) Attention: this pl ...
- IAR:Error [Li005]:no definition for"***" 问题之连接
对于 IAR 出现的 Error[Li005] 链接错误,网上已经给出了比较详尽的解决方法,而对于这次记录,主要是记录解决问题的思路. 网上给出的方法:http://blog.csdn.net/yue ...