最近公司需要对客户手机进行管理并提供二维码存储手机串号的加密字符.供其他接入系统通过扫面二维码解析使用.系统提供手机信息管理,客户管理,用户管理功能.

1.使用到的POJO类

1.1 User

package com.green.phonemanage.model;

/**
* @author maybo 用户实体类
*/
public class User {
private int id;
private String loginName;// 登录名
private String passwd;// 密码
private String name;// 用户名字
private String idCard;// 用户身份证
private String sex;// 性别
private int age;// 年龄
private String phone;// 电话
private String department;// 部门
private int status;// 状态
private int role;// 角色 public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getLoginName() {
return loginName;
} public void setLoginName(String loginName) {
this.loginName = loginName;
} public String getPasswd() {
return passwd;
} public void setPasswd(String passwd) {
this.passwd = passwd;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getIdCard() {
return idCard;
} public void setIdCard(String idCard) {
this.idCard = idCard;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getDepartment() {
return department;
} public void setDepartment(String department) {
this.department = department;
} public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} public int getRole() {
return role;
} public void setRole(int role) {
this.role = role;
} @Override
public String toString() {
return "User [id=" + id + ", loginName=" + loginName + ", passwd="
+ passwd + ", name=" + name + ", idCard=" + idCard + ", sex="
+ sex + ", age=" + age + ", phone=" + phone + ", department="
+ department + ", status=" + status + ", role=" + role + "]";
} }

  1.2Client

package com.green.phonemanage.model;

/**
* @author maybo
*
*/
public class Client {
private int id;
private String code;// 客户编码
private String address;// 地址
private String name;// 客户名字
private String idCard;// 客户身份证
private String phone;// 客户电话
private String province;// 省
private String city;// 市
private String area;// 区 public void setArea(String area) {
this.area = area;
} public void setCity(String city) {
this.city = city;
} public void setProvince(String province) {
this.province = province;
} public String getArea() {
return area;
} public String getCity() {
return city;
} public String getProvince() {
return province;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getIdCard() {
return idCard;
} public void setIdCard(String idCard) {
this.idCard = idCard;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} @Override
public String toString() {
return "Client [id=" + id + ", code=" + code + ", address=" + address
+ ", name=" + name + ", idCard=" + idCard + ", phone=" + phone
+ "]";
} }

  1.3CellPhone

package com.green.phonemanage.model;

import java.util.Arrays;
import java.util.Date; /**
* @author maybo
*
*/
public class CellPhone {
private int id;
private String phoneBrand;// 手机品牌
private String phoneModel;// 手机型号
private String phoneColor;// 手机颜色
private Client client;// 客户
private Client receiver;// 签收人
private String createDate;// 创建时间
private String serviceLife;// 寿命
private byte[] qrCode;// 二维码
private int status;// 状态
private String imei;// 手机串号 public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getPhoneBrand() {
return phoneBrand;
} public void setPhoneBrand(String phoneBrand) {
this.phoneBrand = phoneBrand;
} public String getPhoneModel() {
return phoneModel;
} public void setPhoneModel(String phoneModel) {
this.phoneModel = phoneModel;
} public String getPhoneColor() {
return phoneColor;
} public void setPhoneColor(String phoneColor) {
this.phoneColor = phoneColor;
} public Client getClient() {
return client;
} public void setClient(Client client) {
this.client = client;
} public Client getReceiver() {
return receiver;
} public void setReceiver(Client receiver) {
this.receiver = receiver;
} public String getCreateDate() {
return createDate;
} public void setCreateDate(String createDate) {
this.createDate = createDate;
} public void setServiceLife(String serviceLife) {
this.serviceLife = serviceLife;
} public String getServiceLife() {
return serviceLife;
} public byte[] getQrCode() {
return qrCode;
} public void setQrCode(byte[] qrCode) {
this.qrCode = qrCode;
} public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} public void setImei(String imei) {
this.imei = imei;
} public String getImei() {
return imei;
} @Override
public String toString() {
return "CellPhone [id=" + id + ", phoneBrand=" + phoneBrand
+ ", phoneModel=" + phoneModel + ", phoneColor=" + phoneColor
+ ", client=" + client + ", receiver=" + receiver
+ ", createDate=" + createDate + ", serviceLife=" + serviceLife
+ ", qrCode=" + Arrays.toString(qrCode) + ", status=" + status
+ ", IMEI=" + imei + "]";
} }

  2.定义Dao层接口

