您的位置:首页 > 其它

mybatis自学入门实例(一)

2016-07-23 20:28 585 查看

mybatis 是什么?

MyBatis 是支持普通SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis  消除了几乎所有的JDBC 代码和参数的手工设置以及结果集的检索。MyBatis  使用简单的XML或注解用于配置和原始映射,将接口和Java 的POJOs(Plan  Old  Java  Objects ,普通的Java对象)映射成数据库中的记录。


入门

每一个 MyBatis 的应用程序都以一个 SqlSessionFactory对象的实例为核心。

SqlSessionFactory 对象的实例可以通SqlSessionFactoryBuilder对象来获得。SqlSessionFactoryBuilder 对象可以从XML 配置文件,或从Configuration 类的习惯准备的实例中构建SqlSessionFactory 对象。

从XML 中构建SqlSessionFactory从XML 文件中构SqlSessionFactory 的实例非常简单。这里建议你使用类路径下的资源文件来配置,但是你可以使用任意的Reader 实例,这个实例包括由文字形式的文件路径或URL 形式的文件路径file://来创建。MyBatis 包含了一些工具类,称作为资源,这些工具

类包含一些方法,这些方法使得从类路径或其他位置加载资源文件更加简单。

String resource = "org/mybatis/example/Configuration.xml";

Reader reader = Resources.getResourceAsReader(resource);

sqlMapper = new SqlSessionFactoryBuilder().build(reader);


XML 配置文件包含对MyBatis 系统的核心设置,包含获取数据库连接实例的数据源和

决定事务范围和控制的事务管理器。

入门实例

## 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="db.properties" />
<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="${user}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/StudentMapper.xml" />//对应的POJO对象的配置文件配置在这里
</mappers>
</configuration>


## JDBC配置文件 ##


driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/testmybaties
user=root
password=root


## Student pojo对象类 ##


package com;
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer tid;
public Student(String name, Integer age, Integer tid) {
this.name = name;
this.age = age;
this.tid = tid;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", tid=" + tid + "]";
}
}


##StudentMapper.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.StudentMapper">
<insert id="insertStudent" parameterType="com.Student">
<!-- insert into STUDENT values(se_Student.nextval,#{name},#{age},#{tid}) -->
insert into STUDENT(name,age,tid) values(#{name},#{age},#{tid})
</insert>
</mapper>


## 测试类Test.java ##


package pojo;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Student;

public class Test {
public static void main(String[] args) throws IOException {
String resource = "sqlConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlMapper.openSession();
Student student = new Student("lilei",23,1);
session.insert("com.StudentMapper.insertStudent", student);
session.commit();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 持久层框架