什么是MyBatis?
mybatis是一款优秀的持久层框架,用于简化jdbc开发
持久层:负责将数据保存到数据库的那一层代码
JavaEE三层架构:表现层、业务层、持久层
使用
如果使用Maven来构建项目,则需将下面的依赖代码置于pom.xml中即可
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>版本号</version>
</dependency>
具体使用
1:创建user表,添加数据
2:创建模块,导入坐标
3:编写mybatis核心配置文件,替换连接信息,解决硬编码问题
4:编写sql映射文件,统一管理sql语句,解决硬编码问题
5:编码:定义pojo类、加载核心配置文件获取SqlSessionFactory对象、获取SqlSessionu对象,执行sql语句、释放资源
代码
@Test
public void testSelectAll() throws IOException {
// 1、获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2、获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3、获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
// 4、执行方法
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
// 5、释放资源
sqlSession.close();
}
记录一些增删改查代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="it.gsw.mapper.BrandMapper">
<resultMap id="brandResultMap" type="brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<!--增-->
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand(brand_name, company_name, ordered, description, status)
values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
</insert>
<!--删-->
<delete id="deleteById">
delete
from tb_brand
where id = #{id};
</delete>
<!--批量删除-->
<delete id="deleteByIdS">
delete
from tb_brand
where id in(
<foreach collection="array" item="id" separator=",">
#{id}
</foreach>
)
</delete>
<!--改-->
<update id="update">
update tb_brand
<set>
<if test="companyName!=null and companyName!='' ">
company_name = #{companyName},
</if>
<if test="brandName!=null and brandName!='' ">
brand_name = #{brandName},
</if>
<if test="description!=null and description!='' ">
description = #{description},
</if>
<if test="ordered!=null">
ordered = #{ordered},
</if>
<if test="status!=null">
status = #{status}
</if>
</set>
where id = #{id};
</update>
<!--查取全部数据-->
<select id="selectAll" resultMap="brandResultMap">
select *
from tb_brand;
</select>
<!--查-->
<select id="selectById" resultMap="brandResultMap">
select *
from tb_brand
where id = #{id};
</select>
<!--条件查询,动态sql-->
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status!=null">and 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>
<!--单条件的动态查询-->
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose>
<when test="status!=null">and status = #{status}</when>
<when test="companyName!=null and companyName!='' ">and company_name like #{companyName}</when>
<when test="brandName!=null and brandName!='' ">and brand_name like #{brandName}</when>
</choose>
</where>
</select>
</mapper>