Mybatis_2

143 阅读3分钟

输入输出映射

输入映射:可以在查询参数中再嵌套pojo,使用#{pojo.属性访问}
resultMap
解决表的列属性和pojo属性不一致的情况。
<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
	<!-- id:设置ResultMap的id -->
	<resultMap type="order" id="orderResultMap">
		<!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
		<!-- property:主键在pojo中的属性名 -->
		<!-- column:主键在数据库中的列名 -->
		<id property="id" column="id" />
		<!-- 定义普通属性 -->
		<result property="userId" column="user_id" />
	</resultMap>

一对一映射
<resultMap type="order" id="orderUserResultMap">
	<id property="id" column="id" />
	<!-- association :配置一对一属性 -->
	<!-- property:order里面的User属性名 -->
	<!-- javaType:属性类型 -->
	<association property="user" javaType="user">
		<!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
		<id property="id" column="user_id" />
		<result property="username" column="username" />
	</association>
</resultMap>

一对多查询
<resultMap type="user" id="userOrderResultMap">
	<id property="id" column="id" />
	<!-- 配置一对多的关系 -->
	<collection property="orders" javaType="list" ofType="order">
		<!-- 配置主键,是关联Order的唯一标识 -->
		<id property="id" column="oid" />
	</collection>
</resultMap>

动态SQL

if标签 和 where 标签
<!-- 根据条件查询用户 -->
<select id="queryUserByWhere" parameterType="user" resultType="user">
	SELECT id, username, birthday, sex, address FROM `user`
<!-- where标签可以自动添加where,同时处理sql语句中第一个and关键字 -->
	<where>
		<if test="sex != null">
			AND sex = #{sex}
		</if>
		<if test="username != null and username != ''">
			AND username LIKE
			'%${username}%'
		</if>
	</where>
</select>
SQL片段
  • 声明:

    <!-- 声明sql片段 -->
    <sql id="userFields">
    	id, username, birthday, sex, address
    </sql>
    
  • 使用

    SELECT <include refid="userFields" /> FROM `user`
    
foreach
<select id="queryUserByIds" parameterType="queryVo" resultType="user">
	SELECT * FROM `user`
	<where>
		<!-- foreach标签,进行遍历 -->
		<!-- collection:遍历的集合,这里是QueryVo的ids属性 -->
		<!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
		<!-- open:在前面添加的sql片段 -->
		<!-- close:在结尾处添加的sql片段 -->
		<!-- separator:指定遍历的元素之间使用的分隔符 -->
		<foreach collection="ids" item="item" open="id IN (" close=")"
			separator=",">
			#{item}
		</foreach>
	</where>
</select>

Mybatis 整合Spring

整合思路
  • SqlSessionFactory交给Spring管理
  • 对于原始Dao开发方式中SqlSession由Spring管理
  • Mapper开发方式中代理对象由Spring管理
  • 数据源以及事务由Spring管理
配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource" />
</bean>
原始Dao开发方式

编写DAO实现类,实现类必须继承SqlSessionDaoSupport

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
	@Override
	public void saveUser(User user) {
		// 获取SqlSession
		SqlSession sqlSession = super.getSqlSession();

		// 使用SqlSession执行操作
		sqlSession.insert("saveUser", user);

		// 不用提交,事务由spring进行管理
		// 不要关闭sqlSession,spring 还需要进行事务的管理。
	}
}

使用时还需要注入SQLSessionFactory

Mapper开发方式
  • 编写Dao接口
  • 编写Mapper
  • 配置MapperScannerConfigurer(org.mybatis.spring.mapper.MapperScannerConfigurer
    • 注入basepackage属性

Mybatis逆向工程

使用官方网站的Mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和Mapper映射文件

每个表被映射成四个文件

  • pojo
  • pojoExample
  • Dao接口
  • Mapper.xml