框架(Frame)可以理解为是一个Jar包,里面定义了一些类和接口的集合,供我们在开发中使用,也可以理解为是某种应用的半成品,使用框架能极大的提高我们在开发中的效率,让我们能更加专注于编程本身,而不是拘泥于某些细节。
框架要解决的最重要的一个问题是技术整合的问题,在 Java和J2EE中,有着各种各样的技术,不同的开发需求就对应不同的技术要求,这就使得我们开发出来的应用高度依赖于这些技术,这样一来就会让技术自身的复杂性和风险性对开发的应用造成冲击。因此为了降低应用和技术耦合,于是就有了框架。这样一来软件的研发将集中在应用的设计上,而不是具体的技术实现,从而降低技术直接对应用产生影响,框架一般处在低层应用平台(如 J2EE)和高层业务逻辑之间的中间层。
一个完整的项目可以大致分为三层架构,分别是表现层(UI)、逻辑层(BLL)以及数据访问层(DAL),Java对此提出了分层思想,实现软件工程中的“高内聚、低耦合”。把问题划分开来各个解决,易于控制,易于延展,易于分配资源。 我们常见的MVC 软件设计思想就是很好的分层思想。 基于这三层架构就出现了许多框架,比如在业务逻辑层上的典型框架有Spring、SpringBoot,常见的Web表现层的框架就有SpringMVC等、还有数据持久层框架Mybatis,也就是我今天学习的内容。
mybatis 是一个优秀的基于 java 的持久层框架,它内部封装了 jdbc,让我们在开发中可以直接使用封装好的内容,不用再取做加载驱动、创建连接、创建statement的工作。
mybatis 通过 xml 或注解的方式将要执行的各种 statement 配置起来,并通过 java 对象和 statement 中sql 的动态参数进行映射生成最终执行的 sql 语句,最后由 mybatis 框架执行 sql 并将结果映射为 java 对象并返回。
基于XML使用mybatis完成数据访问
步骤:
定义sqlMapConfig.xml配置文件,在里面定义要连接的数据库的driver、url、username、password。并在里面配置Mapper映射的对应Dao层接口的xml文件定义对应dao的xml文件,命名最好和对应Dao层接口的名称一致。并再里面定义好名称空间、id以及ResultType(返回结果类型)在要调用Dao层方法的地方完成以下步骤读取配置文件
InputStream is=Resources.getResourceAsStream("sqlMapConfig.xml");创建sqlSessionFactory的构建着对象
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();使用构建者对象创建sqlSessionFactory
SqlSessionFactory factory =builder.build(is);使用sqlSessionFaxtory创建sqlSession
SqlSession sqlSession=factory.openSession();使用sqlSession创建dao接口的代理对象
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);使用代理对象执行dao接口的方法
List<Student> list = studentDao.findAll();关流
session.close(); is.close();sqlMapConfig.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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="gzgs/dao/StudentDao.xml"></mapper> </mappers> </configuration>StudentDao.xml
<?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="com.gzgs.dao.StudentDao"> <select id="findAll" resultType="com.gzgs.domain.Student"> select * from student </select> </mapper>StudentTest.java
package com.gzgs.test; import com.gzgs.dao.StudentDao; import com.gzgs.domain.Student; 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 java.io.InputStream; import java.util.List; public class StudentTest { public static void main(String[] args)throws Exception { //读取配置文件 InputStream is=Resources.getResourceAsStream("sqlMapConfig.xml"); //常见sqlSessionFactory的构建着对象 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //使用构建者对象创建sqlSessionFactory SqlSessionFactory factory =builder.build(is); //使用sqlSessionFaxtory创建sqlSession SqlSession sqlSession=factory.openSession(); //使用sqlSession创建dao接口的代理对象 StudentDao studentDao=sqlSession.getMapper(StudentDao.class); //使用代理对象执行dao接口的方法 List<Student> list = studentDao.findAll(); for (Student student:list){ System.out.println(student); } } }基于注解的使用方式
步骤:
定义sqlMapConfig.xml配置文件,在里面定义要连接的数据库的driver、url、username、password。并在里面配置Mapper映射的对应Dao层接口的全限定类名
在对应的Dao层接口的方法上添加相应的注解
@Select("select * from student") public List<Student> findAll();在要调用Dao层方法的地方完成以下步骤
读取配置文件
InputStream is=Resources.getResourceAsStream("sqlMapConfig.xml");创建sqlSessionFactory的构建着对象
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();使用构建者对象创建sqlSessionFactory
SqlSessionFactory factory =builder.build(is);使用sqlSessionFaxtory创建sqlSession
SqlSession sqlSession=factory.openSession();使用sqlSession创建dao接口的代理对象
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);使用代理对象执行dao接口的方法
List<Student> list = studentDao.findAll();关流
session.close(); is.close();sqlMapConfig.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> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test01"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.gzgs.dao.StudentDao"/> </mappers> </configuration>StudentDao
package com.gzgs.dao; import com.gzgs.domain.Student; import org.apache.ibatis.annotations.Select; import java.util.List; /** * 用户的持久层接口 */ public interface StudentDao { @Select("select * from student") public List<Student> findAll(); }StudentTest.java
package com.gzgs.test; import com.gzgs.dao.StudentDao; import com.gzgs.domain.Student; 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 javax.annotation.PreDestroy; import java.io.InputStream; import java.util.List; public class StudentTest { public static void main(String[] args)throws Exception{ //读取配置文件 InputStream is= Resources.getResourceAsStream("sqlMapConfig.xml"); //创建SqlSessionFactoryBulider SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //创建SqlSessionFactory SqlSessionFactory factory = builder.build(is); //创建SqlSession SqlSession session=factory.openSession(); //实验SqlSession生成代理对象 StudentDao dao=session.getMapper(StudentDao.class); //使用dao执行方法 List<Student> all = dao.findAll(); for(Student student:all){ System.out.println(student); } //关流 session.close(); is.close(); } } 本博客纯属个人学习笔记,学习资源来自黑马训练营,如有错误,感激指正