一、MyBatis简介
1、MyBatis的优点
消除了大量的JDBC冗余代码
低学习曲线
友好的Spring集成支持
除本身的缓存外,能友好的第三方缓存类库集成支持
良好的性能
2、MyBatis简单实例
- 创建表
create table t_person
(
id int auto_increment not null primary key,
name varchar(20) not null,
age int,
address varchar(50)
);
- sql配置文件
- pojo类
package com.xiao.mybatis.pojo;
public class Person {
private int pid;
private String pname;
private int age;
private String address;
public Person() {
}
public Person(String pname,int age,String address){
this.pname = pname;
this.age = age;
this.address = address;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age
+ ", address=" + address + "]";
}
}
- pojo配置文件
insert into t_person values(null,#{pname},#{age},#{address})
update t_person set pname = #{pname} , age = #{age} , address = #{address} where pid = #{pid}
delete from t_person where pid = #{pid}
- 测试类
package com.xiao.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.xiao.mybatis.pojo.Person;
public class PersonDaoTest {
SqlSession session = null;
/**
* 在连接数据库前加载
*/
public void setUp() {
try {
// 1、加载配置文件sqlMapConfig.xml
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2、解析配置文件sqlMapConfig.xml
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 3、通过factory创建session对象
session = factory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 执行完操作后提交事务,以及关闭连接
*/
public void tearDown() {
// 5、提交或回滚
session.commit();// session.rollback();
// 6、关闭session
if (null != session) {
session.close();
}
}
// 一、插入数据
public void addPersonTest() {
setUp();
Person person = new Person("boss", 20, "China");
// 4、执行插入操作
int count = session.insert("com.xiao.mybatis.pojo.addPerson", person);
System.out.println(count > 0 ? "插入成功" : "插入失败");
tearDown();
}
// 二、查询数据
public void selectPersonTest() {
setUp();
List personList = session.selectList("com.xiao.mybatis.pojo.getPersonList");
System.out.println( null != personList ? "查询成功" : "查询失败");
if(null != personList){
System.out.println(personList.toString());
}
tearDown();
}
// 二、条件查询数据
public void selectPersonByIdTest() {
setUp();
Person person = (Person) session.selectOne("com.xiao.mybatis.pojo.getPersonByPid",new Integer(7));
System.out.println( null != person ? "查询数据成功" : "查询失败");
if(null != person){
System.out.println(person.toString());
}
tearDown();
}
// 三、修改数据
public void updatePersonTest(){
setUp();
Person person = new Person(5,"boss", 100, "America");
int count = session.update("com.xiao.mybatis.pojo.updatePesron", person);
System.out.println(count > 0 ? "修改成功" : "修改失败");
tearDown();
}
// 四、删除数据
public void deletePersonTest(){
setUp();
Person person = new Person(5,"boss", 100, "America");
int count = session.delete("com.xiao.mybatis.pojo.deletePerson", person);
System.out.println(count > 0 ? "删除成功" : "删除失败");
tearDown();
}
public static void main(String[] args) {
PersonDaoTest personDaoTest = new PersonDaoTest();
//personDaoTest.addPersonTest(); // 插入
//personDaoTest.selectPersonTest(); // 查询
personDaoTest.selectPersonByIdTest();
//personDaoTest.updatePersonTest(); // 修改
//personDaoTest.deletePersonTest(); // 删除
}
}
进行运行,查看运行结果