Mybatis基础配置及增删查改操作

时间:2023-03-08 16:28:49
Mybatis基础配置及增删查改操作

一、简介

平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类。 不仅如此,访问不同的表,还会写很多雷同的代码,显得繁琐和枯燥。
那么用了Mybatis之后,只需要自己提供SQL语句,其他的工作,诸如建立连接,Statement, JDBC相关异常处理等等都交给Mybatis去做了,那些重复性的工作Mybatis也给做掉了,我们只需要关注在增删改查等操作层面上,而把技术细节都封装在了我们看不见的地方。

二、数据库的配置

使用mysql数据库

1、新建数据库

create database mybatis;

2、建表

use mybatis;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3、插入几条原始记录

INSERT INTO category_ VALUES (null,'category1');
INSERT INTO category_ VALUES (null,'category2');

三、新建java项目

1、导入jar包

需要两个包,一个mybatis包,一个mysql连接java的包

需要下载jar包的,GitHub地址:https://github.com/yeyangtao/Mybatis

2、新建Category类

对应数据库中表的字段

public class Category {
private int id;
private String name;
public int getId() {
return id;
}

3、在src根目录新建mybatis-config.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>
<package name="com.yyt.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/yyt/pojo/Category.xml"/>
</mappers>
</configuration>
 <environments>标签:作用主要是提供连接数据库用的驱动,数据库名称,编码方式,账号密码
 <typeAliases>标签:自动扫描com.yyt.pojo下的类型,使得在后续配置文件Category.xml中使用resultType的时候,可以直接使用Category,而不必写全com.yyt.pojo.Category

 <mappers>标签:映射Category.xml

4、和Category类同目录新建Category.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.yyt.pojo">
<select id="listCategory" resultType="Category">
select * from category_
</select>
<select id="getCategory" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<insert id="addCategory" parameterType="Category">
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
</mapper>

select标签中对应sql中select语句,其他也是如此

5、Test类测试增删查改操作

package com.yyt.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.yyt.pojo.Category; public class Test { public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//mybatis二级缓存
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//mybatis一级缓存
SqlSession session=sqlSessionFactory.openSession(); Category c = new Category();
/*
//增加
c.setName("新增加的Category");
session.insert("addCategory", c);
*/
/*
//删除id为4的数据
c.setId(4);
session.delete("deleteCategory",c);
*/
/*
//获取id为3的数据
c=session.selectOne("getCategory",3);
System.out.println("id为3的数据:"+c.getName());
//修改
c.setName("修改后的Category");
session.update("updateCategory",c);
*/ //查询并显示所有数据
listAll(session); session.commit();
session.close(); }
private static void listAll(SqlSession session) {
List<Category> cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName()+" "+c.getId());
}
}
}

查询的运行结果:

Mybatis基础配置及增删查改操作