1、配置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.how2java.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>
2、增加
测试类TestMybatis
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();
Category c = new Category();
c.setName("新增加的Category");
session.insert("addCategory",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());
}
}
}
对应:
<insert id="addCategory" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
3、删除
Category c = new Category();
c.setId(6);
session.delete("deleteCategory",c);
listAll(session);
对应:
<delete id="deleteCategory" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
4、获取
Category c= session.selectOne("getCategory",3);
System.out.println(c.getName());
对应:
<select id="getCategory" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
5、修改
Category c= session.selectOne("getCategory",3);
c.setName("修改了的Category名稱");
session.update("updateCategory",c);
listAll(session);
对应:
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
6、查询所有
listAll(session);
<select id="listCategory" resultType="Category">
select * from category_
</select>
7、模糊查询
Category.xml
<select id="listCategoryByName" parameterType="string" resultType="Category">
select * from category_ where name like concat('%',#{0},'%')
</select>
TestMyBatis类
List<Category> cs = session.selectList("listCategoryByName","cat");
for (Category c : cs) {
System.out.println(c.getName());
}
8、多条件查询
<select id="listCategoryByIdAndName" parameterType="map" resultType="Category">
select * from category_ where id> #{id} and name like concat('%',#{name},'%')
</select>
因为是多个参数,而selectList方法又只接受一个参数对象,所以需要把多个参数放在Map里,然后把这个Map对象作为参数传递进去
Map<String,Object> params = new HashMap<>();
params.put("id", 3);
params.put("name", "cat");
List<Category> cs = session.selectList("listCategoryByIdAndName",params);
for (Category c : cs) {
System.out.println(c.getName());
}