mybatis入门Demo
▶mybatis基本构成:
✔ SqlSessionFactoryBuilder(构造器):它会根据配置信息或者代码来生成SqlSessionFactory(工厂接口)
✔ SqlSessionFactory:依靠工厂来生成SqlSession(会话)
✔ SqlSession:是一个既可以发送SQL去执行并返回结果,也可以获取Mapper的接口
✔ SQL Mapper:它是MyBatis的设计的组件,它是由一个Java接口和XML文件(或注解)构成的,需要给出对应的SQL映射和映射规则,他负责发送SQL去执行,并返回结果
▶搭建开发环境
本次使用的是idea + maven ,所以直接去 https://mvnrepository.com/ maven仓库里下载依赖,mysql-connector-java(本次使用的是mysql数据库),mybatis依赖
◉ 新建Idea的普通Maven项目,操作步骤如下:
进入projecty,骨架什么的都不用选,直接下一步,有助于快速搭建maven项目
创建完成项目之后:
然后创建文件夹和文件:如下截图:
◉创建类和文件:
文件说明:
mybatis-config.xml:包含数据库连接实例的数据源(DateSource),决定事务范围和控制方式的事务管理器(TransactionManager)和映射器(SQL Mapper)
映射器(RoleMapper和RoleMapper.xml):定义了参数类型,描述SQL语句,定义查询结果和POJO的映射关系
单元测试了类test01:该类是测试mybatis的单测类,使用mybatis去操作数据库
▲mybatis-config.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>
<!--定义别名-->
<typeAliases>
<typeAlias type="com.zhang.mybatis.Role" alias="role"/>
</typeAliases>
<!--定义数据库信息, 默认使用development数据库构建环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置数据库连接信息-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="0000"/>
</dataSource>
</environment>
</environments>
<!--定义映射器-->
<mappers>
<mapper resource="com/zhang/mybatis/mapper/RoleMapper.xml"/>
</mappers>
</configuration>
数据库自己按照配置文件中的创建:
▲创建POJO类 Role
public class Role {
private Long id;
private String roleName;
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
▲创建RoleMapper.java
public interface RoleMapper {
public Role getRole(Long id);
}
▲创建RoleMapper.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.zhang.mybatis.mapper.RoleMapper">
<select id="getRole" parameterType="Long" resultType="role">
select id, role_name, note from t_role where id = #{id}
</select>
</mapper>
▲创建单元测试类----测试mybatis:
public class MybatisTest {
@Test
public void test01() {
//定义SqlSessionFactory
SqlSessionFactory sqlSessionFactory = null;
//定义SqlSession
SqlSession sqlSession = null;
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//打开session会话
sqlSession = sqlSessionFactory.openSession();
//获取映射器mapper
RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1L);
System.out.println("==================");
System.out.println(role.getId());
System.out.println(role.getRoleName());
System.out.println(role.getNote());
//事务提交
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
//事务回滚
sqlSession.rollback();
}finally {
//关闭sqlsession,将连接归还给数据库,防止资源耗尽
if (sqlSession != null){
sqlSession.close();
}
}
}
}
▲给出pom配置,其中的build标签下的读取resources很重要(缺少它则mybatis-config.xml中的<mapper resource="">会读取不到文件)
<dependencies>
<!--mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
◉ 启动单元测试类:运行结果显示:
使用mybatis操作数据库成功