mybatis知识点汇总
- 1.核心配置文件属性详解
- property:主要用于配置文件的导入,但是和spring的相似,所以此属性用的不多,特别和spring整合之后
- settings: 设置配置值.
例如:
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings- typeAliases: 设置别名
例如:<typeHandlers>
<typeHandler handler="com.ssw.utils.Converter"/>
</typeHandlers>- Mappers:映射配置文件
例如:<mapper resource="org/mybatis/builder/AuthorMapper.xml"/><package name="com.itheima.dao"/>这个配置要求必须配置文件和接口必须同一个名字,而且必须在一个包下
- 2.映射配置文件详解
- 配置文件中默认增删改都是默认不提交的,需要手动提交
- 传参数两个及以上属性名:param1,param2 或者arg0,arg1或者@param()
- 3.${}和#{}的区别
- #{} 会通过预编译的形式设置占位符,在调用的时候传入参数并执行SQL语句
- ${} 会直接把参数拼接进SQL语句,有SQL注入的风险
- ${} 在为表明、字段名等传值的时候,必须使用${}
- 4.selectkey的使用
- 自动返回插入后自动生成的id 例如: <selectKey order="AFTER" resultType="int" keyProperty="id">
- 5.动态sql的使用
- if:判断语句。例如 <if test="value != null and value != '' and value.length>0">
- where:用于去掉第一个参数的and字段 例如:<where>and id=1</where>
- foreach: 遍历循环字段 <where><foreach collection =”list” open=”id in (” close=”)” item=”id” separator=”,”>#{id}</foreach></where> 要求:传过来的参数要是集合list
- collection:要遍历的对象,如果是list集合,这里写list。如果是数组,这里写array。 如果是map集合里面是list或set,这里写对应的key
- open:字符串拼接的开头
- close:字符串拼接的结尾
- separator:链接遍历的每个值分隔符
- item:要遍历的每个值
- set:用于更新的时候使用。会根据条件自动添加关键字。把最后一个条件的,去掉 例如:<set><if test=”name!=null”>name=”33”,</if></set>
- sql: 对sql片段的抽取: 例如:<sql id=”xxx” >select * from xxxxxx</sql> 在另外的就可以引用它 <include refid=”xxx”></include>
- 6.分页插件的使用
- 分页插件的引入 <plugins>
- 坐标的引入 <dependency>
- 分页插件的使用
- 7.一对多和对多配置文件的开发:下面是一对多的懒加载开发。注意:我们使用一对一的时候通常用立即加载,用一对多的时候通常个使用懒加载
select LAST_INSERT_ID()
</selectKey>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种 数据库-->
<property name="dialect" value="mysql"/>
</plugin>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
- PageHelper.startPage(currentPage,pageSize);
Page<CheckGroup> all = checkGroupDao.findAll(queryString);
PageInfo<CheckGroup> pageInfo = new PageInfo<CheckGroup>(all)
<resultMap id="parent" type="com.ssw.pojo.Setmeal">
<id column="id" property="id"/>
<result property="name" column="name"/>
<result column="code" property="code"/>
<result column="helpCode" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
</resultMap>
<resultMap id="setmeal" type="com.ssw.pojo.Setmeal" extends="parent">
<collection property="checkGroups"
ofType="com.ssw.pojo.CheckGroup"
select="com.ssw.dao.CheckGroupDao.findCheckGroup"
column="id"
fetchType="lazy"
></collection>
</resultMap>
</resultMap>
<select id="findById" parameterType="int" resultMap="setmeal">
select * from t_setmeal where id=#{id}
</select>
@Select("select * from t_checkgroup ch,t_setmeal_checkgroup cg where ch.id=cg.checkgroup_id and cg.setmeal_id=#{id}")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "code",column = "code"),
@Result(property = "name",column = "name"),
@Result(property = "helpCode",column = "helpCode"),
@Result(property = "sex",column = "sex"),
@Result(property = "remark",column = "remark"),
@Result(property = "attention",column = "attention"),
@Result(property = "checkItems",
column = "id",
javaType = List.class,//不写也可以
many = @Many(select = "com.ssw.dao.CheckItemMapper.find2",fetchType=FetchType.LAZY)
)
})- web.xml mybatis没有配置
- application.xml配置:
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliases" value="com.ssw.entity"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>dialect=mysql</value>
</property>
</bean>
</array>
</property>
</bean>
注意:除了扫描之外,所有的配置全在sqlsessionfactoryBean中配置
</bean>
<!--批量扫描接口生成代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定接口所在的包-->
<property name="basePackage" value="com.ssw.dao"/>
</bean>
浙公网安备 33010602011771号