SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql
准备工作:
下载整合所需的jar包 点击此处下载
使用MyBatis Generator生成dao接口、映射文件和实体类 如何生成
搭建过程:
先来看一下项目的 目录结构

1.配置dispatcherServlet-servlet.xml,将此文件放于WEB-INF下
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自动扫描Controller包 -->
<context:component-scan base-package="com.ssm.controller"/>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="suffix" value="" />
</bean>
</beans>
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- 欢迎页面 -->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- spring mvc的配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决访问静态资源问题 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<!-- 解决中文乱码 -->
<filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.配置applicationContext.xml,并 将自动生成的dao实现类交给Spring管理
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 定义dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 指定JDBC驱动类 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<!-- 提供连接数据库的URL地址 -->
<property name="url" value="jdbc:mysql://localhost:3306/easybuy">
</property>
<!-- 提供连接数据库的用户名和密码 -->
<property name="username" value="root"></property>
<property name="password" value="pengxiongpengdi"></property>
</bean>
<!-- 定义SessionFactory Bean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mapperLocations" value="classpath:com/buy/dao/*.xml"></property>
</bean>
<!-- 此处 将自动生成的dao实现类交给Spring管理 -->
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.buy.dao.UserEntityMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--开启注解 将com.jbit包中所有使用了注解的类扫描,然后创建成对象 -->
<context:component-scan base-package="com.buy" />
</beans>
4.将service实现类交给Spring管理
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired // 此处使用ByType自动装配 dao实现类
private UserEntityMapper userDao;
@Override
public UserEntity login(UserEntity user) {
return userDao.getByNameAndPwd(user);
}
@Override
@Transactional // 使用该注解实现事务管理
public void delById(String id) {
userDao.delByid(id);
}
}
5.创建Controller
@Controller@RequestMapping(value="/emp")
public class EmpController {
@Autowired // 使用ByType装配private IEmpService empService;
@RequestMapping(value="/queryEmps")
public String queryEmps(HttpServletRequest request){
List<Emp> list = empService.queryAll();
request.getSession().setAttribute("list",list );
return "index.jsp";
}
6.现在就可以使用 http://localhost:8080/ProjectName/emp/queryEmps访问到上述Controller
SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql的更多相关文章
- SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)
1.开发环境搭建以及创建Maven Web项目 参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html ...
- SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]
SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...
- SSM框架整合(Spring + SpringMVC + MyBatis)
搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...
- Maven+SSM框架搭建【spring+springmvc+mybatis】
本案例用到:ssm[spring+springmvc+mybatis]框架 数据库:mysql (推荐使用mysql 或者 sqlserver .oracle太大,一般大型项目才会用到) 开发工具: ...
- SSM框架的配置Spring+Springmvc +Mybatis
ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...
- SSM后台管理系统(Spring SpringMVC Mybatis Mysql EasyUI)
非常简单的一个后台管理系统,功能不多,框架也不复杂, 源码下载(附数据库)-ssm后台管理系统框架(Spring mvc + mybatis + mysql + easyui ) 实例图片
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller //控制类 service //服务接口 service.impl //服务 ...
- SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例
简介: SSM框架是Spring,SpringMVC 和Mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,dao层四层. Spring实现 ...
- 04_SSM框架整合(Spring+SpringMVC+MyBatis)
[SSM的系统架构] [整合概述] 第一步: MyBatis和Spring整合,通过Spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册. 第二 ...
随机推荐
- windows下egret环境搭建
作者:zccst 2,下载安装WebStorm(经了解是西欧捷克开发的,欧洲人对审美不够,所以界面不太好看,但是功能很强大) (1)遇到的问题是注册码问题,不过也很容易在网上找到. (2)如何打开已创 ...
- 使用myeclipse新建和删除web项目时一定要小心
今天使用myeclipse在非工作空间的路径下新建一个web项目,路径内包含了原有一些web项目,可是我在指定位置时,在那个路径下多写一个子目录的路径,以为myeclipse会 为我默认新建子目录以便 ...
- iOS学习基本常识
转发至:http://blog.sina.com.cn/s/blog_9266da3d010184i0.html 1. 了解main函数, UIApplication是初始化程序的核心,它接受4个参 ...
- Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。_StarSasumi_新浪博客
Ubuntu"无法解析或打开软件包的列表或是状态文件"的解决办法. (2011-04-30 14:56:14) 转载▼ 标签: ubuntu apt 分类: Ubuntu/Linu ...
- 模块的_name_
模块的__name__每个模块都有一个名称,在模块中可以通过语句来找出模块的名称.这在一个场合特别有用——就如前面所提到的,当一个模块被第一次输入的时候,这个模块的主块将被运行.假如我们只想在程序本身 ...
- ajax--2017年1月15日
听说点六下就能复制了? ajax: 一般处理程序(数据接口):ashx 跨语言传递数据:xml: 结构不清晰 代码量比较大 查找起来比较费事 非面向对象结构 json: 结构清晰 代码量相对较小 面向 ...
- MYsql数据库ERROR总结
描述:#Warning: Using a password on the command line interface can be insecure.#ERROR 1045 (28000): Acc ...
- Word中的公式向上偏或向下偏的解决方法
在word 2010中,发现公式无法与文字排成一行时,可选中文字,然后点“字体”,然后“高级”选项中选择“位置”,然后根据不同情况选择“标准”.“提升”.“降低”.
- java 图片生成缩略图后,转化成流
功能:图片生成缩略图后,转化成流 public class ImageUtils { /** * * @param in1 * 文件流 * @param uploadFileName * 文件名称 * ...
- 1.TCP/IP基本概念
为什么会有TCP/IP协议 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样 ...