MyBatis

MyBatis使用笔记

2017-02-06  本文已影响438人  MaxZing

1. 编写配置文件

<?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>
    <settings>
        <!--使用JDBC的getGeneratedKeys获取自增Key数据库主键值-->
        <setting name="useGeneratedKeys" value="true"/>
        <!--使用别名替换列名字-->
        <setting name="useColumnLabel" value="true"/>
        <!--开启驼峰命名转换-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

将连接数据库的库信息,写入Spring的xml文件中

<!--配置MybatisSQLSession-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--注入Mybatis全局配置-->
        <property name="configLocation" value="classpath:database/mybatis-config.xml"/>
        <!--扫描entity包-->
        <property name="typeAliasesPackage" value="com.zing.account.entity"/>
        <!--扫描mapper文件-->
        <property name="mapperLocations" value="classpath:database/mapper/*.xml"/>
    </bean>

2.获取SqlSessionFactory

Spring 管理下,用获取bean的方法就能获取SqlSessionFactory

ApplicationContext acontext = new FileSystemXmlApplicationContext("xxxx.xml");
acontext.getBean("sqlSessionFactory");

还有其他方式获取Bean,不一一列举。

不使用Spring管理SQLSession的话,可以按照官方文档,补全Mybatis-config.xml,再通过下面代码获得SqlSession

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

3.编写SQL映射文件,添加到mybatis-config.xml

创建email.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.zing.account.dao.LoginAndSignUpDAO">
    <select id="isEmailAlreadyExists">
        SELECT count(0)
        FROM cmc_login_oauth
        WHERE login_email=#{email};
    </select>
</mapper>

吧mapper文件添加到mybatis-config.xml<configuration>内,注意路径要正确

<mappers>
    <mapper resource="classpash:/mapper/email.xml"/>
</mappers>

4.SqlSession进行数据库访问

SqlSession session = sqlSessionFactory.openSession();
try {
  // 通过SqlSession,进行数据库访问,如查询
  //session.select("nameSpace.SqlID",param)
  session.select("com.zing.account.dao.LoginAndSignUpDAO.isEmailAlreadyExists","1234@zing.com");

} finally {
  session.close();
}

这里注意:参数只能传一个,如果需要传多个参数作为筛选条件,可使用一个实体类封装,或者使用List,Map封装,获取这些参数可以参考后面OGNL的介绍

5.查询结果映射实体类

可以将实体类,配置到Mapper里面,colum表示列名,jdbcType可以参考jdbc包下面的Type类,property表示实体类中的属性名字

<?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.zing.account.dao.LoginAndSignUpDAO">
    <resultMap id="userEntity" type="com.zing.account.entity.UserEntity">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="userName"/>
        ...
    </resultMap>
    <select id="isEmailAlreadyExists">
        SELECT count(0)
        FROM cmc_login_oauth
        WHERE login_email=#{email};
    </select>
</mapper>

6.OGNL表达式

参考下面列表

6-1 6-2

7.拼接SQL

首先,注意&&和"这类符号,要在XML转义

&& :and
|| :or]
% :mod

" :&quot;
<:&lt;

符号转义可以参考:http://www.w3school.com.cn/xml/xml_cdata.asp
更多的转义,可以百度
那么拼接SQL可以像下面一样

    <select id="isEmailAlreadyExists">
        SELECT count(0)
        FROM cmc_login_oauth
        WHERE login_email=#{email}
        <if text="userName!=null and "".equals(userName.trim())">and user_name=#{userName}</if>
    </select>

这样满足userName不为空的时候,就会拼接SQL预编译。

注意注入漏洞

8.参考

MyBatis官方文档:http://www.mybatis.org/mybatis-3/zh/getting-started.html
慕课网:www.imooc.com

上一篇 下一篇

猜你喜欢

热点阅读