作为ORM框架, 与Hibernate框架相比
Hibernate框架
优点:面向对象编程,设计数据库。开发效率高。缺点:运行效率相对较低(其他框架),空间占用内存比较严重。
应用场景:数据量相对较小的级别的项目中。
Mybatis优点:
运行效率高,相对于Hibernate高。Mybatis对jdbc进行封装。
学习成本低,简单。关键是学好sql编写。
Mybatis开发步骤
使用mysql数据库
1.导入jar包
2.编mybatis-config.xml文件
<dataSource type="POOLED">
<property name="driver" value="${driverClass}" />
<property name="url" value="${jdbcUrl}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</dataSource>
配置文件中的pooled表示这是JDBC连接对象的数据源连接池的实现,
其他属性对应的是jdbc.properties的配置
<?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="jdbc.properties"></properties>
<!-- 声明数据连接环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClass}" />
<property name="url" value="${jdbcUrl}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件<mapper resource="cn/cache/cache.xml"/> -->
<mapper resource="sdibt/lxj/entity/Book.xml" />
</mappers>
</configuration>
mysql 的jdbc.properties配置文件
3.mybatis框架不像hibernate框架一样自动生成表,必须要提前建好表
创建实体类,与表的列对应,实体的属性名是驼峰结构对应数据库的是下划线
package sdibt.lxj.entity;
public class Book {
private int id;
private String bookName;
private Double bookPrice;
private int bookPage;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Double getBookPrice() {
return bookPrice;
}
public void setBookPrice(Double bookPrice) {
this.bookPrice = bookPrice;
}
public int getBookPage() {
return bookPage;
}
public void setBookPage(int bookPage) {
this.bookPage = bookPage;
}
}
4.定义一个关于实体的配置文件,一般是xml文件
在mybatis-config.xml配置映射,<mapper resource="sdibt/lxj/entity/Book.xml" />
mybatis的sql语句都是放在这样的配置文件当中,将来dao需要执行sql语句就调用配置文件当中的就行
namespace:命名空间,为防止多个配置文件的sql有相同的id
查询标签:select
id:这条查询语句的id,将来用namespace.id的方式调用sql
resultType:返回值类型
<?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">
<!-- namespace必须定义-->
<mapper namespace="sdibt.lxj.entity.Book">
<!-- 查询所有book -->
<select id="all" resultType="sdibt.lxj.entity.Book">
select id as id,book_name as bookName,book_price as bookPrice,book_page as bookPage from book
</select>
</mapper>
5.测试
package sdibt.lxj.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sdibt.lxj.entity.Book;
public class TestMybatis {
public static void main(String[] args) throws ClassNotFoundException {
//加载配置文件
InputStream in = String.class.getResourceAsStream("/mybatis-config.xml");
//建造session工厂
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
//创建session
SqlSession session = sf.openSession();
//查询
List<Book> books = session.selectList("sdibt.lxj.entity.Book.all");
for (Book book : books) {
System.out.println(book.getBookName());
}
}
}
sql语句返回值类型:
<select id="all" resultType="sdibt.lxj.entity.Book">
select id as id,book_name as bookName,book_price as bookPrice,book_page as bookPage from book
</select>
如果sql语句写成:select * from book
book_name 就对应不了bookName,属性就不会赋值。
返回值类型可以使Map类型,里面装的是K,V对
<select id="all" resultType="java.util.Map">
select * from book
</select>
现在在查询这个sql语句的话,查询到的数据每一行就会放在一个Map集合当中,多行数据仍然组成一个list集合,不过获取属性的话就要获取表的列名map.get("book_name")
List<Map> books = session.selectList("sdibt.lxj.entity.Book.all");
for (Map map : books) {
System.out.println(map.get("book_name"));
}
将mybatis操作数据公共代码封装做一个MybatisUtils类
package sdibt.lxj.util;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
private static SqlSessionFactory sf;
static{
//加载配置文件静态代码块
InputStream in = String.class.getResourceAsStream("/mybatis-config.xml");
//2.创建session工厂
sf = new SqlSessionFactoryBuilder().build(in);
}
/**
* 获取session
* @return
*/
public static SqlSession getSession(){
SqlSession session = sf.openSession();
return session;
}
/**
* 关闭session
* @param session
*/
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}