mybatis增删改查

一,使用MyBatis实现条件增删改查:
1,在java中创建一个Web Project:
在这里插入图片描述
2,在把配置文件放在WEB-INF的lib里面
在这里插入图片描述
3,在创建resources来连接数据库库:
在这里插入图片描述
创建mybatus-confing和database.properties就不会乱码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=utf-8 username=自己的账号 password=自己的密码 1 2 3 4 4,在src里面写还好要用的dao,实体类,测试类: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190920110929967.png) 5,Dao里面的GoodsMapper.xml是给来写sql语句的GoodsMapper.java是抽象类写方法的:

(GoodsMapper.java)
public interface GoodsMapper {
public Listchaxun();//查询
public int delete(int gid );//删除
public int update(Goods xg);//修改
public int insert(Goods xz);//新增
//模糊查询
public Listfuzzy(@Param(“gid”)int gid,@Param(“gname”)String gname,@Param(“gprice”)double gprice ,@Param(“gcolor”)String gcolor,@Param(“gscore”)int gscore);
public List page(@Param(“i”)int gegin,@Param(“j”)int size);//分页
}
1
2
3
4
5
6
7
8
9
10
(GoodsMapper.xml)

select * from goods delete from goods where gid=#{gid} UPDATE goods SET gname=#{gname},gprice=#{gprice},gcolor=#{gcolor},gscore=#{gscore} WHERE gid=#{gid} INSERT INTO goods (gname,gprice,gcolor,gscore) VALUES(#{gid},#{gname},#{gprice},#{gcolor},#{gscore}) select * from goods
	<if test="gname!=''">
	 gname like concat('%',#{gname},'%')
	</if>
	<if test="gprice!=0">
	and gprice=#{gprice}
	</if>
	<if test="gcolor!=''">
	and gcolor like  concat('%',#{gcolor},'%')
	</if>
	<if test="gscore!=0">
	and gscore=#{gscore}
	</if>
	</where>
select * from goods LIMIT #{i},#{j}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
6,实体类entity:

public class Goods {
private int gid;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public double getGprice() {
return gprice;
}
public void setGprice(int gprice) {
this.gprice = gprice;
}
public String getGcolor() {
return gcolor;
}
public void setGcolor(String gcolor) {
this.gcolor = gcolor;
}
public int getGscore() {
return gscore;
}
public void setGscore(int gscore) {
this.gscore = gscore;
}
private String gname;
private int gprice;
private String gcolor;
private int gscore;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
7,最后一步是测试类写代码:

public class Test {

/**
 * @param args
 */
static String path="goods.xml";
static InputStream is=null;
static SqlSessionFactory factory=null;
static SqlSession session=null;
static int count=0;
public static void main(String[] args) {
	// TODO Auto-generated method stub
		do {
			Scanner input=new Scanner(System.in);
			System.out.println("1.查询所有");
			System.out.println("2.删除");
			System.out.println("3.修改");
			System.out.println("4.添加");
			System.out.println("5.模糊查询");
			System.out.println("6.分页");
			System.out.println("0.退出");
			System.out.println("请输入");
			int a=input.nextInt();
			switch (a) {
			case 1:
				getAll();
				break;
			case 2:
				del();
				break;
			case 3:
				xiu();
				break;
			case 4:
				add();
				break;
			case 5:
				mohu();
				break;
			case 6:
				fenye();
				break;
			case 0:
				System.exit(0);
				break;
			default:
				System.out.println("输入错误");
				break;
			}
		} while (true);
		
	}
	public static void getAll(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			List<Goods>list=a.chaxun();
			for (Goods goods : list) {
				System.out.println(goods.getGid()+"\t"+goods.getGname()+"\t"+goods.getGprice()+"\t"+goods.getGcolor()+"\t"+goods.getGscore());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void del(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			Scanner input=new Scanner(System.in);
			System.out.println("请输入要删除的编号");
			int b=input.nextInt();
			count=a.delete(b);
			session.commit();
			if (count>0) {
				System.out.println("删除成功");
			}else{
				System.out.println("删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void xiu(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			Scanner input=new Scanner(System.in);
			Goods b=new Goods();
			System.out.println("请输入要修改的数据");
			b.setGid(input.nextInt());
			System.out.println("请输入新的姓名");
			b.setGname(input.next());
			System.out.println("请输入新的价格");
			b.setGprice(input.nextInt());
			System.out.println("请输入新的颜色");
			b.setGcolor(input.next());
			System.out.println("请输入新的评分");
			b.setGscore(input.nextInt());
			count=a.update(b);
			session.commit();
			if (count>0) {
				System.out.println("修改成功");
			}else {
				System.out.println("修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void add(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			Scanner input=new Scanner(System.in);
			Goods b=new Goods();
			System.out.println("请输入姓名");
			b.setGname(input.next());
			System.out.println("请输入价格");
			b.setGprice(input.nextInt());
			System.out.println("请输入颜色");
			b.setGcolor(input.next());
			System.out.println("请输入评分");
			b.setGscore(input.nextInt());
			count=a.insert(b);
			session.commit();
			if (count>0) {
				System.out.println("添加成功");
			}else {
				System.out.println("添加失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void mohu(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			Scanner input=new Scanner(System.in);
			System.out.println("请输入你的ID:");
			int id=input.nextInt();
			System.out.println("请输入要查询的姓名");
			String b=input.next();
			System.out.println("请输入要查询的价格");
			int c=input.nextInt();
			System.out.println("请输入要查询的颜色");
			String d=input.next();
			System.out.println("请输入要查询的评分");
			int e=input.nextInt();
			List<Goods>list=a.fuzzy(id, b, c, d, e);
			for (Goods goods : list) {
				System.out.println(goods.getGid()+"\t"+goods.getGname()+"\t"+goods.getGprice()+"\t"+goods.getGcolor()+"\t"+goods.getGscore());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void fenye(){
		try {
			is=Resources.getResourceAsStream(path);
			factory=new SqlSessionFactoryBuilder().build(is);
			session=factory.openSession();
			GoodsMapper a=session.getMapper(GoodsMapper.class);
			Scanner input=new Scanner(System.in);
			System.out.println("请输入要页数");
			int b=input.nextInt();
			System.out.println("请输入个数");
			int c=input.nextInt();
			List<Goods>list=a.page((b-1)*c, c);
			for (Goods goods : list) {
				System.out.println(goods.getGid()+"\t"+goods.getGname()+"\t"+goods.getGprice()+"\t"+goods.getGcolor()+"\t"+goods.getGscore());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

————————————————
版权声明:本文为CSDN博主「weixin_45650198」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45650198/article/details/101050575

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值