
本文共 5181 字,大约阅读时间需要 17 分钟。
MyBatis基础
1. MyBatis快速入门
1.1 框架介绍
框架是一款半成品软件,我们可以基于这个半成品软件继续开发,来完成我们个性化的需求。通过框架,我们可以减少重复性代码的编写,提高开发效率。
1.2 ORM(对象关系映射)
ORM(Object Relational Mapping)是用来解决面向对象与关系型数据库互不匹配问题的技术。其核心思想是通过自动映射,将数据库中的数据表、表字段与对象进行对应。
1.3 MyBatis介绍
MyBatis 是一款基于 Java 的持久层框架,通过封装 JDBC,简化了数据库操作。它通过 XML 或注解配置 SQL 语句,并将结果映射为 Java 对象。MyBatis 展现了 ORM 的特点,帮助开发者更高效地与数据库交互。
1.4 MyBatis入门程序
要使用 MyBatis,需要按照以下步骤进行操作:
1. 数据准备
创建一个空的 MySQL 数据库,并准备好相应的数据表。
2. 导入 jar 包
将 MyBatis 的相关 jar 包添加到项目的类路径中。
3. 创建映射配置文件
在 src
目录下新建一个 StudentMapper.xml
文件,用于定义数据库与对象的映射关系。
4. 创建核心配置文件
在 src
目录下新建一个 MyBatisConfig.xml
文件,配置数据库连接信息等设置。
5. 编写测试类
编写一个测试类 StudentTest01
,完成以下操作:
查询全部
@Testpublic void selectAll() throws IOException { InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); Listlist = sqlSession.selectList("StudentMapper.selectAll"); for (Student student : list) { System.out.println(student); } sqlSession.close(); is.close();}
根据 id 查询
@Testpublic void selectById() throws IOException { InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); Student student = sqlSession.selectOne("StudentMapper.selectById", 3); System.out.println(student); sqlSession.close(); is.close();}
2. MyBatis 相关 API
2.1 Resources
org.apache.ibatis.Resources
提供了获取资源文件的工具类,常用的方法有 getResourceAsStream
用于获取类路径下的资源文件。
2.2 SqlSessionFactoryBuilder
org.apache.ibatis.session.SqlSessionFactoryBuilder
用于构建 SqlSessionFactory
工厂对象,通过 build
方法加载配置文件并生成工厂实例。
2.3 SqlSessionFactory
org.apache.ibatis.session.SqlSessionFactory
是工厂接口,用于获取 SqlSession
构建器对象。
2.4 SqlSession
org.apache.ibatis.session.SqlSession
是构建者对象,提供了执行 SQL 语句、管理事务、接口代理等功能。
3. MyBatis 映射配置文件
3.1 映射配置文件介绍
映射配置文件的主要作用是定义数据库与对象之间的映射关系。常用的标签有 <select>
、 <insert>
、 <update>
、 <delete>
。
3.2 查询功能
3.3 新增功能
insert into student values(#{id}, #{name}, #{age})
3.4 修改功能
update student set name = #{name}, age = #{age} where id = #{id}
3.5 删除功能
delete from student where id = #{id}
4. MyBatis 核心配置文件
4.1 核心配置文件介绍
核心配置文件用于配置数据库环境、事务管理、连接池等设置。
4.2 数据库连接配置
4.3 起别名
通过配置文件起别名,可以简化代码编写。例如:
5. MyBatis 传统方式实现 Dao 层
5.1 Dao 层实现方式
按照分层思想,MyBatis 的 Dao 层通过接口和实现类对数据库进行 CRUD 操作。
5.2 功能实现
public interface StudentMapper { ListselectAll(); Student selectById(Integer id); Integer insert(Student student); Integer update(Student student); Integer delete(Integer id);}public class StudentMapperImpl implements StudentMapper { public List selectAll() { List list = null; InputStream is = null; SqlSession sqlSession = null; try { is = Resources.getResourceAsStream("MyBatisConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); sqlSession = sqlSessionFactory.openSession(true); list = sqlSession.selectList("StudentMapper.selectAll"); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (is != null) { is.close(); } } return list; } // 其他方法同理}
5.3 业务层接口
public interface StudentService { ListselectAll(); Student selectById(Integer id); Integer insert(Student student); Integer update(Student student); Integer delete(Integer id);}public class StudentServiceImpl { private StudentMapper mapper = new StudentMapperImpl(); public List selectAll() { return mapper.selectAll(); } // 其他方法同理}
5.4 控制层测试
public class StudentController { private StudentService service = new StudentServiceImpl(); @Test public void selectAll() { Liststudents = service.selectAll(); for (Student student : students) { System.out.println(student); } } @Test public void selectById() { Student student = service.selectById(3); System.out.println(student); } // 其他测试方法同理}
6. LOG4J 日志配置
为了更好地调试和排查问题,可以集成 LOG4J 进行日志记录。通过配置 LOG4J 的话,开发者可以查看 SQL 语句执行情况、参数信息等。
LOG4J 配置
LOG4J 输出示例
DEBUG [main] -Preparing: select * from student where id = ?DEBUG [main] -Parameters: 3(Integer)DEBUG [main] -Total: 1DEBUG [main] -Closed JDBC Connection [com.mysql.jdbc.JDBC4Connection@6adbc9d]DEBUG [main] -Returned connection 112049309 to pool.Student{id=3, name=‘王五’, age=18}
发表评论
最新留言
关于作者
