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 基本数据结 ...
随机推荐
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- 闰秒导致MySQL服务器的CPU sys过高
今天,有个哥们碰到一个问题,他有一个从库,只要是启动MySQL,CPU使用率就非常高,其中sys占比也比较高,具体可见下图. 注意:他的生产环境是物理机,单个CPU,4个Core. 于是,他抓取了CP ...
- .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...
- ABP教程-打造一个《电话簿项目》-目录-MPA版本-基于ABP1.13版本
此系列文章会进行不定期的更新,应该会有6章左右. 感兴趣的朋友可以跟着看看,本教程适合已经看过ABP的文档但是又无从下手的小伙伴们. 初衷: 发布系列教程的原因是发现ABP在园子火了很久,但是发现还是 ...
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- Java学习之反射机制及应用场景
前言: 最近公司正在进行业务组件化进程,其中的路由实现用到了Java的反射机制,既然用到了就想着好好学习总结一下,其实无论是之前的EventBus 2.x版本还是Retrofit.早期的View注解框 ...
- CSS 3学习——box-sizing和背景
box-sizing 在CSS 2中设置元素的width和height仅仅是设置了元素内容区的宽和高,元素实际的尺寸是margin + border + padding + 内容区. CSS 3(截止 ...
- iptables
一.在服务器上打开 22.80.9011端口: iptables -A INPUT -p tcp --dport 9011 -j ACCEPT iptables -A OUTPUT -p tcp -- ...
- 【Java学习系列】第3课--Java 高级教程
本文地址 可以拜读: 从零开始学 Java 分享提纲: 1. Java数据结构 2. Java 集合框架 3. Java泛型 4. Java序列化 5. Java网络编程 6. Java发送Email ...
- 【干货分享】流程DEMO-借款申请
流程名: 借款申请 业务描述: 当员工个人在工作中需要进行借款时,通过此项流程提交借款申请,审批通过后,财务部进行款项支付. 流程相关文件: 流程包.xml WebService业务服务.xm ...