什么是Mybatis?
Mybatis的签署了叫iBatis,是apache的一个开源项目,现在在GitHub上
①手写sql进行数据操作
②支持高级映射
③消除了几乎所有的jdbc代码(疯转过的比较彻底)
④xml或者注解进行映射配置
导包
核心配置文件(mybatis-config.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>
<!-- 引入数据库资源文件 -->
<properties resource="db.properties"></properties>
<!-- 为映射添加别名 -->
<typeAliases>
<!-- <typeAlias type="cn.itsource.domain.Product" alias="product"/> -->
<package name="cn.itsource.domain"/>
</typeAliases>
<environments default="mysql_developer">
<!-- 连接环境信息,取一个任意唯一的名字 -->
<environment id="mysql_developer">
<!-- mybatis使用jdbc事务管理方式 -->
<transactionManager type="jdbc"/>
<!-- mybatis使用连接池方式来获取连接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的4个必要属性 -->
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="cn/itsource/mapper/ProductMapper.xml" />
</mappers>
</configuration>
映射文件
<?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="cn.itsource.mapper.ProductMapper">
<!-- 查询一条 -->
<select id="getOne" parameterType="long" resultType="product">
select *
from product where id=#{id}
</select>
<!-- 查询所有 -->
<select id="getAll" resultType="product">
select * from product
</select
<!-- 删除一条 -->
<delete id="deleteOne" parameterType="long">
delete from product where
id=#{id}
</delete>
<!-- 添加一条 -->
<insert id="add" parameterType="product" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into product(
productName,
dir_id,
salePrice,
supplier,
brand,
cutoff,
costPrice
) values(
#{productName},
#{dir_id},
#{salePrice},
#{supplier},
#{brand},
#{cutoff},
#{costPrice}
)
</insert>
<update id="update" parameterType="product">
update product set
productName = #{productName},
dir_id = #{dir_id},
salePrice = #{salePrice},
supplier = #{supplier},
brand = #{brand},
cutoff = #{cutoff},
costPrice = #{costPrice}
where id = #{id}
</update>
</mapper>
namespace 命名空间
写法: domain的完全限定名+Mapper
resultType
返回的每一条数据的类型,类的完全限定名
parameterType
参数的类型,完全限定名
useGeneratedKeys 是否返回生成的主键
keyColumn 主键列的名称 , 可以省略不写
keyProperty domain中主键对应的属性名称
id sql语句的标识(方便后面dao中调用)
<!--
如果domain中的属性和列名不匹配,则不能自动映射,我们需要手动映射,
手动映射类型使用resultMap
id 为你的手动映射设置一个标识
-->
<resultMap type="product" id="productMap">
<!-- 手动映射名称不匹配的列 ,映射普通的属性使用result,映射主键使用id-->
<result column="productName" property="name"/>
</resultMap>
抽取工具包
public class MyBatisUtil{
private static SqlSessionFactory factory;
//为静态的成员变量初始化值
static{
try {
factory = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis-config.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 抽取的获取SqlSession对象的方法
* @return
*/
public static SqlSession openSession(){
return factory.openSession();
}
}
增删改查的写法
public class ProductDaoImpl implements IProductDao {
private final String namespace="cn.itsource.mapper.ProductMapper.";
//查询一条数据
@Override
public Product getOne(Long id) {
SqlSession sqlSession = MyBatisUtil.getSession();
Product one = sqlSession.selectOne(namespace+"getOne",id);
sqlSession.close();
return one;
}
@Override
public List<Product> getAll() {
SqlSession sqlSession = MyBatisUtil.getSession();
List<Product> all = sqlSession.selectList(namespace+"getAll");
sqlSession.close();
return all;
}
@Override
public void delOne(Long id) {
SqlSession sqlSession = MyBatisUtil.getSession();
sqlSession.delete(namespace+"deleteOne",id);
sqlSession.commit();
sqlSession.close();
}
@Override
public void add(Product product) {
SqlSession sqlSession = MyBatisUtil.getSession();
sqlSession.insert(namespace+"add",product);
sqlSession.commit();
sqlSession.close();
}
@Override
public void update(Product product) {
SqlSession sqlSession = MyBatisUtil.getSession();
sqlSession.update(namespace+"update",product);
sqlSession.commit();
sqlSession.close();
}
}