MyBatis
- 持久层框架,简化JDBC开发
- 持久层:负责将数据保存到数据库的那层代码
- JavaEE三层架构:表现层、业务层、持久层
- 框架:半成品软件,一套可重用的、通用的软件基础代码模型。在框架的基础上构建软件编写更加高效、规范、通用、可扩展。
- Mybatis几乎免除了所有JDBC代码,以及设置参数和获取结果集的工作
一、MyBatis快速入门
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.InputStream;
import java.util.List;
public class MyBatisDemo {
/**
* MyBatis 快速入门
*/
public static void main(String[] args) throws Exception {
//1.加载mybatis的核心配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession对象,用它执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.执行sql
List<User> users = sqlSession.selectList("test.selectAll");
System.out.println(users);
sqlSession.close();
}
}
二、MyBatis代理开发
- 解决原生方式中的硬编码
- 简化后期执行SQL
- 要求
(1)定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
(2)设置SQL映射文件的namespace属性为Mapper接口全限定名
(3)在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
(4)编码:
a. 通过SqlSession的getMapper方法获取Mapper接口的代理对象
b. 调用对应方法完成sql的执行
//3.执行sql
//List<User> users = sqlSession.selectList("test.selectAll");
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.selectAll();
三、MyBatis核心配置文件
- enviroment:配置数据库连接环境信息,可以配置多个environment,通过default属性切换不同的environment
- 类型别名(typeAliases)
配置各个标签时,需要遵循前后顺序
四、配置文件完成增删改查
(1)查询所有
- 编写接口方法:Mapper接口
- 编写SQL语句
- 执行方法、测试
- 数据库表的字段名称和实体类的属性名称不一样,不能自动封装数据
1.起别名:对不一样的列名起别名,让别名和实体的属性名一致。缺点:不方便
2.sql片段:缺点:不灵活
3.resultMap
a. 定义标签
b. 在标签中,使用resultMap属性替换 resultType属性
<resultMap id="brandResultMap" type="Brand">
<result column="brand_name" property="brandName"></result>
<result column="company_Name" property="companyName"></result>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select * from tb_brand;
</select>
(2)查看详情
- 参数占位符
1.#{}
:会替换为?为了防止SQL注入
2.${}
:拼sql,存在sql注入问题
3.使用时机:
参数传递的时候:#{}
表名或列名不固定的情况下:${}
<select id="selectById" resultMap="brandResultMap">
select * from tb_brand where id = #{id};
</select>
-
parameterType:
用于设置参数类型,可以省略 -
特殊字符的处理:
1.转义字符
2.<![CDATA[内容]]>:CD提示
(3)条件查询
1.散装参数
2.对象参数
3.Map集合
List<Brand> selectByCondition(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);
// List<Brand> selectByCondition(Brand brand);
// List<Brand> selectByCondition(Map map);
(4)动态条件查询
1、多条件动态查询
<!--动态条件查询-->
<select id = "selectByCondition" resultMap="brandResultMap">
select * from tb_brand
<!--where 1=1-->
<where>
<if test="status != null">
status = #{status}
</if>
<if test = "companyName !=null and companyName !=''">
and company_name like #{companyName}
</if>
<if test = "brandName !=null and brandName !=''">
and brand_name like #{brandName}
</if>
</where>
</select>
2、单条件动态查询
<select id = "selectByConditionSingle" resultMap="brandResultMap">
select * from tb_brand
where
<choose><!--相当于switch -->
<when test="status != null">
status = #{status}
</when>
<when test = "companyName !=null and companyName !=''">
company_name like #{companyName}
</when>
<when test = "brandName !=null and brandName !=''">
brand_name like #{brandName}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</select>
五、注解完成增删改查
- 查询 @Select
- 添加 @Insert
- 修改 @Update
- 修改 @Delete
- 注册完成简单功能,配置文件完成复杂功能
@Select("select * from tb_user where id = #{id}")
User selectById(int id);