menu 愤鸟杂记 关于 友链 看番列表 漫画观看列表
我从site_blog学到的操作
作者 | 浏览量 1879
请注意,本文编写于 862 天前,最后修改于 862 天前,其中某些信息可能已经过时。

site_blog 项目地址

这个项目是带学某课程中的实践项目,通过阅读其中的部分代码,我学到了一些新东西。

基本操作:MyBatis对数据库的操作

通过使用MyBatis,并进行XML和Mapper类的配置与生成,可以使数据库访问变得简单、方便。

XML和Mapper类对于程序员来说,定义了一组标准的数据库访问操作,使其可以通过方法调用,实现数据库的增删改查及分页等操作。

操作定义

Mapper

Mapper类的主要作用是,定义访问数据库方法的接口,使得数据库操作真正变为方法调用。

比如,Mapper类定义了条件查询的接口:

List<Contents> selectByExample(ContentsExample example);

这里的Example(ContentsExample),是MyBatis生成的,用于定义查询条件的类。

XML

XML文件的主要作用是,将方法调用转化为数据库脚本并执行。

比如,以下代码将条件查询转化为具体的SQL脚本。

  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.yaa.model.vo.ContentsExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from bl_contents
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>

条件查询

条件查询是数据库查询的典型操作。

使用MyBatis进行条件查询的一般步骤是,声明example,即查询条件,并添加所需的查询条件,再调用Mapper对象进行查询。

一个典型的条件查询如下:

public PageInfo<Contents> selectArticlePage(int page,int limit,int uid) {
    ContentsExample example = new ContentsExample();
    //选择条件,降序排序
    example.setOrderByClause("created desc");
    example.createCriteria().andTypeEqualTo(Types.ARTICLE.getType()).andAuthorIdEqualTo(uid);
    //当前页数,一页个数
    PageHelper.startPage(page, limit);
    //条件选择
    List<Contents> contents = contentsMapper.selectByExample(example);
    return new PageInfo<>(contents);
}

以上语句的含义是:

查询content表中,类型为Types.ARTICLE.getType()(返回值为“post”),并且id等于uid的所有行,并按照创建时间降序排列;最后按照每页limit行进行分页,返回第page页。

特别的,如果不添加条件,我们将会得到该表中所有行。于是我们可以以此统计行数,进而得到某些数量。

public int countComment(){
    CommentsExample example = new CommentsExample(); //未添加任何条件
    int count = commentsMapper.countByExample(example);
    return count;
}

秀操作

除了有关MyBatis的一些基本操作,我还知道了一些技术上不难理解,但需要一定想象力的操作。

“万能”日期格式化

表示日期的字符串多种多样,但总有一些约定俗成的表示方式;然而,即使有一些约定俗成的表示方式,程序员也不知道用户(甚至是另一个同行)会用什么表示方式。

但有了“万能”格式化方法,将表示日期的字符串转换成时间戳的事情便不再那么令人恼火。

首先,我们需要一个日期列表:

//设置日期格式列表
private static List<SimpleDateFormat> dateFormats = new ArrayList(12) {
    private static final long serialVersionUID = 2249396579858199535L;
    {
        this.add(new SimpleDateFormat("yyyy-MM-dd"));
        this.add(new SimpleDateFormat("yyyy/MM/dd"));
        this.add(new SimpleDateFormat("yyyy.MM.dd"));
        this.add(new SimpleDateFormat("yyyy-MM-dd HH:24:mm:ss"));
        this.add(new SimpleDateFormat("yyyy/MM/dd HH:24:mm:ss"));
        this.add(new SimpleDateFormat("yyyy.MM.dd HH:24:mm:ss"));
        this.add(new SimpleDateFormat("M/dd/yyyy"));
        this.add(new SimpleDateFormat("dd.M.yyyy"));
        this.add(new SimpleDateFormat("M/dd/yyyy hh:mm:ss a"));
        this.add(new SimpleDateFormat("dd.M.yyyy hh:mm:ss a"));
        this.add(new SimpleDateFormat("dd.MMM.yyyy"));
        this.add(new SimpleDateFormat("dd-MMM-yyyy"));
    }
};

有了日期列表以后,我们就可以通过try...catch逐个尝试列表中的格式化方式,直到得到时间戳。

public static Date convertToDate(String input) {
    Date date = null;
    if(null == input) {
        return null;
    } else {
        //格式化方式列表
        Iterator var2 = dateFormats.iterator();

        //遍历列表尝试转换
        while(var2.hasNext()) {
            SimpleDateFormat format = (SimpleDateFormat)var2.next();

            try {
                format.setLenient(false);
                date = format.parse(input);
            } catch (ParseException var5) {
                ;
            }

            if(date != null) {
                break;
            }
        }

        return date;
    }
}
版权所属愤鸟杂记
本文作者
本文链接https://54df.cc/archives/123/
版权声明本博客文章使用 知识共享署名-相同方式共享 4.0 国际许可协议
添加新评论
您没有输入昵称,昵称不能是空哦~
您的邮箱地址格式不正确哦~
您没有输入网址,网址不能是空哦~
您没有输入评论,评论不能是空哦~