共计 3839 个字符,预计需要花费 10 分钟才能阅读完成。
Mybatis 快速入门
步骤:
- 1. 加入 mybatis 的依赖
- 2. 添加 Mybatis 的配置文件
- 3. 场景介绍
- 4. 编写实体类丶 mapper 接口及 mapper.xml 文件
- 5. 编写测试代码
demo:
public class TUser {
private Integer id;
private String userName;
private String realName;
private Byte sex;
private String mobile;
private String email;
private String note;
private Integer positionId;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public String getUserName() {return userName;}
public void setUserName(String userName) {this.userName = userName;}
public String getRealName() {return realName;}
public void setRealName(String realName) {this.realName = realName;}
public Byte getSex() {return sex;}
public void setSex(Byte sex) {this.sex = sex;}
public String getMobile() {return mobile;}
public void setMobile(String mobile) {this.mobile = mobile;}
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
public String getNote() {return note;}
public void setNote(String note) {this.note = note;}
public Integer getPositionId() {return positionId;}
public void setPositionId(Integer positionId) {this.positionId = positionId;}
}
public interface TUserMapper {TUser selectByPrimaryKey(Integer id);
}
TUserMapper.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.enjoylearning.mybatis.mapper.TUserMapper">
<select id="selectByPrimaryKey" resultType="com.enjoylearning.mybatis.entity.TUser" parameterType="java.lang.Integer">
select
id,
user_name ,
real_name ,
sex,
mobile,
email,
note,
position_id positionId
from t_user
where id = #{id}
</select>
</mapper>
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>
<properties resource="db.properties" />
<settings>
<!-- 全局开关 -->
<!-- 设置自动驼峰转换 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 开启懒加载 -->
<setting name="lazyLoadingEnabled" value="false"/>
</settings>
<!-- 别名定义 -->
<typeAliases>
<package name="com.enjoylearning.mybatis.entity"/>
</typeAliases>
<!-- 配置 environment 环境 -->
<environments default="development">
<!-- 环境配置 1,每个 SqlSessionFactory 对应一个环境 -->
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="UNPOOLED">
<property name="driver" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</dataSource>
</environment>
</environments>
<!-- 映射文件,mapper 的配置文件 -->
<!-- <mappers>
直接映射到相应的 mapper 文件
<mapper resource="sqlmapper/TUserMapper.xml" />
</mappers> -->
<mappers>
<mapper class="com.enjoylearning.mybatis.mapper.TUserMapper" />
</mappers>
</configuration>
db.properties:
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
jdbc_username=root
jdbc_password=qq997921
测试类:
public class MybatisQuickStart {
private SqlSessionFactory sqlSessionFactory;
@Test
// 快速入门
public void quickStart() throws IOException {
//------------- 第一阶段 -------------
// 1. 读取 mybatis 配置文件创 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
inputStream.close();
//------------- 第二阶段 -------------
// 2. 获取 sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3. 获取对应 mapper
TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
//------------- 第三阶段 -------------
// 4. 执行查询语句并返回结果
TUser user = mapper.selectByPrimaryKey(1);
System.out.println(user.toString());
}
}
Mybatis 两种编程模型
1. 封装了 iBatis 编程模型, 使用 sqlSession 对外提供数据库的访问
比如使用 sqlSession 直接访问数据库, 代码如:
User user1 = session.selectOne("com.XX.XX.XXMapper.selectOneByKey","1");
2. 使用 mapper 接口编程, 就可以访问数据库
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1);
对比两种模型
第一种缺点: 代码里面会很多硬编码, 硬编码不好维护, 代码不优雅, 并且 selectOne 方法名称没有业务含义.
优先选择 mapper 接口编程。
Mybatis 核心流程三大阶段
缕清思路也就是 Mybatis 核心流程之后,我们就开始写代码了,详见下节~~
正文完