持久层框架,ORM框架,核心对象为sqlsession
核心对象的创建方式,抽取为工具类
package cn.itsource.util;
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.IOException;
import java.io.Reader;
public enum MyBatisUtils {
INSTANCE;
private static SqlSessionFactory sqlSessionFactory;
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("MyBatis-Config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("配置文件解析异常"+e.getMessage());
}
}
//写方法
public SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
在mapper里面配置三个属性
useGeneratedKey 设置这个使用生成键为true
keyColumn 数据库的列id
keyProperty 实体类的属性id
通过日志,把信息打印出来,方便查看错误
导包,写一个log4j.properties配置文件,然后测试就可以看信息了
# 日志输出级别 输出到控制
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
log4j.logger.cn.itsource=TRACE
# 输出到控制台的配置信息
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 输出到控制台的 格式类
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
内置别名和自定义别名
<typeAliases>
<package name="cn.itsource.domain">package>
<package name="cn.itsource.query">package>
typeAliases>
$ 是拼接字符串的方式,类似于statement,sql注入危险
# 是占位符,类似于preparedStatement
平时用#比较多,排序和分页的时候使用$
使用in,在mapper里面使用foreach
<delete id="deleteBatch" parameterType="list">
delete from t_user where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
foreach>
delete>
使用where和if标签 外部sql标签写一次可以多次引用
<sql id="test">
<if test="name!=null">
and name like #{name}
if>
sql>
<select id="queryAllByName" parameterType="userQuery" resultType="User">
select * from t_user
<where>
<include refid="test">include>
where>
select>
<mapper namespace="cn.itsource.ssm.mapper.EmployeeMapper">
<resultMap id="employeeMap" type="employee">
<id property="id" column="id">id>
<result property="name" column="name">result>
<result property="age" column="age">result>
<association property="dept" column="dept_id" javaType="dept" select="selectById">association>
resultMap>
<select id="queryAll" resultMap="employeeMap">
select * from t_employee
select>
<select id="selectById" parameterType="long" resultType="dept">
select * from t_dept where id = #{id}
select>
mapper>
sqlsession级别,1:1:1
mybatis自带一级缓存
sqlsessionfactory级别 1:0:1
在配置文件中设置cache标签即可使用二级缓存
序列化和反序列化
将对象转换成二进制形式,方便传输,保存到磁盘或者硬盘上,实现serializable接口就可以进行序列化
框架的集成第一步基本都是导包,此处省略
然后是配置文件,最后是测试
跟之前SSJ框架不一样的地方在于核心对象从entityManager成为了sqlSession,目录结构也发生了一些变化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6NxxRlcn-1575460214458)(E:\ITSource\homework\JavaEE\1127MyBatis\doc\1.PNG)]
在资源文件夹中有一层专门放mapper.xml的地方
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="cn.itsource.ssm.service">context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"/>
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
cn.itsource.ssm.query
value>
property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>
beans>
applicationContext-mvc.xml和web.xml不再赘述,与之前的SSJ集成基本一致。配置完毕后,mapper层相当于dao层,与数据库打交道。