一、MyBatis的简单使用
1、maven项目依赖
简单的使用Mybatis只需要引入依赖包便可以了。
1 | <dependency> |
2、需要配置的文件
1、mybatis的配置文件
2、数据库的连接配置文件
3、数据库和Do的实体映射文件
mybatis配置文件 configuration.xml名字自定。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<configuration>
<!-- 加载类路径下的属性文件 -->
<properties resource="jdbc.properties"/>
<!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_developer">
<!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--将mybatis配置和映射文件关联起来-->
<mappers>
<mapper resource="DictionaryMapper.xml"/>
</mappers>
</configuration>连接配置文件jdbc.properties
1
2
3
4driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/trainee?useSSL=false
name=root
password=root相关的配置说明:
<properties>
resource 加载数据库的相关配置。配置文件和简单的jdbc连接的配置一样。如何将数据库连接配置文件加载入mybatis配置文件。
<dataSource type="pooled">
使用连接池的方式。连接配置参数是如何将属性加载入mybatis的配置文件。
dataSource的属性。<property>
中的name属性是固定的。需要的是从外部加载进相应的value中。例如<property name="driver" value="${driverClass}"/>
driverClass便是从连接配置文件加载值的。当然也可以手工给他赋值实体映射文件。该文件是将数据库中的表和java中的pojo类进行映射和配置相关的操作的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!--
namespace属性是名称空间,必须唯一
可以自定义
-->
<mapper namespace="DictionarySpace">
<resultMap id="dictionaryMap" type="com.bosssoft.hr.train.chp2.pojo.entity.Dictionary">
<id property="categoryId" column="category_id"/>
<result property="name" column="name" />
<result property="category" column="category" />
<result property="value" column="value" />
<result property="remark" column="remark"/>
<result property="status" column="status"/>
<result property="orgId" column="org_id"/>
<result property="createdBy" column="created_by"/>
<result property="createdTime" column="created_time"/>
<result property="updatedBy" column="updated_by"/>
<result property="updatedTime" column="updated_time"/>
<result property="version" column="version"/>
</resultMap>
<!--增-->
<insert id="insertDictionary" parameterType="com.bosssoft.hr.train.chp2.pojo.entity.Dictionary">
INSERT INTO t_dictionary (category_id,name,category,value,remark,status,org_id,
created_by,created_time,updated_by,updated_time,version) values
(#{categoryId},#{name},#{category},#{value},#{remark},#{status},#{orgId},
#{createdBy},#{createdTime},#{updatedBy},#{updatedTime},#{version});
</insert>
<!--删-->
<delete id="deleteDictionary" parameterType="long">
DELETE FROM t_dictionary WHERE category_id = #{id};
</delete>
<!--查-->
<select id="queryDictionary" parameterType="long" resultMap="dictionaryMap">
SELECT * FROM t_dictionary WHERE category_id = #{id};
</select>
<!--改-->
<update id="updateDictionary" parameterType="com.bosssoft.hr.train.chp2.pojo.entity.Dictionary">
UPDATE t_dictionary SET name=#{name},category=#{category},value=#{value},remark=#{remark},status=#{status},org_id=#{orgId},
created_by=#{createdBy},created_time=#{createdTime},updated_by=#{updatedBy},updated_time=#{updatedTime},version=#{version}
WHERE category_id = #{categoryId};
</update>
</mapper>相关属性说明:
mapper 中的
namespace
命名空间。
作用是让mybatis的sqlsession找到该空间下的sql语句来执行。sqlSession.insert("DictionarySpace.insertDictionary",dictionary);
resultMap
这是最重要的配置项。该项连接了数据库的表项和java的实体类。
(1)resultMap#id 标识唯一的值。该值可以被引用。
(2)resultMap#type 对应的java类。
(3)<id>
key值。其中的property
是对应的POJO类的属性。column
是对应的数据表中的字段值。sql语句
相关属性说明
(1)id
该sql语句的唯一标识符。和命名空间一起被唯一的调用。
(2)parameterType
参数的类型。这个是入口参数类型。可以是基本的数据类型,也可以是自己的类。
(3)resultMap
返回的数据对象。和前面的resultMap相对应。这样在代码中便可以获得该返回对象了。
将该配置文件和Mybatis的配置文件关联起来需要在configuration.xml中引入该mapper.
1 | <!--将mybatis配置和映射文件关联起来--> |
1对多返回的部分数据中存储为list
使用如下的格式返回list数据
1 | <collection property="roles" ofType="java.lang.String" javaType="java.util.List"> |
3、Mybatis加载过程。
基本流程如下:
1、读取configuration.xml -> 加载连接配置jdbc.properties -> 获得连接对象SqlSession
1 | static { |
返回连接对象
1 | /** |
2、使用SqlSession对象来调用sql语句,configuration.xml 知道mapper位置 -> mapper命名空间的和sql的id -> 加载语句执行
简单的插入语句的操作
1 | /** |
4、完整的源代码
1、configration.xml
1 |
|
2、DictionaryMapper.xml
1 |
|
3、MybatisUtil.java
1 | package com.bosssoft.hr.train.chp2.util; |
4、Dao 的接口类 IDictionaryDao.java
1 | package com.bosssoft.hr.train.chp2.dao; |
5、实现类 DictionaryDaoImpl.java
1 | package com.bosssoft.hr.train.chp2.dao.impl; |
6、实体类 Dictionary.java
1 | package com.bosssoft.hr.train.chp2.pojo.entity; |
7、测试类
1 |
|
规范化的测试。使用断言,不用捕获异常。
1 | package com.bosssoft.hr.train.chp2.dao.impl; |
二、SpringBoot 整合Mybatis
1、application.properties配置mybatis
1 | com.mysql.cj.jdbc.Driver = |
2、使用接口来定义一个Dao层
相比于传统的xml的配置文件来映射数据库实体文件。SpirngBoot只需要注解便可以实现java到实体的映射
2.1 常用注解
@Insert()
插入语句的注解
例子:1
2
3
4
5
int insertDictionary(Dictionary dictionary);注意
#{}
使用预编译的字符填充。填充的字符和dictionary
中的成员一致。@Select()
查询语句,需要指定返回值类型1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
List<Dictionary> queryAll();@Results
是返回值的类型。如果没有定义过的话就要自己定义一个。id是该result的标识符。property是java实体的成员。column是数据库表中的列。使用上面定义好的Results
1
2
3
List<Dictionary> queryByNameTypeStatus(String name,String category, byte status);注解中的
@ResultMap(id)
使用已定义的Results id =dictionaryMap
更新的注解
@Update()
1
2
3
4
int updateDictionary(Dictionary dictionary);删除的注解
@Delete()
1
2
int deleteDictionary(long id);2.2 启动类中扫描相关的组件
@MapperScan("com.bosssoft.hr.train.chp2.dao")
扫描为dao3、使用Dao来进行访问
1
2
3
4
5/**
* dao对象
*/
private DictionaryDao dictionaryDao;