1、Mybatis与hibernate的不同
(1)Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句。mybatis可以通过XML或注解方式灵活配置要运行的sql语句,并将java对象和sql语句映射生成最终执行的sql,最后将sql执行的结果再映射生成java对象。
mybatis需要在配置文件中书写sql语句:
<delete id="deleteStudentById" parameterType="Integer">
delete from student
where studentno = #{value}
</delete>
(2)Mybatis学习门槛低,简单易学,程序员直接编写原生态sql,可严格控制sql执行性能,灵活度高,非常适合对关系数据模型要求不高的软件开发,例如互联网软件、企业运营类软件等,因为这类软件需求变化频繁,一但需求变化要求成果输出迅速。但是灵活的前提是mybatis无法做到数据库无关性,如果需要实现支持多种数据库的软件则需要自定义多套sql映射文件,工作量大。
(3)Hibernate对象/关系映射能力强,数据库无关性好,对于关系模型要求高的软件(例如需求固定的定制化软件)如果用hibernate开发可以节省很多代码,提高效率。但是Hibernate的学习门槛高,要精通门槛更高,而且怎么设计O/R映射,在性能和对象模型之间如何权衡,以及怎样用好Hibernate需要具有很强的经验和能力才行。总之,按照用户的需求在有限的资源环境下只要能做出维护性、扩展性良好的软件架构都是好架构,所以框架只有适合才是最好。
2、原始Dao开发
(1)创建接口:
public interface StudentDao {
public Student selectStudentById(Integer id);
}
(2)创建接口的实现类:
public class StudentDaoImp implements StudentDao{
private SqlSessionFactory sqlSessionFactory;
public StudentDaoImp(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Student selectStudentById(Integer studentno){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession.selectOne("test.findStudentById", studentno);
}
}
(3)创建测试类:
public class Test1 {
public SqlSessionFactory sqlSessionFactory;
public void before() throws Exception {
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
public void testDao() throws Exception {
StudentDao studentDao = new StudentDaoImp(sqlSessionFactory);
Student student=studentDao.selectStudentById(201811);
System.out.println(student);
}
public static void main(String args []){
Test1 test1=new Test1();
try {
test1.before();
test1.testDao();
} catch (Exception e) {
e.printStackTrace();
}
}
}
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1750905143.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@685cb137]
DEBUG [main] - ==> Preparing: select * from student where studentno = ?
DEBUG [main] - ==> Parameters: 201811(Integer)
DEBUG [main] - <== Total: 1
Student [studentno=201811, sname=zhai, sex=男, birthday=1998-11-11, classno=tx171, point=890, phone=1234567890, email=null]
3、mapper动态代理开发
(1)接口:
public interface StudentMapper {
Student findStudentById(Integer studentno);
}
- Mapper接口方法名和xml中定义的每个statement的id相同
- Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
- Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
(2)配置文件(注意命名空间):
<mapper namespace="pers.zhb.mapper.StudentMapper">
<select id="findStudentById" parameterType="Integer" resultType="pers.zhb.pojo.Student">
select * from student where studentno = #{v}
</select>
注意:xml文件中的namespace与mapper接口的类路径相同
(3)测试:
public class StudentMapperTest {
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帮我生成一个实现类 (给接口)
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.findStudentById(201811);
System.out.println(student);
}
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1291286504.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4cf777e8]
DEBUG [main] - ==> Preparing: select * from student where studentno = ?
DEBUG [main] - ==> Parameters: 201811(Integer)
DEBUG [main] - <== Total: 1
Student [studentno=201811, sname=zhai, sex=男, birthday=1998-11-11, classno=tx171, point=890, phone=1234567890, email=null]