MyBatis的基本架构
MyBatis由三个主要组件组成:SqlSessionFactory,SqlSession和Mapper。SqlSessionFactory是创建SqlSession的工厂,SqlSession是与数据库交互的主要接口,Mapper是将Java方法调用转换为SQL语句的接口。
1. SqlSessionFactory:
SqlSessionFactory是MyBatis初始化的入口,是创建SqlSession的工厂。它通过加载mybatis-config.xml配置文件和Mapper映射文件来初始化MyBatis运行环境,并提供了创建SqlSession对象的方法。SqlSessionFactory的主要作用是管理数据库连接池和缓存。
以下是一个SqlSessionFactory的示例代码:
String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
2. SqlSession:
SqlSession是与数据库交互的主要接口,它提供了多种操作数据库的方法,如selectOne、selectList、insert、update、delete等。SqlSession的生命周期是比较短暂的,每个请求都应该使用一个新的SqlSession对象。
以下是一个SqlSession的示例代码:
SqlSession sqlSession = sqlSessionFactory.openSession(); try { User user = sqlSession.selectOne("com.example.mapper.UserMapper.selectUserById", 1); System.out.println(user); } finally { sqlSession.close(); }
3. Mapper:
Mapper是将Java方法调用转换为SQL语句的接口。它提供了一组方法,可以将Java方法映射成对应的SQL语句,并且通过SqlSession执行SQL语句。Mapper通常定义在一个接口中,并且需要编写对应的Mapper映射文件。
以下是一个Mapper的示例代码:
public interface UserMapper { User selectUserById(Integer id); }
对应的Mapper映射文件userMapper.xml:
<mapper namespace="com.example.mapper.UserMapper"> <select id="selectUserById" parameterType="java.lang.Integer" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper>
MyBatis的配置文件
MyBatis使用XML格式的配置文件来配置SqlSessionFactory和Mapper。配置文件包含数据库连接信息、事务管理器、映射文件位置等信息。
MyBatis的配置文件是一个XML格式的文件,它用于配置SqlSessionFactory和Mapper等组件。下面是对MyBatis配置文件中常见概念的详细解释。
1. configuration元素:
configuration元素是所有其他元素的根元素。它包含了所有需要装配的组件的信息。
2. properties元素:
properties元素用来定义属性。可以在属性文件、环境变量或JVM系统属性中配置属性值,并通过${}语法引用它们。
例如:
<properties resource="jdbc.properties"> <property name="username" value="root"/> <property name="password" value="${my.password}"/> </properties>
3. typeAliases元素:
typeAliases元素用来设置Java类型和数据库列类型之间的映射关系。可以通过别名来减少XML配置文件中的重复代码。
例如:
<typeAliases> <typeAlias alias="int" type="java.lang.Integer"/> <typeAlias alias="user" type="com.example.entity.User"/> </typeAliases>
4. environments元素:
environments元素用来定义不同的数据库环境。每个环境都包含一个数据源和事务管理器。
例如:
<environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments>
5. mappers元素:
mappers元素用来定义Mapper映射文件位置。可以使用相对路径或classpath路径来指定位置。
例如:
<mappers> <mapper resource="com/example/mapper/UserMapper.xml"/> <mapper class="com.example.mapper.OrderMapper"/> </mappers>
以上就是MyBatis配置文件中常见概念的详细解释。通过配置文件,我们可以配置数据库连接信息、事务管理器、映射文件位置等信息,从而初始化SqlSessionFactory并创建SqlSession对象。
MyBatis的动态SQL
MyBatis支持动态SQL,可以根据需要构建不同的SQL语句。动态SQL包括if、choose、where、foreach等标签。
MyBatis支持动态SQL,可以根据需要构建不同的SQL语句。以下是MyBatis中常用的动态SQL标签及其作用:
1. if元素:
if元素用于条件判断,类似于Java中的if语句。它可以嵌套在其他标签之内,用于构建复杂的SQL语句。
例如:
<select id="findUser" resultType="User"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select>
2. choose、when、otherwise元素:
choose元素相当于Java中的switch语句,它包含多个when和一个otherwise元素。当满足某个条件时,执行对应的when元素中的SQL语句;否则,执行otherwise元素中的SQL语句。
例如:
<select id="findUser" resultType="User"> SELECT * FROM user <where> <choose> <when test="name != null and name != ''"> AND name = #{name} </when> <when test="age != null"> AND age = #{age} </when> <otherwise> AND sex = #{sex} </otherwise> </choose> </where> </select>
3. where元素:
where元素用于拼接WHERE子句,它会自动去掉第一个AND或OR关键字。如果条件都没有满足,则WHERE子句被省略。
例如:
<select id="findUser" resultType="User"> SELECT * FROM user <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> </where> </select>
4. foreach元素:
foreach元素用于循环遍历集合,生成对应的SQL语句。它可以循环遍历数组、List、Set、Map等类型的集合。
例如:
<select id="getUsersByIds" resultType="User"> SELECT * FROM user WHERE id IN <foreach collection="ids" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
以上就是MyBatis中常用的动态SQL标签及其作用。通过使用动态SQL标签,我们可以根据不同的条件构建不同的SQL语句,使得SQL语句更加灵活和可重用。