首页 技术 正文
技术 2022年11月8日
0 收藏 640 点赞 1,358 浏览 4019 个字

一、使用mybatis的动态代理方式开发

需求:这里以crm系统中分页条件查询所有的客户信息的功能为例?

1、创建工程

2、引入所需的jar包

3、引入日志文件、数据库连接参数的配置文件等

4、创建mybatis的核心配置文件,其中包括加载数据参数的配置文件和mybatis的映射文件,还有配置数据源(个人比较喜欢使用阿里巴巴的druid)等。

mybatis_开发篇

 <?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对象中,最后填充在页面上即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905