文章目录
Mybatis 中使用的 SQL 解析器有哪些?它们有哪些优点和缺点?
Mybatis 中使用的 SQL 解析器有:
-
StandardSqlParser:标准的 SQL 解析器,适用于大多数数据库,支持大部分 SQL 语法。
-
OracleSqlParser:适用于 Oracle 数据库,可以识别 Oracle 特有的 SQL 语法。
-
DB2SqlParser:适用于 IBM DB2 数据库,可以识别 DB2 特有的 SQL 语法。
-
MySqlSqlParser:适用于 MySQL 数据库,可以识别 MySQL 特有的 SQL 语法。
这些 SQL 解析器的优点在于可以针对不同的数据库类型进行定制化的解析,使 Mybatis 在各种数据库环境下都能够有效地处理 SQL 语句。同时,他们可以识别不同的 SQL 语法,对于使用特定数据库的应用程序来说,这是非常重要的。但是,这些解析器的缺点在于需要额外的配置和维护工作,并且需要对每个解析器的特点有一定的了解才能够正确的使用它们。
10.2 第一个MyBatis示例
10.2.1 创建 Maven项目
1.创建工程 mybatis first _demo
在 idea 中创建Maven Project,如图10-1所示选择 New Project,在“GroupId”文本框中输入“com.mialab”在“ArtifactID”文本输入“mybatis_first_demo”。

图10-1 在idea中创建Maven 项目mybatis_first_demo
2.pom.xml
此 Maven 项目 mybatis_first_demo 中的 pom.xml 内容如下:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
POM用于描述项目如何构建、声明项目依赖等。
10.2.2 准备数据
可以使用 MySOL 客户端工具 Navicat 创建数据库 stu,在数据库 stu 中创建表 student,并插入两条数据,代码如下:
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sno` varchar(100) NOT NULL DEFAULT '',
`name` varchar(100) NOT NULL,
`sex` varchar(8) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`dept_no` varchar(60) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('20171508', '李勇', '男', '20', '2601');
INSERT INTO `student` VALUES ('20171509', '刘娟', '女', '19', '2602');
10.2.3 MyBatis 配置
在 mybatis_first_demo 工程中 src 文件夹的 main 目录下,创建 resources 子文件夹,再在src/main/resources中创建 mybatis-config.xml 文件,如图10-2所示。
<?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">
<!-- XML 配置文件包含对 MyBatis 系统的核心设置 -->
<configuration>
<!-- 指定 MyBatis 所用日志的具体实现 -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<environments default="development">
<!-- 环境配置,即连接的数据库。 -->
<environment id="development">
<!-- 指定事务管理类型,type="JDBC"指直接简单使用了JDBC的提交和回滚设置 -->
<transactionManager type="JDBC" />
<!-- dataSource指数据源配置,POOLED是JDBC连接对象的数据源连接池的实现。 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:33306/stu" />
<property name="username" value="yourname" />
<property name="password" value="yourpassword" />
</dataSource>
</environment>
</environments>
<!-- mappers告诉了MyBatis去哪里找持久化类的映射文件 -->
<mappers>
<mapper resource="mapper/StudentMapper.xml" />
</mappers>
</configuration>
MyBatis配置文件的根元素是 < configuration>,子元素 < settings> 中的 logImpl 属性配置指定使用 Log4j 输出日志,子元素 < environments>用来配置连接的数据库,子元素 < mappers> 中配置了一个包含完整类路径的StudentMapper.xml,这是 MyBatis的SQL 语映射文件。
10.2.4 创建实体类
根据 stu 数据库中的表 student 来创建实体类 Student。具体操作如下:如图10-2所示右击src/main/java,从弹出的快捷菜单中选择“New一Package”选项,输入包名为com.mialab.mybatis_first_demo.domain,在domain 包下创建类 Student.java,代码如下:
package com.mialab.mybatis_first_demo.main;
/**
* ClassName: FirstMain
* Package: com.mialab.mybtis_first_demo.main
* Description:
*
* @Author lyty
*/
import com.mialab.mybatis_first_demo.domain.Student;
import com.mialab.mybatis_first_demo.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class FirstMain {
public static void main(String[] args) {
SqlSession session = null;
try {
// 读取mybatis-config.xml文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 初始化mybatis,创建SqlSessionFactory类的实例
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建Session实例
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.getStudent("20171509");
System.out.println(student);
} catch (Exception e) {
e.printStackTrace();
} finally {
//System.out.println(session);
if(session!=null)
session.close();
}
}
}
10.2.5 创建映射接口和 SQL映射文件
1.创建映射接口
如图10-2 所示,在 src/main/java 中创建 Package“com.mialab.mybatis_first_demo.mapper”,再在mapper包中创建接口StudentMapper.java,具体代码如下:
public interface StudentMapper {
public Student getStudent(String sno);
}
注意:接口的方法要和 XML 映射文件中的 id 保持一致
2.创建SQL映射文件
在包com.mialab.mybatis_first_demo.mapper 下创建 StudentMapper.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.mialab.mybatis_first_demo.mapper.StudentMapper">
<select id="getStudent" resultType="com.mialab.mybatis_first_demo.domain.Student">
select * from student where
sno = #{sno}
</select>
</mapper>
此XML映射文件的根元素是< mapper>,其属性 namespace 值为 StudentMapper 接口的
完整类路径,如图10-2所示。
10.2.6 配置Log4j
如图 10-2 所示,在 src/main/resources 中创建 log4j.properties 文件,其内容如下:
# 全局输出日志
log4j.rootLogger=DEBUG, stdout
# MyBatis 日志配置
com.mialab.mybatis_first_demo.mapper.StudentMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
使用 log4j.properties 置文件的目的是方便查看控制台输出,以易于调试
10.2.7 测试
如图10-2所示,在src/main/java中创建 Package“com.mialab.mybatis_first_demo.main在此包中创建测试类FirstMain.java,其主要代码如下:
public class FirstMain {
public static void main(String[] args) {
SqlSession session = null;
try {
// 读取mybatis-config.xml文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 初始化mybatis,创建SqlSessionFactory类的实例
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 创建Session实例
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.getStudent("20171509");
System.out.println(student);
} catch (Exception e) {
e.printStackTrace();
} finally {
//System.out.println(session);
if(session!=null)
session.close();
}
}
}
如图10-2所示,右击com.mialab.mybatis_first_demo.main 包下的 FirstMain,java,在弹出的快捷菜单中选择“RunAs-JavaApplication”选项,在控制台上可以得到以下结果:
Student{sno='20171509', name='刘娟', sex='女', age=19, dept_no='2602'}

1358

被折叠的 条评论
为什么被折叠?