2.1 CellPhoneDao

package com.green.phonemanage.dao;

import java.sql.Blob;
import java.util.List; import com.green.phonemanage.model.CellPhone;
import com.green.phonemanage.model.SearchForm; /**
* @author maybo
*
*/
public interface CellPhoneDao {
public void add(CellPhone cellPhone);// 添加手机 public void rmove(int id);// 删除手机 public void update(CellPhone cellPhone);// 修改手机 public List<CellPhone> finds(SearchForm searchForm);// 查询手机 public CellPhone findById(int id);// 查询手机通过id public Integer findByIMEI(String IMEI);// 通过手机串号查询手机是否已经存在 public CellPhone findQr(int id);// 查询手机二维码通过id public Integer findByClient(int id);//查询通过用户id public Integer findTotalByStatus(int status);//查询总数通过状态 public Integer findTotalByBrand(String brand);//查询总数通过品牌 public Integer findTotalByAddress(String address);//查询总数通过地址 public List<String> findBrands();//查询品牌 public List<Integer> findStatus();//查询所状态 public List<String> findCitys();//查询所有城市 public Integer totals();//查询总数 }

  2.2 ClientDao

package com.green.phonemanage.dao;

import java.util.List;

import com.green.phonemanage.model.Client;
import com.green.phonemanage.model.SearchForm; /**
* @author maybo
*
*/
public interface ClientDao {
public void add(Client client);// 添加客户 public void rmove(int id);// 删除客户 public void update(Client client);// 修改客户 public List<Client> finds(SearchForm searchForm);// 查询客户 public Client findById(int id);// 查询客户通过id public Integer findByIdCard(String idCard);//通过身份证查询客户是否存在 public Integer totals();//查询总数
}

  2.3 UserDao

package com.green.phonemanage.dao;

import java.util.List;

import com.green.phonemanage.model.SearchForm;
import com.green.phonemanage.model.User; /**
* @author maybo
*
*/
public interface UserDao {
public Integer login(User user);// 用户登录 public void register(User user);// 用户注册 public void rmove(int id);// 删除用户 public void update(User user);// 修改用户 public List<User> finds(SearchForm searchForm);// 查找用户 public User findById(int id);// 查询用户通过id public Integer findByLoginName(String loginName);// 查询登录名是否存在 public void freeze(int id);//冻结用户 public void unfreeze(int id);//用户解冻 public Integer totals();//查询总数
}

