mybatis学习笔记

最近在找实习的时候发现了许多的公司的后台持久层框架使用的是mybatis,因此利用假期的时间简单的了解了一下mybatis,主要学习了一下mybatis的配置,以及一些简单的curd操作,对于一些复杂的操作,在之后的学习之后再做补充。

1.配置mybatis

为了学习mybatis, 我们需要准备mybatis的jar包,同时为了与数据库进行连接,我们也要导入java的 mysql连接包。

mybatis下载地址:https://github.com/mybatis/mybatis-3/releases
mysql连接下载地址:https://dev.mysql.com/downloads/file/?id=476197

下载完成后,将其导入到你的项目中去,在此我们只用的是ide为idea

导入

导入

写入相关配置文件:

XML 中构建 SqlSessionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}”/> //驱动com.mysql.jdbc.Driver
<property name="url" value="${url}”/>//连接地址jdbc:mysql://127.0.0.1:3306/mybites_test_617
<property name="username" value="${username}”/>//用户名
<property name="password" value="${password}”/>// 密码
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml”/>//连接映射
</mappers>
</configuration>

连接映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
<mapper namespace="map">
<select id="findById" parameterType="int" resultType="entity.User">
select * from user where id = #{id}
</select>
<insert id="insertUser" parameterType="user" statementType="PREPARED" keyColumn="id" useGeneratedKeys="true">
INSERT INTO user (name, address) VALUES
(#{name}, #{address})
</insert>
<update id="updateUser" parameterType="user">
UPDATE user SET name = #{name}, address = #{address}
WHERE id = #{id}
</update>
</mapper>

使用方法:

1
2
3
4
5
6
String resource = "map/mybatisConfig.xml”; // 连接配置文件
Reader reader = null;
SqlSession sqlSession;
reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();

第一个参数对应mapper中的ID,第二个参数为parameterType参数类型

1
2
3
User user = sqlSession.selectOne("findById", 0);// 查找
sqlSession.insert("insertUser", user);// 增加
sqlSession.update("updateUser", user);// 更新

利用注解来进行操作:
1.创建接口

1
2
3
4
public interface InterfaceUserMap {
@Delete("delete from user where id = #{id}")
public void deleteUser(int id);
}

2.内部方法实现接口

1
2
3
4
InterfaceUserMap interfaceUserMap = sqlSession.getMapper(InterfaceUserMap.class);
interfaceUserMap.deleteUser(1);
sqlSession.commit();
sqlSession.close();

总结:在这本想着来谈谈自己对mybatis和hibernate的理解,但发现自己对于两个框架的理解还是及其简陋的,个人感觉hibernate的封装性更好一些,而mybatis更灵活吧

最后以mybatis作者的一句话结尾:
If you are starting a new project and you’re in full control of your object model and database design, Hibernate is a good choice of O/R tool.If you are accessing any 3rd party databases (e.g. vendor supplied), or you’re working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. That’s were an SQL Mapper comes in handy
如果您正在启动一个新项目,并且完全控制了对象模型和数据库设计,Hibernate是O/R工具的一个好选择。如果您正在访问任何第三方数据库(例如供应商提供的),或者您正在处理遗留数据库,或者甚至只是一个设计得很差的数据库。然后,O/R映射器可能无法处理这种情况。这是一个SQL映射器派上用场。

推荐文章