做web开发时离不开对数据库的操作,时下最流行的javaweb框架ssm中使用Mybatis完成对于数据库的访问,代替了ssh框架中的Hibernate。比起Hibernate,Mybatis拥有更轻量级,操作更简便等优势。同时在DAO层的实现过程中,Mybatis只需要将sql语句写在相关的mapper中即可,而不必像Hibernate那样一个个去实现DAO层接口。下面不使用注解方式将sql语句写入mapper,用将sql语句写在xml中方法来实现Mybatis中对于数据库的CRUD即增删改查操作。
首先在数据库中建表:
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;
接着在Eclipse中创建一个java project,在src下建一个包com.mybatis.pojo用来存放java代码。然后在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.mybatis.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="zx5983736"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/pojo/Category.xml"/>
</mappers>
</configuration>
在com.mybatis.pojo包下创建Category类,包含一个int类型的id以及一个String类型的name成员变量
Category.java
package com.mybatis.pojo;
public class Category {
public String name;
public int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
接着继续在此包下配置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.mybatis.pojo">
<insert id="addCategory" parameterType="Category" >//增加操作
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" >//删除操作
delete from category_ where id= #{id}
</delete>
<select id="getCategory" parameterType="_int" resultType="Category">//查询操作
select * from category_ where id= #{id}
</select>
<update id="updateCategory" parameterType="Category" >//修改操作
update category_ set name=#{name} where id=#{id}
</update>
<select id="listCategory" resultType="Category">//展示全部
select * from category_
</select>
</mapper>
创建TestMybatis.java用于对程序进行测试,依照控制台提示完成相关操作。
package com.mybatis.pojo;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
int i = 0;
Scanner scanner = new Scanner(System.in);
while(i!=6) {
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
System.out.println("请输入你要进行的操作:1.添加 2.删除 3.查询 4.修改 5.查看所有 6.退出");
i = scanner.nextInt();
int id;
String name;
switch(i) {
case 1:
System.out.println("请输入添加的水果名称");
name = scanner.next();
addFruit(session, name);
break;
case 2:
System.out.println("要删除的水果id");
id = scanner.nextInt();
deleteFruit(session, id);
break;
case 3:
System.out.println("请输入要查询的id");
id = scanner.nextInt();
getFruit(session, id);
break;
case 4:
System.out.println("请输入要添加的id、名称");
id = scanner.nextInt();
name = scanner.next();
updateFruit(session, id, name);
break;
case 5:
listAll(session);
case 6:
break;
default:
break;
}
}
}
private static void addFruit(SqlSession session, String name) {
Category c = new Category();
c.setName(name);
session.insert("addCategory",c);
listAll(session);
session.commit();
session.close();
}
private static void deleteFruit(SqlSession session, int id) {
Category c = new Category();
c.setId(id);
session.delete("deleteCategory",c);
listAll(session);
session.commit();
session.close();
}
private static void updateFruit(SqlSession session, int id, String name) {
Category c= session.selectOne("getCategory",id);
c.setName(name);
session.update("updateCategory",c);
listAll(session);
session.commit();
session.close();
}
private static void getFruit(SqlSession session, int id) {
Category c= session.selectOne("getCategory",id);
System.out.println(c.getName());
// 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.getId()+c.getName());
}
}
}
至此,我们就完成了Mybatis的简单的增删查改操作。