文章目录
配置详解
1、全局配置文件的介绍(mybatis-config.xml)
参考文档:MyBatis配置
(1)properties:读取外部资源的
properties用来给定配置,可以进行动态的替换
<!--读取配置文件信息:动态的替换配置-->
<properties >
<property name="user" value="root"/>
<property name="pasword" value="123456"/>
</properties>
<!--配置数据源:必须-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="${user}"/>
<property name="password" value="${pasword}"/>
</dataSource>
</environment>
</environments>
使用的配置在properties中给定例如:username和password,在指定的配置文件中db.propertis也给定了配置信息,即属性不只一个地方进行配置:mybatis提供如下加载顺序:
- 首先读取的是properties元素体内指定的属性
- 再根据properties元素中的resource属性读取指定路径下的问题,如果存在相容的属性文件,则覆盖之前读取的同名的属性
- 最后读取防范参数传递的属性,覆盖之前读取的同名属性
注意:通过parameterType传递的属性具有最高优先级,resource或URL加载的属性次之,最低优先级的是properties元素体内指定的属性
(2)setting:全局的参数配置
cacheEnabled
:mybatis中提供的缓存机制,其中二级缓存的使用需要打开开关lazyLoadingEnabled
和aggressiveLazyLoading
配置用来进行懒加载的开关配置
<!--全局的参数配置-->
<settings>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
(3)typeAliases:类型别名
类型别名是java的类型设置一个短的名字,只和XML配置有关,用来减少类完全限定名的冗余的问题
在mapper.xml
配置文件中,定义了很多的statement
,stetement
需要使用ParameterType
指定输入参数类型,需要使用resultType
来指定输出参数类型,指定类型的全路径,不方便开发,就可以在配置文件中配置别名,在mapper.xml
文件中就可以使用定义别名
<!--类型别名-->
<typeAliases>
<!--定义单个别名 type属性:pojo类的全限定名 alias:别名的名称-->
<typeAlias type="com.tulun.pojo.Student23" alias="student23"/>
<!--批量的别名配置 name:指定包名,将包下的所有的pojo类定义别名,别名为类名(首字母大写小写都可以)-->
<package name="com.tulun.pojo"/>
</typeAliases>
(4)environments:环境配置
mybatis支持多种环境的配置,方法在多个数据库键做切换,例如:开发环境,测试环境,生产环境
- 可以配置多个环境,但是在使用的使用SQLSessionFactory实例只能选择一种环境
- 使用的默认环境:default=“development”
- 每一个环境environment元素定义的环境ID
- 事务管理器的配置:transactionManager
- 数据源的配置:dataSource
(5)mappers:映射器
定义的SQL映射语句,告诉mybatis去哪查找SQL的语句,主要提供三种方式加载映射文件
<!--引入映射文件:在和spring整合后不需要在这里配置了-->
<mappers>
<!--resource方式:单个mapper的映射:定义的mapper.xml文件中namespace为maper接口的地址,
映射文件通过namespace找到对应的mapper的接口文件
mapper.xml文件和mapper的接口文件可以不放在一个目录下,且mapper.xml和mapper的及接口文件名可以不一致
-->
<mapper resource="mapper/Student23Mapper.xml"/>
<!--class方式:单个mapper的映射:遵循规则:将mapper.xml文件和mapper接口文件放在一个目录下,并且文件名保持一致-->
<mapper class="com.tulun.inteface.Student23Mapper"/>
<!--批量的mapper扫描:package方式:批量mapper映射:遵循规则:mapper.xml文件和mapper接口文件在同一目录下,且文件名相同-->
<package name="com.tulun.inteface"/>
</mappers>
2、配置XML文件的基本用法
主要关注 增、删、改、查
(1)select
<!--
select查询操作标签
id属性:对应接口中的方法名,必填项,且在同一个namespace下,id不能重复
#{XXX}:占位符,里面添加的是方法中的参数名
parameterType属性:指定入参类型
resultType:执行返回参数类型,指定全路径名
resultMap属性:指定返回参数类型
-->
<select id="selectStudentByUid" parameterType="long" resultType="com.tulun.pojo.Student23">
select * from Student where SID = #{uid}
</select>
(2)insert
<insert id="insertStudent" parameterType="com.tulun.pojo.Student23">
insert into Student(SID,Sname,Ssex,Sage) values (#{SID},#{Sname},#{Ssex},#{Sage})
</insert>
(3)update
<update id="updateStudent" parameterType="com.tulun.pojo.Student23">
update Student
set
Sname = #{Sname},Sage = #{Sage}
where SID = #{SID}
</update>
(4)delete
<delete id="deleteStudent">
delete from Student where id = #{uid}
</delete>
resultType 和 resultMap 的区别:
- 使用 resultType 是需要数据库字段和Java类的类型名保持一致,如果不一致则相应属性无法完成映射。
- 使用 resultMap 进行显性映射,可以解决数据库字段和Java类属性名不一致的问题。
<resultMap id="Student" type="com.tulun.pojo.Student23">
<!-- id:设置ResultMap的id -->
<!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
<!-- property:主键在pojo中的属性名 -->
<!-- column:主键在数据库中的列名 -->
<id property="SID" column="SID"/>
<!-- 映射其他普通列 -->
<result property="Sname" column="Sname"/>
<result property="Ssex" column="Ssex"/>
<result property="Sage" column="Sage"/>
</resultMap>
3、注解基本用法
MyBatis框架的注解方式就是将SQL语句直接写在接口上。这种方式的优点是,对于需求比较简单的系统,效率较高。缺点是,当SQL有变化时需要重新编译代码,一般情况下不建议使用注解方式。在MyBatis注解SQL中,最基本的就是@Select
、@Insert
、@Update
和@Delete
四种。
@Select
操作:
@Select("select * from Student where SID = #{uid}")
public Student23 selectStudentByUid(Long uid);
@Insert
操作:
@Insert("insert into Student(SID,Sname,Ssex,Sage) values (#{SID},#{name},#{Ssex},#{Sage})")
public int insertStudent(Student23 student);
@Update
操作:
@Update("update Student set Sage = #{Sage} where SID = #{SID}")
public int updateStudent(@Param("SID") Integer SID, @Param("Sage") Integer Sage);
@Delete
操作:
@Delete("delete from Student where SID = #{SID}")
public int deleteStudent(int SID);
- 注意:在注解形式也提供了类似xml方式提供的功能,包括自增注解,缓存开启等功能,需要将这些配置写在
@Options
注解内,如下:
@Options(useGeneratedKeys = true,timeout = 1000,useCache = true)
@Insert("insert into Student(SID,Sname,Ssex,Sage) values (#{SID},#{name},#{Ssex},#{Sage})")
public int insertStudent(Student23 student);
@Results({
@Result(property ="SID" ,column = "SID"),
@Result(property ="name" ,column = "Sname"),
@Result(property ="Sage" ,column = "Sage"),
@Result(property ="Ssex" ,column = "Ssex"),
})
-
以上写法类似于xml方式中通过resultMap返回结果集
@Results
类似于xml中resultMap
标签,@Result
类似于result
属性 -
@Result(property ="SID" ,column = "SID",id = true)
,和xml配置文件中的<id property="SID" column="SID" />
标注主键的映射关系 -
在mybatis3.3.0之前是没有id属性,
@Results
是不能共享的,
在mybatis3.3.1 之后才支持id属性,方式如@Result(property ="SID" ,column = "SID",id = true)
,支持@Results
可以共用
4、编码详解
public static void main(String[] args) throws IOException {
//获取全局配置文件
String path = "mybatis-config.xml";
//通过mybatis提供共的Resources类来获取配置文件流
InputStream inputStream = Resources.getResourceAsStream(path);
//获取SQLSessionFactory对象
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取会话工厂
SqlSession sqlSession = sessionFactory.openSession();
//通过反射机制来后去到mapper的实例
Student23Mapper student23Mapper = sqlSession.getMapper(Student23Mapper.class);
Student23 student23 = student23Mapper.selectStudentByUid(1L);
System.out.println(student23);
sqlSession.close();
}
}
-
SqlSessionFactoryBuilder
创建会话工厂实例SqlSessionFactory(会话工厂) -
SqlSessionFactory
:会话工厂
通过读取配置文件,配置文件一次读取就可以实例化所有需要的会话,SqlSessionFactory设置为单例,通过SqlSessionFactory创建SqlSession(会话) -
SqlSession
:会话
对数据库的CRUD操作都是通过SqlSession来操作的
SqlSession是线程不安全的,将其设置为局部变量