哈喽~大家好呀,这篇来看看使用的常用操作。
个人主页:个人主页
系列专栏:【JAVAEE框架】
与这篇相关的文章:
【JAVAEE框架】Mybatis项目起步讲解 【JAVAEE框架】Mybatis项目起步讲解_程序猿追的博客-CSDN博客 JAVAWEB开发】基于Java+Servlet+Ajax+jsp网上购物系统设计实现 【JAVAWEB开发】基于Java+Servlet+Ajax+jsp网上购物系统设计实现_程序猿追的博客-CSDN博客 Servlet 架构思路(MVC) Servlet 架构思路(MVC)_程序猿追的博客-CSDN博客
目录
一、准备工作
二、查询所有用户信息
三、按 xx 进行查询
四、模糊查询
五、增删改操作
六、联合查询
七、分页查询
如何新建 Mybatis 项目,可以看看上一篇的讲解——【JAVAEE框架】Mybatis项目起步讲解
准备 tb_brand 表,字段id(id)、品牌名(brand_name)、公司名(company_name)、排序顺序(ordered)、描述信息(description)、状态(status)。
项目结构在上篇有所讲解,这里就不重复了,直接上代码讲解。
我们常见的 xx 管理系统最常见的操作之一,查看所有的记录。
mapper
IBrandDao
public interface IBrandDao {
public List getUserList() throws Exception;
}
接着上次的讲,上次我们讲到SqlSession是dao与db建立的一次会话,就像 servlet 的 session 一样,但,有没有想过,我们以后一个项目有成千上万的访问者访问页面(获取 db 的信息),那么岂不是要建立成千上万次会话?这是很不合理的。所以就有了 MyBatisUtil,就像 jdbc 的 Util 一样
MyBatisUtil
public class MyBatisUtil {
private static SqlSessionFactory factory;
static {
try {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
factory = builder.build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession openSession(){
return factory.openSession();
}
public static SqlSession openSession(boolean autoCommit){
return factory.openSession(autoCommit);
}
}
实体类(Brand)
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brand_name;
// 企业名称
private String company_name;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
private Integer status;
public Brand() {
}
public Brand(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {
this.id = id;
this.brand_name = brand_name;
this.company_name = company_name;
this.ordered = ordered;
this.description = description;
this.status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brand_name='" + brand_name + '\'' +
", company_name='" + company_name + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
测试
public class TestMyBatis {
@Test
public void test01() throws Exception {
IBrandDao userDao = new BrandDao();
List userList = userDao.getUserList();
for (Brand brand : userList) {
System.out.println(brand);
}
}
}
效果
常见的按照姓名查询、按照 id 查询等,这里就演示 id 查询
mapper
IBrandDao
这里就全部给了,这小节就不多写 IBrandDao 了
public interface IBrandDao {
public List getBrandList();
public Brand getBrandById(String id);
public List getBrandListByCondition(Brand conn);
public List getBrandListByCondition(Map conn);
public Integer addBrand(Brand brand);
public Integer updateBrand(Brand brand);
public Integer deleteBrand(Brand brand);
public Integer getBrandByCondition(Brand brand);
}
测试
@Test
public void test02(){
IBrandDao dao = new BrandDao();
Brand brandList = dao.getBrandById("142");
System.out.println(brandList);
}
效果
查询品牌含有8的品牌名
mapper
测试
@Test
public void test03(){
SqlSession sqlSession = MyBatisUtil.openSession();
IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
Brand brand = new Brand();
brand.setBrand_name("8");
// brand.setCompany_name("小米");
List brandList = mapper.getBrandListByCondition(brand);
for (int i = 0; i < brandList.size(); i++) {
System.out.println(brandList.get(i));
}
}
效果
扩:
用法
在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用来解决。
eg:> < 这两个符号
mapper
测试
@Test
public void test04(){
SqlSession sqlSession = MyBatisUtil.openSession();
IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
Brand brand = new Brand(null, "菠萝手机","菠萝",100,"美国有苹果,中国有菠萝",0);
Integer integer = mapper.addBrand(brand);
sqlSession.commit();
System.out.println(integer);
}
@Test
public void test05(){
SqlSession sqlSession = MyBatisUtil.openSession();
IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
Brand brand = new Brand();
brand.setId(191);
brand.setCompany_name("大菠萝手机");
Integer integer = mapper.updateBrand(brand);
sqlSession.commit();
System.out.println(integer);
}
@Test
public void test06(){
SqlSession sqlSession = MyBatisUtil.openSession();
IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
Brand brand = new Brand();
brand.setId(195);
Integer integer = mapper.deleteBrand(brand);
sqlSession.commit();
System.out.println(integer);
}
效果
mapper
IUserDao
public List getUserListByCondition(UserCondition conn);
测试
@Test
public void test01(){
SqlSession sqlSession = MyBatisUtil.openSession();
IUserDao mapper = sqlSession.getMapper(IUserDao.class);
UserCondition conn = new UserCondition();
List userList = mapper.getUserListByCondition(conn);
for (SmbmsUser smbmsUser : userList) {
System.out.println(smbmsUser);
}
sqlSession.close();
}
效果
mapper
IUserDao
public List selectByPage(@Param("begin") int begin, @Param("size") int size);
测试
@Test
public void test01() {
SqlSession sqlSession = MyBatisUtil.openSession();
IUserDao mapper = sqlSession.getMapper(IUserDao.class);
List pageBean = mapper.selectByPage(1, 10);
for (Brand brand : pageBean) {
System.out.println(brand);
}
sqlSession.close();
}
效果
不积跬步无以至千里,趁年轻,使劲拼,给未来的自己一个交代!向着明天更好的自己前进吧!