一、mybatis与hibernate区别
1、mybatis不完全是ORM框架,mybatis可以通过xml或注解方式灵活配置要运行
的sql语句,并将java对象和sql语句映射生成最终执行的sql,最终将sql执行
的结果再映射生成Java对象;
2、mybatis学习门槛低,简单易学,程序员直接编写原生态sql,可严格控制sql执
行性能,灵活度高,适合对关系数据模型要求不高的软件开发,如互联网软
件、企业运营类软件等。因为这类软件需求变化频繁,一旦需求变化要求成
果输出迅速,但是灵活的前提是mybatis无法做到数据库无关性,如果需要实
现支持多种数据库的软件则需要自定义多套sql映射文件,工作量大;
3、hibernate 对象/关系映射能力墙,数据库无关性好,对于关系模型要求高的
软件(需求固定的定制化软件),使用hibernate开发可以节省代码,提高
效率,但是学习门槛高
二、原始DAO开发(hibernate)
mybatisDaoTest.java
package com.itheima.mybatis.junit;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.itheima.mybatis.dao.UserDao;
import com.itheima.mybatis.dao.UserDaoImpl;
import com.itheima.mybatis.pojo.User;
public class MybatisDaoTest {
public SqlSessionFactory sqlSessionFactory;
@Before
public void before() throws Exception {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void testDao() throws Exception {
UserDao userDao = new UserDaoImpl(sqlSessionFactory);
User user = userDao.selectUserById(10);
System.out.println(user);
}
}
UserDao.java
package com.itheima.mybatis.dao;
import com.itheima.mybatis.pojo.User;
public interface UserDao {
//通过用户ID查询一个用户
public User selectUserById(Integer id);
}
UserDaoImpl.java
package com.itheima.mybatis.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.itheima.mybatis.pojo.User;
/**
* Dao
* @author lx
*
*/
public class UserDaoImpl implements UserDao {
//注入
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
//通过用户ID查询一个用户
public User selectUserById(Integer id){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession.selectOne("test.findUserById", id);
}
//通过用户名称模糊查询
public List<User> selectUserByUsername(Integer id){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession.selectList("test.findUserById", id);
}
}
三、Mapper动态代理开发
原始dao开发,重复代码较多
Mapper接口开发需要遵循的规范:
1、Mapper.xml文件中的namespace与Mapper接口的类路径相同
2、Mapper接口方法名和mapper,xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的
parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的
resultTtpe的类型相同
UserMapper.java
package com.itheima.mybatis.mapper;
import com.itheima.mybatis.pojo.User;
public interface UserMapper {
//遵循四个原则
//接口 方法名 == User.xml 中 id 名
//返回值类型 与 Mapper.xml文件中返回值类型要一致
//方法的入参类型 与Mapper.xml中入参的类型要一致
//命名空间 绑定此接口:<mapper namespace="com.itheima.mybatis.mapper.UserMapper">
public User findUserById(Integer id);
}
MybatisMapperTest.java
package com.itheima.mybatis.junit;
import java.io.InputStream;
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 org.junit.Test;
import com.itheima.mybatis.mapper.UserMapper;
import com.itheima.mybatis.pojo.User;
public class MybatisMapperTest {
@Test
public void testMapper() throws Exception {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//SqlSEssion帮我生成一个实现类 (给接口)
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(10);
System.out.println(user);
}
}