一、MyBatis概述
MyBatis是一个ORM(对象,关系,映射)的数据库持久化框架,MyBatis底层还是原生的JDBC代码,对JDBC代码的封装
二、MyBatis相较于JDBC有哪些优势
- 把sql语句从java代码中抽取出来,方便维护,并且修改sql时不用修改java代码
- 不用手动设置参数和对结果集的处理,让我们操作数据库更加简单
- 与JDBC相比,大大减少了代码量,提高了开发效率
三、MyBatis全局配置文件
3.1 属性配置文件(db.properties)
在工程中对数据源信息写在db.properties文件中,可以通过properties标签来加载该文件
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis
username = root
password = 1234
3.2 Mybatis核心配置文件(Mybatis-Config.xml)
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 通过properties标签,读取数据库配置文件的内容 -->
<properties resource="DB.properties"/>
<!-- 配置mybatis的环境信息 -->
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC" />
<!-- 配置数据源,采用dbcp连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
</configuration>
四、MyBatis入门
4.1导入MyBatis相应的jar包
网上有很多资源找到相应的即可
4.2.1建立对应的实体表
4.2.2创建对应的实体类
public class Product {
private Long id;
//产品名
private String productName;
//产品类型id
private Long dir_id;
//零售价
private Double salePrice;
//供应商
private String supplier;
//品牌
private String brand;
//折扣
private Double cutoff;
//成本价
private Double costPrice;
}
4.3创建Mapper接口和Mapper映射文件
接口ProductMapper,接口不要写实现类
public interface ProductMapper {
/**增*/
void save(Product product);
/**删*/
void delete(Long id);
/**改*/
void update(Product product);
/**查询一条信息*/
Product selectOne(Long id);
/**查询所有信息*/
List<Product> selectAll();
}
映射文件名和接口一样ProductMapper
<!-- 命名空间:namespace直接写UserMapper接口的的完全限定名 -->
<mapper namespace="cn.itsource.domain.ProductMapper">
<!-- 查询一条 -->
<select id="selectOne" parameterType="long" resultType="cn.itsource.domain.Product">
select * from product where id = #{id}
</select>
<!-- 查询所有 -->
<select id="selectAll" resultType="cn.itsource.domain.Product">
select * from product
</select>
<!-- 添加一条 -->
<insert id="save" parameterType="cn.itsource.domain.Product">
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="cn.itsource.domain.Product">
update product set
productName = #{productName},dir_id = #{dir_id},salePrice = #{salePrice},
supplier = #{supplier},brand = #{brand},cutoff = #{cutoff},costPrice = #{costPrice}
where id = #{id}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="long">
delete from product where id = #{id}
</delete>
</mapper>
4.4工具类
public enum MyBatisUtil {
INSTRUMENT;
private static SqlSessionFactory factory = null;
static{
try {
//读取配置文件,mybatis提供了工具类
Reader reader = Resources.getResourceAsReader("Mybatis-Config.xml");
//获取SqlSessionFactory对象,SqlSessionFactory()(工厂模式)是接口,通过SqlSessionFactoryBuilder()(创建者模式)创
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public SqlSession openSession() {
return factory.openSession();
}
}
4.5测试
这里展示了添加数据和修改数据
public class ProductMapperTest {
@Test
public void testSave() {
SqlSession session = null;
try {
session = MyBatisUtil.INSTRUMENT.openSession();
ProductMapper mapper = session.getMapper(ProductMapper.class);
Product product = mapper.selectOne(33L);
product.setProductName("毅毅");
mapper.save(product);
//提交事务
session.commit();
} catch (Exception e) {
// 事务回滚
session.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
@Test
public void testUpdate() {
SqlSession session = null;
try {
session = MyBatisUtil.INSTRUMENT.openSession();
ProductMapper mapper = session.getMapper(ProductMapper.class);
Product product = mapper.selectOne(33L);
product.setProductName("凡凡");
mapper.update(product);
//提交事务
session.commit();
} catch (Exception e) {
// 事务回滚
session.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
}