Mybatis上手简单、容易掌握,使用简单的 XML或注解用于配置和原始映射可以简便JDBC的操作,实现数据的持久化,并通过建立类对象与数据库表对应关系,更加方便操作数据库表,实现增删改查等操作。
基本环境配置
一、首先要先导入两个核心jar包:
- Mybatis的jar包:mybatis-x.x.x jar
- JDBC的jar包连接数据库:jdbc.jar
二、建立表-类一一对应关系和映射文件mapper.xml
- 数据库表的属性与类中的属性一一对应。
- mapper.xml文件中, < mapper> < /mapper>之间添加存放若干个sql语句,用于对数据库表的操作。
三、配置conf.xml(数据库配置信息、映射文件)
- db.properties
用于存放用户名、密码等信息,方便修改,类似于key-value对应关系,然后通过 标签< properties resource=“db.properties”/> 进行引入,然后使用 ${key} 进行应用driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mbts username=root password=root
- conf.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>
<!-- 引入db.properties -->
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置数据库连接 -->
<!-- jdbc驱动 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<!-- 用户名以及密码 -->
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="org/entity/mapper/PersonMapper.xml"/>
</mappers>
</configuration>
在conf.xml文件中,< environments default = ID>< /environments>之间可以有多个< environment id=ID>(数据库配置信息)< /environment>,然后通过default中的ID与配置当中的id为ID的相匹配,从而调用相应的配置。
四、测试实现
- 加载MyBatis的配置文件
//加载MyBatista配置文件(为访问数据库)
Reader reader = Resources.getResourceAsReader("conf.xml");
- 建立会话工厂
//建立会话工厂sessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
- 建立会话
//建立会话
SqlSession session = sessionFactory.openSession();
- 测试实现
通过mapper.xml全类名加上< mapper>< /mapper>中sql语句id名称,实现sql语句定位,从而可以调用相应语句操作数据库表。
//sql语句
String statement = "org.entity.PersonMapper."+"queryPersonById";
//查询操作
Person person = session.selectOne(statement,1);
System.out.println(person);
//关闭会话
session.close();
备注:
- 如果实现了接口式编程,就可以通过调用接口的方法直接操作,简略了sql语句。
- 也可以在建立会话工厂中设置为SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader,ID);通过build(reader,ID)中的参数ID指定数据库环境,此时忽略conf.xml文件中default的值,但不建议使用。
单个别名设置与批量别名设置
- 单个别名设置
<!-- 单个别名 -->
<typeAliases>
<typeAlias type="org.entity.Person" alias="Person"/>
<typeAlias type="org.entity.PersonId" alias="PersonId"/>
<typeAlias type="org.entity.PersonBusiness" alias="PersonBusiness"/>
</typeAliases>
- 批量别名设置
<!-- 批量别名 -->
<typeAliases>
<package name="org.entity"/>
</typeAliases>
注:无论是单个别名还是批量别名,均是在conf.xml文件编辑在< typeAliases>< /typeAliases>之中,而批量别名省略了package包名,使用别名会更加简便。
如经常用到的输入参数parameterType与返回参数resultType为对象类型时:
可以由:
<select id="queryPersonById" resultType="org.entity.Person" parameterType="org.entity.Person">
变成:
<select id="queryPersonById" resultType="Person" parameterType="Person">
输出参数与输入参数
mybatis约定:输入参数parameterType 和 输出参数resultType,在形式上都只能有一个,但可以是数组或对象类型等。
输出参数:resultType
- 输出参数:如果返回值类型是一个对象,则无论返回一个、还是多个
此时resultType=“全类名”。 - 当数据库表的属性不一致时,如Person类中性别属性为perSex而数据表person中性别属性名字为sex时,可以使用resultMap将它们的关系一 一进行对应起来,根据ID进行调用
<!-- resultMap=对应的ID值 -->
<select id="queryPersonByIdWithConverter" parameterType="int" resultMap="person12">
select * from person where id = #{Id}
</select>
<!-- property="类属性" column="数据表属性" 实现对应映射 -->
<resultMap type="Person" id="person12">
<id property = "Id" column="id"/>
<result property = "Name" column="name"/>
<result property = "Age" column="age"/>
<!-- 将不同名字对应起来 -->
<result property = "perSex" column="sex"/>
</resultMap>
输入参数:parameterType
一、类型为 简单类型(8个基本类型+String)
#{}、${}的区别
a.
#{任意值},但建议规范一些
${value} ,其中的标识符只能是value
b.
#{}自动给String类型加上’ ’ (自动类型转换)
${} 原样输出,但是适合于 动态排序(动态字段)
c.
#{}可以防止SQL注入
${}不防止注入
${}、#{}相同之处:
a.
都可以 获取对象的值 (嵌套类型对象)
一般调用对比:
//#{}的使用
select id,name,age from person where name = #{Name}
//${}的使用
select id,name,age from person where name = #{value}
动态排序:
//根据输入属性(如:id、name、age)升序输出
select id,name,age from person order by ${value} asc
模糊查询两种方式对比:
(由于用惯接口式编程,以下两种方法都是接口方法调用,而并非session定位sql语句)
- 方法一
//sql语句
select id,name,age from person where age= #{Age} or name like #{Name}
//执行方法
Person person= new Person();
person.setAge(24);
person.setName("%w%");
List<Person> Persons = personMapper.queryPersonByAgeOrName(person) ;//接口的方法->SQL
- 方法二
person.setName("w");
select id,name,age from person where age= #{Age} or name like '%${Name}%'
二、输入类型为对象类型
#{属性名}
${属性名}
注:本篇文章是在Mybatis学习过程中总结出来的,用于以后对于Mybatis知识点的复习回顾,如有错漏之处敬请指出,不胜感激!