1.通过Mybatis进行CRUD (创建:Create, 读取:Read,更新:Update,删除: Delete)一套操作,建立在上一个知识点[小白自学]MyBatis技术(一)的基础上进行。
修改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">
<!-- 表示命名空间是com.how2java.pojo,在后续调用sql语句的时候,会用到它 -->
<mapper namespace="com.how2java.pojo">
<!--这条sql语句用id: listCategory 进行标示以供后续代码调用。resultType="Category" 表示返回的数据和Category关联起来,这里本应该使用的是 com.how2java.pojo.Category, 但是因为上一步配置了别名,所以直接使用Category就行了 -->
<select id="listCategory" resultType="Category">
select * from category_
</select>
<!-- CRUD (创建:Create, 读取:Read,更新:Update,删除: Delete) -->
<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>
</mapper>
2.TestMybatis.java
package com.how2java;
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.how2java.pojo.Category;
public class TestMybatis {
public static void main(String[] args) throws IOException {
// 根据配置文件mybatis-config.xml得到sqlSessionFactory 。
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 然后再根据sqlSessionFactory 得到session
SqlSession session = sqlSessionFactory.openSession();
System.out.println("-------新增-------");
Category c = new Category();
c.setName("this is new");
session.insert("addCategory", c);
listAll(session);
session.commit();
System.out.println("---------删除------");
c.setId(1);
session.delete("deleteCategory",c);
listAll(session);
System.out.println("---------根据id找name------");
c= session.selectOne("getCategory",3);
System.out.println("查询的3为:"+c.getName());
System.out.println("---------根据id修改name------");
c.setName("修改了的Category名稱");
session.update("updateCategory",c);
listAll(session);
session.close();
}
/**
* 通过session的selectList方法,调用sql语句listCategory。listCategory这个就是在配置文件Category.xml中那条sql语句设置的id。
* 执行完毕之后,得到一个Category集合,遍历即可看到数据。
* 也可以写成 session.selectList("com.how2java.pojo.listCategory"); 效果一样
*/
private static void listAll(SqlSession session) {
System.out.println("-------查询所有listAll()方法-------");
List<Category> cs = session.selectList("listCategory");
for (Category category : cs) {
System.out.println(category.getName());
}
}
}
3.运行结果
-------新增-------
-------查询所有listAll()方法-------
11
category2
t3
4
5
6
this is new
this is new
this is new
---------删除------
-------查询所有listAll()方法-------
11
category2
t3
4
5
6
this is new
this is new
this is new
---------根据id找name------
查询的3为:t3
---------根据id修改name------
-------查询所有listAll()方法-------
11
category2
修改了的Category名稱
4
5
6
this is new
this is new
this is new
官方的Test
package com.how2java;
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.how2java.pojo.Category;
public class TestMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
listAll(session);
session.commit();
session.close();
}
private static void update(SqlSession session) {
Category c= session.selectOne("getCategory",3);
c.setName("修改了的Category名稱");
session.update("updateCategory",c);
listAll(session);
}
private static void get(SqlSession session) {
Category c= session.selectOne("getCategory",3);
System.out.println(c.getName());
}
private static void delete(SqlSession session) {
Category c = new Category();
c.setId(6);
session.delete("deleteCategory",c);
listAll(session);
}
private static void add(SqlSession session) {
Category c = new Category();
c.setName("新增加的Category");
session.insert("addCategory",c);
listAll(session);
}
private static void listAll(SqlSession session) {
List<Category> cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName());
}
}
}