博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
初识MyBatis
阅读量:4626 次
发布时间:2019-06-09

本文共 2549 字,大约阅读时间需要 8 分钟。

Mybatis

1.持久化
持久化,就是内存数据和硬盘数据状态的转换
2.ORM思想
Object Relation Mapping 对象关系映射
3.MyBatis入门案例
3.1导入jar包
依赖
<!--MySQL配置-->
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<!--MyBatis核心jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>

3.2书写大配置

<?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="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/y2165"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
3.3实体类
public class StudentInfo {
private Integer stuId;
private String stuName;
private Integer stuAge;
private Date stuDate;
}
3.4小配置
<?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="cn.happy.dao.IStudentInfoDAO">
<select id="findAll" resultType="cn.happy.entity.StudentInfo">
select * from studentinfo
</select>
</mapper>

两个注意事项:

1.你得更新POM.xml文件中build节点
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2.你得在大配置中关联小配置文件
<mappers>
<mapper resource="cn/happy/dao/IStudentInfoDAO.xml"/>
</mappers>
3.5 写测试类
3.5.1 String path="大配置的路径名";
InputStream is=Resources.getResourcesAsStream(path);
SessionFactory factory=new SessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();

结论:
1. 注意小配置的命名空间的名称

4.别名的使用

<typeAliases>
<!--<typeAlias type="cn.happy.entity.StudentInfo" alias="StudentInfo"></typeAlias>-->
<!--将该包中的简单类型 StudentInfo作为类的别名-->
<package name="cn.happy.entity"></package>
</typeAliases>

5.getMapper() 动态代理数据

class<T> 类型的类型
is = Resources.getResourceAsStream(path);
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo info = dao.getStudentById(3);
System.out.println(info.getStuName());

转载于:https://www.cnblogs.com/dongyuhan/p/7144133.html

你可能感兴趣的文章
asp.net 页面定时跳转的小技巧
查看>>
java继承
查看>>
python(2)-函数相关
查看>>
HDU 2120 Ice_cream's world I
查看>>
PAT 1034 有理数四则运算
查看>>
redis性能优化——生产中实际遇到的问题排查总结
查看>>
linux(Ubuntu)下安装JDK
查看>>
Python基础:输入与输出(I/O)
查看>>
C++ 11 学习3:显示虚函数重载(override)
查看>>
简单回溯,最少步数
查看>>
表面积最小(POJ3536)
查看>>
2013 Asia Regional Changchun I 题,HDU(4821),Hash
查看>>
分布式锁实践(二)-ZooKeeper实现总结
查看>>
POJ 1004
查看>>
Item 26: Avoid Returning References to Internal Class Objects(Effective C#)
查看>>
Rule 5: Put Stylesheets at the Top(Chapter 5 of High performance Web Sites)
查看>>
如何让你的移动端网站更快
查看>>
编译原理
查看>>
phthon
查看>>
使用第三方软件打开文件的情况
查看>>