您的位置:首页 > 数据库

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 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”>

都需要配置。否则可能会出问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis java xml 数据库