mybatis_开发篇
一、使用mybatis的动态代理方式开发
需求:这里以crm系统中分页条件查询所有的客户信息的功能为例?
1、创建工程
2、引入所需的jar包
3、引入日志文件、数据库连接参数的配置文件等
4、创建mybatis的核心配置文件,其中包括加载数据参数的配置文件和mybatis的映射文件,还有配置数据源(个人比较喜欢使用阿里巴巴的druid)等。
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- spring整合mybatis的配置文件 -->
<!-- 1、加载数据库连接配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2、数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean> <!-- 3、管理mybatis的会话工厂对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean> <!-- 4、管理mybatis中所有mapper接口的代理对象 -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zxz.ssm.crm.mapper"/>
</bean> </beans>
5、创建pojo类
6、创建mybatis的映射文件(配置成功后记得将该映射文件加载到mybatis的核心配置文件中)
<?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="com.zxz.ssm.crm.mapper.CustomerMapper"> <!-- 提取查询条件的sql语句 -->
<sql id="customer_where">
<where>
<if test="custName!=null and custName!=''">
<!-- 【注意:这里尽量使用#{}占位符,是为了防止sql注入的问题】,但是也可以使用${}拼接符 -->
<!-- and cust_name like '%${custName}%' -->
and cust_name like "%"#{custName}"%"
</if>
<if test="custSource!=null and custSource!=''">
and cust_source=#{custSource}
</if>
<if test="custIndustry!=null and custIndustry!=''">
and cust_industry=#{custIndustry}
</if>
<if test="custLevel!=null and custLevel!=''">
and cust_level=#{custLevel}
</if>
</where>
</sql> <!-- 带分页查询数据 -->
<!-- 按用户传递过来的参数条件查询客户数据的集合: -->
<select id="findByQueryVoList" parameterType="com.zxz.ssm.crm.pojo.QueryVo" resultType="com.zxz.ssm.crm.pojo.Customer">
select
c.cust_id,c.cust_name,b1.dict_item_name cust_source,b2.dict_item_name cust_industry,b3.dict_item_name cust_level,
c.cust_linkman,c.cust_phone,c.cust_mobile,c.cust_zipcode,c.cust_address
from customer c
LEFT JOIN base_dict b1 on c.cust_source=b1.dict_id
LEFT JOIN base_dict b2 on c.cust_industry=b2.dict_id
LEFT JOIN base_dict b3 on c.cust_level=b3.dict_id
<include refid="customer_where"/>
limit #{start},#{size}
</select> <!-- 带分页查询数据 -->
<!-- 按用户传递过来的参数条件查询数据的总记录数 -->
<select id="findByQueryCount" parameterType="com.zxz.ssm.crm.pojo.QueryVo" resultType="java.lang.Integer">
select
count(*)
from customer c
LEFT JOIN base_dict b1 on c.cust_source=b1.dict_id
LEFT JOIN base_dict b2 on c.cust_industry=b2.dict_id
LEFT JOIN base_dict b3 on c.cust_level=b3.dict_id
<include refid="customer_where"/>
</select>
54</mapper>
7、通过service层注入mapper接口的代理对象调用查询方法,接着再controller控制层调用service成中的查询方法得到相应的数据,并存放到model对象中,最后填充在页面上即可。
mybatis_开发篇的更多相关文章
- 华清远见金牌讲师名家大讲堂Android开发篇成功举办
2014年3月5日.12日华清远见金牌讲师名家大讲堂(以下简称名家大讲堂)在线讲座全新升级开讲,至此拉开了新一年名家大讲堂的序幕! 华清远见名家大讲堂作为业内颇具影响力的公益免 费线上课程,自2009 ...
- 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)
华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...
- E8.Net工作流平台开发篇
E8.Net开发篇(一) E8.Net开发框架有哪些源程序模型? E8.Net开发框架为开发企业流程应用系统提供了最佳实践的开发架构.范例及源代码,包括待办事项的组织.流程启动模型.处理模型.母版 ...
- linux一句话问答(网络无关篇+网络相关篇+程序开发篇+经典图书)
一句话问答(网络无关篇+网络相关篇+程序开发篇+经典图书) --------------------------目录-网络无关篇-目录-------------------------- 0001 修 ...
- 小试ImageMagik——开发篇
===================================================== ImageMagick的使用和开发的文章: 小试ImageMagik--使用篇 小试Imag ...
- .NET Core实战项目之CMS 第十一章 开发篇-数据库生成及实体代码生成器开发
上篇给大家从零开始搭建了一个我们的ASP.NET Core CMS系统的开发框架,具体为什么那样设计我也已经在第十篇文章中进行了说明.不过文章发布后很多人都说了这样的分层不是很合理,什么数据库实体应该 ...
- Mac 配置教程-开发篇
将 Mac 日常使用的软件和开发软件区分开,将之前写的 Mac 配置的文章分成了两篇: Mac 配置教程-日常篇 Mac 配置教程-开发篇 图床 iPic 设置快捷键 Command+Shift+u ...
- Hyperledger fabric-SDK-GO客户端开发篇(六)
Hyperledger fabric-SDK-GO客户端开发篇(六) Fabric-SDK-GO是提供的Go语言开发包,应用程序可以利用Fabric-SDK-GO与fabric网络进行交互并访问链码. ...
- nginx模块开发篇 (阿里著作)
背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...
随机推荐
- 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- 恢复SQL Server被误删除的数据
恢复SQL Server被误删除的数据 <恢复SQL Server被误删除的数据(再扩展)> 地址:http://www.cnblogs.com/lyhabc/p/4620764.html ...
- nw.js桌面软件开发系列 第0.1节 HTML5和桌面软件开发的碰撞
第0.1节 HTML5和桌面软件开发的碰撞 当我们谈论桌面软件开发技术的时候,你会想到什么?如果不对技术本身进行更为深入的探讨,在我的世界里,有这么多技术概念可以被罗列出来(请原谅我本质上是一个Win ...
- MVC Core 网站开发(Ninesky) 2.1、栏目的前台显示
上次创建了栏目模型,这次主要做栏目的前台显示.涉及到数据存储层.业务逻辑层和Web层.用到了迁移,更新数据库和注入的一些内容. 一.添加数据存储层 1.添加Ninesky.DataLibrary(与上 ...
- Node.js:进程、子进程与cluster多核处理模块
1.process对象 process对象就是处理与进程相关信息的全局对象,不需要require引用,且是EventEmitter的实例. 获取进程信息 process对象提供了很多的API来获取当前 ...
- CSS 3学习——边框
在CSS 3中可以设置边框圆角.边框阴影和边框图像,分别通过border-radius.border-image和box-shadow属性设置. 边框圆角 border-radius属性是以下4个属性 ...
- Spring resource bundle多语言,单引号format异常
Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...
- ASP.NET中常用的优化性能的方法
1. 数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET中提供了连接池( ...
- animate.css(第三方动画使用方法)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px Monaco; color: #a5b2b9 } animation 语法: animatio ...
- 如何理解DT将是未来IT的转型之路?
如今的IT面临着内忧外患的挑战. 一方面,企业多多少少都建立了信息化,有些企业或集团甚至会有数几十个分公司,包含直销.代理.零售以及第三方物流等多种业态.越是复杂的业务,信息化建设越困难,比如运用大量 ...