Spring+springmvc+Mybatis整合案例

Version:annotation版

文档结构图:

从底层开始做起:

01.配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<display-name></display-name>

<!-- 配置spring装载bean的设置 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationcontext.xml</param-value>

</context-param>

<!-- 配置编码 -->

<filter>

<filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 配置springmvc需要的组件设置  -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

02.配置applicationcontext.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:p="http://www.springframework.org/schema/p"

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.2.xsd

         http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

           http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context-4.2.xsd  ">

<!-- 配置  注解start -->

<!--01. 包扫描器 -->

<context:component-scan base-package="cn.zym"></context:component-scan>

<!-- 02.数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driverClass}"></property>

<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

<property name="user" value="${jdbc.user}"></property>

<property name="password" value="${jdbc.password}"></property>

</bean>

<!-- 1.1 关联jdbc.properties -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 02.配置SessionFactory -->

<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

<property name="dataSource" ref="dataSource"></property>

</bean>

<!--03. dao -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>

<property name="basePackage" value="cn.zym.dao"></property>

</bean>

<!-- 开启事务 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 配置注解驱动   是基于注解的方式使用事务配置声明(eg:@Transactional) -->

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

<!-- 配置  注解end -->

一般使用Mybatis时不建议使用注解(会降低程序效率和增加开发难度):这里依然还是选择原生的配置;

mybatis-config.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>

<typeAliases>

这里将pojo包下的类设置了别名(在XXXdao.xml中直接调用该包下的类名即可)

<package name="cn.zym.pojo"/>

</typeAliases>

。。。。添加其他配置文件

</configuration>

Dao层书写:

Ok,这里需要添加对应dao的Mybatis操作文件,该文件通过spring容器生成了代理类,在上面有提到;

<!--03. dao -->   该代理类肯能会有多个,每个代理类的名称的生成规则:

Interface:IUserDao    proxy:IUserDao

Interface:UserDao    proxy:userDao

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>

<property name="basePackage" value="cn.zym.dao"></property>

</bean>

Ok,配置dao对应的文件

IUserDao.xml   这里的xml文件一定要放置在与该接口同目录下(如果不设置具体描述文件路径的话;)

<?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="cn.zym.dao.IUserDao">

<select id="save" parameterType="User">

insert into user (name,password) values (#{name},#{password})

</select>

</mapper>

Ok,接下来是注解配置Controller:

@Controller

@RequestMapping("/")

public class UserController{

@Autowired

@Qualifier("userservice")

private IUserService service;

@RequestMapping(value="/adduser.do",method=RequestMethod.POST)

public String addUser(HttpServletRequest request,User user) throws UnsupportedEncodingException{

request.setCharacterEncoding("utf-8");

System.out.println(user);

service.save(user);

return "forward:/welcome.jsp";

}

这样在前台直接请求携带数据的时候将会触发该Handler,将数据报错到DB中

Useradd.jsp;

<body>

<form action="adduser.do" method="post">

<input type="text" name="name"/><br/>

<input type="text" name="password"/><br/>

<input type="submit" value="submit"/>

</form>

</body>

Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版的更多相关文章

  1. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  2. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  3. Java基础-SSM之Spring和Mybatis整合案例

    Java基础-SSM之Spring和Mybatis整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   在之前我分享过mybatis和Spring的配置案例,想必大家对它们的 ...

  4. ssm之spring+springmvc+mybatis整合初探

    1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...

  5. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  6. Spring+SpringMVC+MyBatis整合基础篇(三)搭建步骤

    作者:13GitHub:https://github.com/ZHENFENG13版权声明:本文为原创文章,未经允许不得转载. 框架介绍 Spring SpringMVC MyBatis easyUI ...

  7. Spring+SpringMVC+MyBatis整合(山东数漫江湖)

    Spring+SpringMVC+MyBatis(SSM)在我们项目中是经常用到的,这篇文章主要讲解使用Intellij IDEA整合SSM,具体环境如下: 数据库:MySQL5.7 依赖管理:Mav ...

  8. Spring+SpringMVC+MyBatis整合进阶篇(四)RESTful实战(前端代码修改)

    前言 前文<RESTful API实战笔记(接口设计及Java后端实现)>中介绍了RESTful中后端开发的实现,主要是接口地址修改和返回数据的格式及规范的修改,本文则简单介绍一下,RES ...

  9. Spring+SpringMVC+MyBatis整合(easyUI、AdminLte3)

    实战篇(付费教程) 花了几天的时间,做了一个网站小 Demo,最终效果也与此网站类似.以下是这次实战项目的 Demo 演示. 登录页: 富文本编辑页: 图片上传: 退出登录: SSM 搭建精美实用的管 ...

随机推荐

  1. Linux5.5安装10g rac

    以前安装总结的,现把它贴出来,虽然10g现在慢慢越少了,但也有不少生产库跑10g的. 1.vi /etc/hosts 10.168.39.243    orcldb1   10.168.39.245  ...

  2. rpm命令

    RPM 安装.卸载.升级.查询和验证. RPM 安装 命令: rpm -i 文件名 如: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rp ...

  3. arc下dealloc得处理

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  4. 增强VPS SSH账号安全:改端口,禁用Root,密钥登录,Denyhosts防暴力攻击

    VPS SSH账号是我们日常管理VPS的主要登入方式,尤其是Root账号,对Linux系统安全至关重要.以前好多站长喜欢用Putty中文版,这实际是别人修改官方Putty汉化而来,这些软件被植入了后门 ...

  5. 牛客网程序员面试金典:1.2——原串翻转(java实现)

    问题描述: 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串. ...

  6. c++学习--继承与派生

    继承和派生 1 含有对象成员(子对象)的派生类的构造函数,定义派生类对象成员时,构造函数的执行顺序如下: 1 调用基类的构造函数,对基类数据成员初始化: 2 调用对象成员的构造函数,对对象成员的数据成 ...

  7. python:让源码更安全之将py编译成so

    应用场景 Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效. python的解释特性是将py编译为独有的二进制编码pyc文件,然后对pyc中的指令进行解 ...

  8. Android IOS WebRTC 音视频开发总结(八十五)-- 使用WebRTC广播网络摄像头视频(下)

    本文主要介绍WebRTC (我们翻译和整理的,译者:weizhenwei,校验:blacker),最早发表在[编风网] 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID:bl ...

  9. webstorm安装后的一些设置技巧:

    如何更改主题(字体&配色):File -> settings -> Editor -> colors&fonts -> scheme name.主题下载地址 如 ...

  10. [转]非常实用的15款开源PHP类库

    源文件:http://www.csdn.net/article/2013-10-09/2817123-PHP-Libraries 英文原文:https://codegeekz.com/useful-p ...