Mybatis 基础应用-封装数据库
2017-05-19 10:25
405 查看
MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。SqlSessionFactoryBuilder可以从一个xml配置文件或者一个预定义的配置类的实例获得。
用xml文件构建SqlSessionFactory实例是非常简单的事情。推荐在这个配置中使用类路径资源(classpath resource),但你可以使用任何Reader实例,包括用文件路径或file://开头的url创建的实例。MyBatis有一个实用类—-Resources,它有很多方法,可以方便地从类路径及其它位置加载资源。
需要用到的jar包文件
mybatis.jar
ojdbc14.jar (事宜你的数据库jar包)
mybatis-3-config.dtd
mybatis-3-mapper.dtd
看代码(以mysql为例):
首先配置封装数据库的xml文件
下面是一个工厂类。调用上面xml文件
编写mapper(接口类和mapper xml(sql语句) )
在这里需要注意的是,接口里可以传递参数可作为查询数据库时的关键字,但要是有多个参数的话就要提前将数据封装到实体类,把实体类做参数;接上,在哪调用这些接口?答案是mapper xml
类都封装好了,怎么使用呢:
小提示:当我们用上面sm调用了方法之后,想更新数据库内容我们就得需要提交数据库,语句:ss.commit();
注意:创建xml文件的时候需要注意
PUBLIC “-//mybatis.org//DTD Config 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-config.dtd”>
PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
都需要配置。否则可能会出问题。
每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。SqlSessionFactoryBuilder可以从一个xml配置文件或者一个预定义的配置类的实例获得。
用xml文件构建SqlSessionFactory实例是非常简单的事情。推荐在这个配置中使用类路径资源(classpath resource),但你可以使用任何Reader实例,包括用文件路径或file://开头的url创建的实例。MyBatis有一个实用类—-Resources,它有很多方法,可以方便地从类路径及其它位置加载资源。
需要用到的jar包文件
mybatis.jar
ojdbc14.jar (事宜你的数据库jar包)
mybatis-3-config.dtd
mybatis-3-mapper.dtd
看代码(以mysql为例):
首先配置封装数据库的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.po.Student" alias="Student" //类和类别名 /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:oracle"></property> <property name="username" value="scott" ></property> <property name="password" value="32767"></property> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mapper/StudentMapper.xml"></mapper> //注册mapper文件 </mappers> </configuration>
下面是一个工厂类。调用上面xml文件
package com.sqlSessionFactory; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil { public static final SqlSessionFactory sqlSessionFactory; static{ String resource = "mybatis-config.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } }
编写mapper(接口类和mapper xml(sql语句) )
package com.mapper; import java.util.List; import com.po.Student; public interface StudentMapper { public List<Student> selectStudentAll(); public List<Student> selectStudent(String name); public void updateStudent(Student stu); }
在这里需要注意的是,接口里可以传递参数可作为查询数据库时的关键字,但要是有多个参数的话就要提前将数据封装到实体类,把实体类做参数;接上,在哪调用这些接口?答案是mapper 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.mapper.StudentMapper"> <resultMap type="Student" id="backMap"></resultMap> <select id="selectStudentAll" resultMap="backMap"> select * from student </select> <select id="selectStudent" resultMap="backMap" parameterType="java.lang.String"> select * from student where name =#{name} </select> //id为调用的接口 //resultMap="backMap" 返回值类型,backMap为别名 //parameterType为传递参数的类型 <update id="selectStudent" parameterType="Student"> </up ac3b date> </mapper>
类都封装好了,怎么使用呢:
public static void main(String[] args) { java.util.Scanner in = new java.util.Scanner(System.in); // TODO Auto-generated method stub SqlSessionFactory ssf = SqlSessionFactoryUtil.getSqlSessionFactory(); SqlSession ss = ssf.openSession(); StudentMapper sm = ss.getMapper(StudentMapper.class); List<Student> list = sm.selectStudentAll(); Iterator<Student> it = list.iterator(); while(it.hasNext()){ System.out.println(it.next()); } }
小提示:当我们用上面sm调用了方法之后,想更新数据库内容我们就得需要提交数据库,语句:ss.commit();
注意:创建xml文件的时候需要注意
PUBLIC “-//mybatis.org//DTD Config 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-config.dtd”>
PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
都需要配置。否则可能会出问题。
相关文章推荐
- 创新谈-开发基础框架进行应用与数据库访问的封装-王作敬
- 基础水文数据库应用-水文预报中任意时段净雨计算
- 转-数据库应用基础系列-第2章 并发操作的一致性问题 (1)
- VB6基本数据库应用(一):数据库基础
- java自学之路-----数据库基础应用
- mybatis从零基础到增删改查数据库
- [ASP基础]ASP与数据库应用(给初学者)
- IOS-20-数据库操作之(SQLite第三方封装库FMDB)的基础入门使用及demo源码
- WEB应用之: 对PHP数据库连接的简单封装
- DELPHI基础教程 第十四章 简单数据库应用的创建及MASTAPP介绍
- hibernate和mybatis中数据库锁的应用
- 基础水文数据库应用-水文预报中洪量计算
- JAVA操作数据库方式与设计模式应用-Java基础-Java-编程开发
- 数据库应用基础系列
- QX项目实战-10.基础架构实验一:传递消息、序列化对象和数据库封装
- 数据库基础:讲解MySQL索引的概念及数据库索引的应用<转载>
- 数据库应用最基础知识!
- [零基础学JAVA]Java SE应用部分-33.Java数据库编程(JDBC)之二 推荐
- android基础笔记:数据库SQLite应用
- Groovy 数据库应用开发实践-SQL对象托管和封装