学习笔记(1)

1. 从XML中构建SqlSessionFactory

​
String resource = “org/mybatis/example/Configuration.xml”;

Reader reader = Resources.getResourceAsReader(resource);

SqlSessionFactory  sqlMapper = new SqlSessionFactoryBuilder().build(reader);

       mybatis-config.xml

       <?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>  
    <typeAliases>  
        <typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/>  
    </typeAliases>  

<environments default="development">

      <environment id="development">

         <transactionManager type="JDBC"></transactionManager>

         <dataSource type="POOLED">

            <property name="driver" value="${driver}"/>    

            ...  

         </dataSource>

      </environment>

</environments>
    <mappers>  
        <mapper resource="com/manager/data/maps/StudentMapper.xml" />  
    </mappers>  
</configuration>   

​

2.从SqlSessionFactory中获取SqlSession

SqlSession session = sqlMapper.openSession();

session.selectOne(“com.huaxin.mapper.BlogMapper.selectBlog”, 101);



或者

session.getMapper(BlogMapper.class);

Blog blog = mapper.selectBlog(101);

3.范围和生命周期

SqlSessionFatoryBuilder 方法范围   可以用spring beanfactory

SqlSessionFactory      应用范围   

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  <!-- <property name="mapperLocations" value="classpath:sample/config/mappers/**/*.xml" -->

    <property name="configLocation" value="classpath:mybatis-config.xml" />

    <property name="dataSource" ref="dataSource" />

  </bean>

SqlSession 请求范围  Spring依赖注入

4.XML映射配置文件

Configuration配置

   Properties属性

   Settings设置

   typeAliases类型命名

   objectFactory对象工厂

   plugins 插件

      environment环境变量

         transactionManager事务管理器

         dataSource数据源

      映射器

5. 一对一连接查询

1)延迟加载方式

<select>语句不写连接

<resultMap>

       <association column=”blog_author_id” select=”selectAuthor” />

       <!— 复合主键 column=”{prop1=col1, prop2=col2}” à

</resultMap>

2)连接查询方式 

<select>语句写连接

<resultMap>

       <association column=”blog_author_id” resultMap=”authorResult” />

       <!— 复合主键 column=”{prop1=col1, prop2=col2}” à

</resultMap>

6.一对多连接查询

1)s<select>语句不写连接

<resultMap>

       <collection column=”blog_author_id” select=”selectAuthor” ofType=”集合里元素的类型”/>

       </resultMap>

2)连接查询方式 

<select>语句写连接

<resultMap>

       <collection column=”blog_author_id” resultMap=”selectAuthor” ofType=”集合里元素的类型”/>

</resultMap>

注意:对于不是用left join连接的,而用where 全连接的,只需用resultType就行了,关联属性,MYBATIS会自动填补相应的属性。

7.获取自动增长主键

1)<insert>

<selectKey resultType=”int” keyProperty=”id”>

       <!—mysql --> SELECT LAST_INSERT_ID()   

       <!—oracle -->SELECT SEQ_TEST.NEXTVAL FROM DUAL

      <!—mssql -->SELECT @@IDENTITY

</selectKey>

</insert>

Spring整合的 三种方式查询

1)映射类

<bean id=”userMapper” class=”org.mybatis.spring.mapper.MapperFactoryBean”>

<property name=”mapperInterface” value=”org.mybatis.spring.sample.mapper.UserMapper”/>

<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>

</bean>

2)使用SqlSessionTemplate

public Notice get(Integer id) {

   return (Notice) this.sqlSessionTemplate.selectOne("com.huaxin.dao.NoticeDao.get", id);

   }

3UserDaoImpl extends SqlSessionDaoSupport

   继承父类getSqlSession()方法

<bean id=”userMapper” class=”com.huaxin.dao.UserDaoImpl”>

  <property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>

</bean>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trouble-Solver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值