MyBatis(数据持久层框架)
官方对其的优势简介:
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及抽取结果集。
MyBatis 使用简单的 XML 或注解来配置和映射基本体,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
实现过程:
1.在普通maven项目或者其他类型项目的基础上添加依赖
pom.xml配置文件中需要添加以下几个关键包和插件:
mybatis
mysql
resources插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven--plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
compiler插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
2.添加完成后要在数据源(resources)文件夹书写配置文件
jbcp.properties 用于配置连接数据库所需要的字符串
mybatis-config.xml 用于配置mybatis
<?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="jdbc.properties" />
<!-- 配置了运行时环境,内部可以配置多个环境,当前默认使用的是名为development的环境 -->
<environments default="development">
<environment id="development">
<!-- 事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 配置连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 接口映射器,映射路径默认为resource -->
<mapper resource="BlogMapper.xml"/>
</mappers>
</configuration>
BlogMapper.xml 配置了映射,该xml文件规定了各种CRUD方法的语句以及映射名
<?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.accp.mapper.BlogMapper">
<!-- 每个方法的返回值都要注意不要写错,特别是查询,查询多个和单个时返回类型都是填写单个实体类的路径 -->
<insert id="save" parameterType="com.accp.pojo.Blog">
insert into t_blog values(null, #{title}, #{content}, #{createTime})
</insert>
<delete id="remove" parameterType="Integer">
delete from t_blog where id=#{id}
</delete>
<update id="update" parameterType="com.accp.pojo.Blog">
update t_blog set title=#{title},conten=#{content},createTime=#{createTime} where id=#{id}
</update>
<select id="findById" parameterType="Integer" resultType="com.accp.pojo.Blog">
select id,title,content,createTime form t_blog where id=#{id}
</select>
<select id="findAll" resultType="com.accp.pojo.Blog">
select*from t_blog
</select>
</mapper>
3.书写完配置文件和各种接口映射以后开始测试
public class MyBatisTest {
public static void main(String[] args) throws IOException {
//定义数据源路径
String resource="mybatis-config.xml";
//通过resources获取数据流
InputStream inputStream=Resources.getResourceAsStream(resource);
//通过new创建SqlSessionFactoryBuilder对象,,调用其build方法,传入流,创建出factory对象
SqlSessionFactory factory= new SqlSessionFactoryBuilder().build(inputStream);
//调用factory对象openSession方法创建sqlSession对象
SqlSession sqlSession=factory.openSession();
//查询测试
List<Blog> list=sqlSession.selectList("com.accp.mapper.BlogMapper.findAll");
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i).toString());
}
sqlSession.close();
// try {
// //创建添加测试实例对象
// Blog blog = new Blog();
// blog.setTitle("testTitle");
// blog.setContent("testContent");[/
// blog.setCreateTime(new Date());
//
// //SqlSession对象执行添加方法,传入添加方法所在位置和添加对象
// int rows=sqlSession.insert("com.accp.mapper.BlogMapper.save",blog);
//
// //运行正常提交
// sqlSession.commit();
// System.out.println(rows);
// } catch (Exception e) {
// //运行异常则回滚
// sqlSession.rollback();
// }finally {
// //记得关闭SqlSession
// sqlSession.close();
// }
}
}
MyBatis实现小记
最新推荐文章于 2024-10-06 20:16:20 发布