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基础知识 ...
随机推荐
- Spring(3.2.3) - Beans(3): Bean 实例的创建方式
创建一个 Bean 实例对象的方法通常有如下方式: 调用构造器创建 Bean 实例 调用静态工厂方法创建 Bean 实例 调用实例工厂方法创建 Bean 实例 使用构造器创建 Bean 实例 XML ...
- 为Widget添加事件
原帖:http://bbs.51cto.com/thread-965565-1-1.html 在appWidget中,ImageButton和Button都是被支持的控件,其事件可分成三种类型: ...
- UML——类和对象
- Insert data from excel to database
USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...
- windows server 2008下装SQL 2008R2x64
1. 在windows server 2008下装SQL 2008出现 This SQL Server Setup media is not supported on a X64 system 使用虚 ...
- Koajs原理
Koajs让习惯阻塞式代码写法的同学感到很舒服,再也不用盖楼式的callback了,而且也不需要学习Promise的then,catch这些新东西. 但实际上,Koajs这样的写法有点像是语言的语法糖 ...
- CodeForces 679B(Bear and Tower of Cubes)
题意:Limak要垒一座由立方体垒成的塔.现有无穷多个不同棱长(a>=1)的立方体.要求:1.塔的体积为X(X<=m).2.在小于X的前提下,每次都选体积最大的砖块.3.在砖块数最多的前提 ...
- lex&yacc5--YYSTYPE
yacc里的YYSTYPE默认是int型的,当然也可以勇%union来定义联合但是由于程序需要,我要将YYSTYPE定义为我自己定义的一个struct的指针然后作为一个全局变量,让lex在扫描的时候, ...
- 结构型模式——Adapter
1.意图 将一个类的接口转换成客户希望的另一个接口.使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 2.结构 类适配器 对象适配器 3.参与者 Target定义Client使用的与特定领域 ...
- C++ 读取 pcap文件.
http://blog.csdn.net/haolipengzhanshen/article/details/51854853 1.了解下pcap文件的结构 2.定义pcap文件头部结构体pcapFi ...