  3.Mapper文件

3.1 user.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="com.green.phonemanage.dao.UserDao">
<resultMap type="User" id="userMap">
<id column="id" property="id" />
<result column="login_name" property="loginName" />
<result column="age" property="age" />
<result column="passwd" property="passwd" />
<result column="name" property="name" />
<result column="id_card" property="idCard" />
<result column="sex" property="sex" />
<result column="phone" property="phone" />
<result column="department" property="department" />
<result column="status" property="status" />
<result column="role" property="role" />
</resultMap>
<sql id="sql_select">
select *
</sql>
<sql id="select_where">
from user
<if test="key!=null">
where
login_name like "%"#{key}"%" or phone like
"%"#{key}"%" or name like
"%"#{key}"%"
</if>
<if test="key==null">
order by id desc
limit #{pageIndex},#{pageSize}
</if>
</sql>
<select id="findById" parameterType="int" resultMap="userMap">
select *
from user u where u.id=#{id}
</select>
<insert id="register" parameterType="User">
insert into
user(login_name,age,passwd,name,id_card,sex,phone,department,status,role)
values(#{loginName},#{age},#{passwd},#{name},#{idCard},#{sex},#{phone},#{department},#{status},#{role})
</insert>
<update id="update" parameterType="User">
update user set
login_name=#{loginName},age=#{age},passwd=#{passwd},name=#{name},id_card=#{idCard},sex=#{sex},phone=#{phone},department=#{department},status=#{status},role=#{role}
where id=#{id}
</update>
<update id="unfreeze" parameterType="int">
update user set status=1
where id=#{id} and role=1
</update>
<update id="freeze" parameterType="int">
update user set status=0 where
id=#{id} and role=1
</update>
<select id="login" parameterType="User" resultType="integer">
select u.id
from user u where u.login_name=#{loginName} and u.passwd=#{passwd} and u.role=#{role} and u.status=1 limit 0,1
</select>
<select id="findByLoginName" parameterType="string" resultType="integer">
select u.id from user u where u.login_name=#{loginName} and u.role=1
</select>
<delete id="rmove" parameterType="int">
delete from user where id=#{id}
</delete>
<select id="finds" parameterType="SearchForm" resultMap="userMap">
<include refid="sql_select"></include>
<include refid="select_where"></include>
</select>
<select id="totals" resultType="Integer">
select count(*) from user
</select>
</mapper>

  3.2 cellphone.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="com.green.phonemanage.dao.CellPhoneDao">
<resultMap type="CellPhone" id="cellPhoneMap">
<id column="id" property="id" />
<result column="phone_brand" property="phoneBrand" />
<result column="phone_model" property="phoneModel" />
<result column="phone_color" property="phoneColor" />
<result column="create_date" property="createDate" />
<result column="service_life" property="serviceLife" />
<result column="Qr_code" property="qrCode" />
<result column="status" property="status" />
<result column="IMEI" property="imei" />
<association property="client" javaType="Client">
<id column="cl_id" property="id" />
<result column="cl_code" property="code" />
<result column="cl_address" property="address" />
<result column="cl_name" property="name" />
<result column="cl_id_card" property="idCard" />
<result column="cl_phone" property="phone" />
</association>
<association property="receiver" javaType="Client">
<id column="cli_id" property="id" />
<result column="cli_code" property="code" />
<result column="cli_address" property="address" />
<result column="cli_name" property="name" />
<result column="cli_id_card" property="idCard" />
<result column="cli_phone" property="phone" />
</association>
</resultMap>
<sql id="sql_select">
select c.id,c.phone_brand,c.phone_model,c.phone_color,c.create_date,c.service_life,c.status,c.IMEI,cl.id as cl_id,cl.code as cl_code,cl.address as
cl_address,cl.name as cl_name,cl.id_card as cl_id_card,cl.phone as
cl_phone,cli.id as cli_id,cli.code as cli_code,cli.address as
cli_address,cli.name as cli_name,cli.id_card as cli_id_card,cli.phone
as cli_phone
</sql>
<sql id="select_where">
from cellphone c left join client cl on c.client=cl.id left join
client cli on c.receiver=cli.id
<if test="key!=null">
where phone_brand like "%"#{key}"%" or phone_model like
"%"#{key}"%" or IMEI like
"%"#{key}"%"
</if>
order by id desc limit #{pageIndex},#{pageSize}
</sql>
<select id="findById" parameterType="int" resultMap="cellPhoneMap">
select
c.id,c.phone_brand,c.phone_model,c.phone_color,c.create_date,c.service_life,c.status,c.IMEI,cl.id as cl_id,cl.code as cl_code,cl.address as cl_address,cl.name
as cl_name,cl.id_card as cl_id_card,cl.phone as cl_phone,cli.id as
cli_id,cli.code as cli_code,cli.address as cli_address,cli.name as
cli_name,cli.id_card as cli_id_card,cli.phone as cli_phone from
cellphone c left join client cl on c.client=cl.id left join client cli
on c.receiver=cli.id where c.id=#{id}
</select>
<select id="findByIMEI" parameterType="string" resultType="integer">
select c.id from cellphone c where c.IMEI=#{IMEI}
</select>
<insert id="add" parameterType="CellPhone">
insert into
cellphone(phone_brand,phone_model,phone_color,create_date,service_life,Qr_code,status,IMEI,client,receiver)
values(#{phoneBrand},#{phoneModel},#{phoneColor},#{createDate},#{serviceLife},#{qrCode},#{status},#{imei},#{client.id},#{receiver.id})
</insert>
<update id="update" parameterType="CellPhone">
update cellphone set
phone_brand=#{phoneBrand},phone_model=#{phoneModel},phone_color=#{phoneColor},create_date=#{createDate},service_life=#{serviceLife},Qr_code=#{qrCode},status=#{status},IMEI=#{imei},client=#{client.id},receiver=#{receiver.id}
where id=#{id}
</update>
<delete id="rmove" parameterType="int">
delete from cellphone where
id=#{id}
</delete>
<select id="finds" parameterType="SearchForm" resultMap="cellPhoneMap">
<include refid="sql_select"></include>
<include refid="select_where"></include>
</select>
<select id="findQr" parameterType="int" resultMap="cellPhoneMap">
select Qr_code from cellphone where id=#{id}
</select>
<select id="findByClient" parameterType="int" resultType="integer">
select c.id from cellphone c where c.client=#{id} or c.receiver=#{id}
</select>
<select id="findTotalByStatus" parameterType="integer" resultType="integer">
select count(*) from cellphone where status=#{status}
</select>
<select id="findTotalByBrand" parameterType="string" resultType="integer">
select count(*) from cellphone c where c.phone_brand=#{brand}
</select>
<select id="findTotalByAddress" parameterType="string" resultType="integer">
select count(*) from cellphone c left join client cl on c.client=cl.id where cl.city=#{address}
</select>
<select id="findStatus" resultType="Integer">
select distinct status from cellphone
</select>
<select id="findBrands" resultType="string">
select distinct phone_brand from cellphone
</select>
<select id="findCitys" resultType="string">
select distinct cl.city from cellphone c left join client cl on c.client=cl.id
</select>
<select id="totals" resultType="Integer">
select count(*) from cellphone
</select>
</mapper>

  3.3 client.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="com.green.phonemanage.dao.ClientDao">
<resultMap type="Client" id="clientMap">
<id column="id" property="id" />
<result column="code" property="code" />
<result column="address" property="address" />
<result column="name" property="name" />
<result column="id_card" property="idCard" />
<result column="phone" property="phone" />
<result column="province" property="province" />
<result column="city" property="city" />
<result column="area" property="area" />
</resultMap>
<sql id="sql_select">
select *
</sql>
<sql id="select_where">
from client
<if test="key!=null">
where
code like "%"#{key}"%" or name like "%"#{key}"%" or
phone like
"%"#{key}"%"
</if>
<if test="key==null">
order by id desc
limit #{pageIndex},#{pageSize}
</if>
</sql>
<select id="findById" parameterType="int" resultMap="clientMap">
select *
from client c where c.id=#{id}
</select>
<select id="findByIdCard" parameterType="string" resultType="integer">
select c.id from client c where c.id_card=#{idCard} limit 0,1
</select>
<insert id="add" parameterType="Client">
insert into
client(code,address,name,id_card,phone,province,city,area)
values(#{code},#{address},#{name},#{idCard},#{phone},#{province},#{city},#{area})
</insert>
<update id="update" parameterType="Client">
update client set
code=#{code},address=#{address},name=#{name},id_card=#{idCard},phone=#{phone},province=#{province},city=#{city},area=#{area}
where id=#{id}
</update>
<delete id="rmove" parameterType="int">
delete from client where
id=#{id}
</delete>
<select id="finds" parameterType="SearchForm" resultMap="clientMap">
<include refid="sql_select"></include>
<include refid="select_where"></include>
</select>
<select id="totals" resultType="Integer">
select count(*) from client
</select>
</mapper>

  4.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>
<typeAlias type="com.green.phonemanage.model.CellPhone"
alias="CellPhone" />
<typeAlias type="com.green.phonemanage.model.Client" alias="Client" />
<typeAlias type="com.green.phonemanage.model.User" alias="User" />
<typeAlias type="com.green.phonemanage.model.SearchForm"
alias="SearchForm" />
<typeAlias type="com.green.phonemanage.dao.CellPhoneDao"
alias="CellPhoneDao" />
<typeAlias type="com.green.phonemanage.dao.ClientDao" alias="ClientDao" />
<typeAlias type="com.green.phonemanage.dao.UserDao" alias="UserDao" />
</typeAliases>
<mappers>
</mappers>
</configuration>

  其中数据库管理交给spring来管理如果只需启动mybaits那么可以参考mybaits安装将环境配置加入配置文件.

Mybatis 实现手机管理系统的持久化数据访问层的更多相关文章

  1. 1.1 DAL数据访问层

    分布式(Distributed)数据访问层(Data Access Layer),简称DAL,是利用MySQL Proxy.Memcached.集群等技术优点而构建的一个架构系统.主要目的是解决高并发 ...

  2. 数据访问层DAL(数据库访问抽象类DataProvider)

    晒晒数据访问层DAL,看看你的项目数据访问层使用的是什么形式,数据访问性能比较 采用什么样的数据访问形式是软件编码很重要的一个环节,良好的数据访问形式不仅能够提搞代码的执行效率,协作能力,更重要的是对 ...

  3. 【Hades】ades是一个开源库,基于JPA和Spring构建,通过减少开发工作量显著的改进了数据访问层的实现

    几乎每个应用系统都需要通过访问数据来完成工作.要想使用领域设计方法,你就需要为实体类定义和构建资源库来实现领域对象的持久化.目前开发人员经常使用JPA来实现持久化库.JPA让持久化变得非常容易,但是仍 ...

  4. NHibernate:教你如何搭建数据访问层?

    NHibernate:教你如何搭建数据访问层? 什么是NHibernate NHibernate 是一个基于.net 的针对关系型数据库的对象持久化类库.NHibernate 来源于非常优秀的基于Ja ...

  5. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  6. 项目架构开发:数据访问层之UnitOfWork

    接上文 项目架构开发:数据访问层之IQuery 本章我们继续IUnitOfWork的开发,从之前的IRepository接口中就可以看出,我们并没有处理单元事务, 数据CUD每次都是立即执行的,这样有 ...

  7. 数据访问层 (DAO)

    数据持久化 持久化:将程序中的数据在瞬间状态下和持久状态间转换的机制(JDBC) 主要持久化操作:保存.删除.读取.和查找. 采用面向接口编程,可以降低代码间的耦合性,提高代码的可扩展性和可维护性. ...

  8. ClownFish:比手写代码还快的通用数据访问层

    http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...

  9. 使用JDBC构建简单的数据访问层

    本教程的目的是使用Java编写的分离的层去访问数据库中的表,这一层通常称为数据访问层(DAL) 使用DAL的最大好处是通过直接使用一些类似insert()和find()的方法简化了数据库的访问操作,而 ...

随机推荐

  1. &&和||的那点事儿

    以前一直以为&&和||的运算结果就是布尔值,但今天看到一段代码又填补的一些知识漏洞. var a = (1&&2&&5) || 3; console.l ...

  2. 从100PV到1亿级PV网站架构演变

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不 ...

  3. 怎样用VB编写.DLL动态链接库文件

    VB一般可以生成两种特殊的DLL,一个是ActiveX DLL和ActiveX Control(*.ocx).这两种DLL都是VB支持的标准类型,在VB自身的例子中有,你可以参考.更详细的介绍可以参考 ...

  4. 学习练习 java 二分查找法

    package com.hanqi; import java.util.*; public class Test5 { public static void main(String[] args) { ...

  5. CRC校验代码实现

    1.CRC校验简介 CRC就是块数据的计算值,它的全称是“Cyclic Redundancy Check”,中文名是“循环冗余码”.CRC校验是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要 ...

  6. css选择器分类

    css选择器大致可以分为10大类: 1.元素选择器 如:p{} 2.类选择器 如:.xx{} 3.ID选择器 如:#xx{} 4.关联选择器 如:p a{} 5.子元素选择器 如:p>a{} 6 ...

  7. oracle11g空表不能导出记录

    select 'alter table '||table_name||' allocate extent(size 64k);' from tabs t where not exists (selec ...

  8. css定义表格样式

    table.gridtable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width ...

  9. CSS 居中效果完整指南

    本文翻译自:<Centering in CSS: A Complete Guide> 使用 CSS 实现效果困难吗?显然不是.实际上有许多方法可以实现居中效果,但在具体情况中,我们往往无法 ...

  10. 如何解决Android的SDK与ADT不匹配问题

    win7/xp 下面安装Android虚拟机,更新SDK后,在Eclipse preference里指向android-sdk-windows时.出现 :This Android SDK requir ...