内容简介:MyBatis使用笔记
<?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数据库主键值-->
<settingname="useGeneratedKeys"value="true"/>
<!--使用别名替换列名字-->
<settingname="useColumnLabel"value="true"/>
<!--开启驼峰命名转换-->
<settingname="mapUnderscoreToCamelCase"value="true"/>
</settings>
</configuration>
将连接数据库的库信息,写入Spring的xml文件中
<!--配置MybatisSQLSession-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入连接池-->
<propertyname="dataSource"ref="dataSource"/>
<!--注入Mybatis全局配置-->
<propertyname="configLocation"value="classpath:database/mybatis-config.xml"/>
<!--扫描entity包-->
<propertyname="typeAliasesPackage"value="com.zing.account.entity"/>
<!--扫描mapper文件-->
<propertyname="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">
<mappernamespace="com.zing.account.dao.LoginAndSignUpDAO">
<selectid="isEmailAlreadyExists">
SELECT count(0)
FROM cmc_login_oauth
WHERE login_email=#{email};
</select>
</mapper>
吧mapper文件添加到 mybatis-config.xml 的 <configuration> 内,注意路径要正确
<mappers>
<mapperresource="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">
<mappernamespace="com.zing.account.dao.LoginAndSignUpDAO">
<resultMapid="userEntity"type="com.zing.account.entity.UserEntity">
<idcolumn="id"jdbcType="INTEGER"property="id"/>
<resultcolumn="name"jdbcType="VARCHAR"property="userName"/>
...
</resultMap>
<selectid="isEmailAlreadyExists">
SELECT count(0)
FROM cmc_login_oauth
WHERE login_email=#{email};
</select>
</mapper>
6.OGNL表达式
参考下面列表
7.拼接SQL
首先,注意&&和 " 这类符号,要在XML转义
&& :and || :or % :mod " :" < :<
符号转义可以参考: http://www.w3school.com.cn/xml/xml_cdata.asp
更多的转义,可以百度
那么拼接SQL可以像下面一样
<selectid="isEmailAlreadyExists">
SELECT count(0)
FROM cmc_login_oauth
WHERE login_email=#{email}
<iftext="userName!=null and "".equals(userName.trim())">and user_name=#{userName}</if>
</select>
这样满足userName不为空的时候,就会拼接SQL预编译。
注意注入漏洞
以上所述就是小编给大家介绍的《MyBatis使用笔记》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Book of CSS3
Peter Gasston / No Starch Press / 2011-5-13 / USD 34.95
CSS3 is the technology behind most of the eye-catching visuals on the Web today, but the official documentation can be dry and hard to follow. Luckily, The Book of CSS3 distills the heady technical la......一起来看看 《The Book of CSS3》 这本书的介绍吧!