Mybatis基础增删改查、模糊查询、多条件查询
1、新建测试数据库,根据实体类属性创建
2、实体类
package com.entity;
/**
* 数据库实体类
* Created by lvjun on 2018-04-12.
*/
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//显示中文
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3、配置文件Category.xml
delete from category where id= #{id}
update category set name=#{name} where id=#{id}
insert into category ( name ) values (#{name})
5、测试类
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 查询列表
* Created by lvjun on 2018-04-12.
*/
public class TestSelectList {
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();
List cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
}
}
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 查询一条记录
* Created by lvjun on 2018-04-12.
*/
public class TestSelectOne {
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 = session.selectOne("getCategory", 3);
System.out.println(c.getId()+" "+c.getName());
session.commit();
session.close();
}
}
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 模糊查询
* Created by lvjun on 2018-04-12.
*/
public class TestSelectLike {
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();
List cs = session.selectList("listCategoryByName", "win");
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
session.commit();
session.close();
}
}
package com.lvjun;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.entity.Category;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* 多条件查询
* Created by lvjun on 2018-04-12.
*/
public class TestSelectToLike {
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();
Map params = new HashMap<>();
params.put("id", 3); //条件查询
params.put("name", "wi");
List cs = session.selectList("listCategoryByIdAndName", params);
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
session.commit();
session.close();
}
}
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 修改记录
* Created by lvjun on 2018-04-12.
*/
public class TestUpdate {
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 = session.selectOne("getCategory", 2);
c.setName("Android 8");
session.update("updateCategory", c);
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
}
}
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 插入一条记录
* Created by lvjun on 2018-04-12.
*/
public class TestInsert {
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("新增加的数据");
session.insert("addCategory", c);
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
}
}
package com.lvjun;
import com.entity.Category;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 删除一条记录
* Created by lvjun on 2018-04-12.
*/
public class TestDeleteOne {
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.setId(6);
session.delete("deleteCategory", c);
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getId()+" "+c.getName());
}
}
}