一,使用MyBatis实现条件增删改查:
1,在java中创建一个Web Project:
2,在把配置文件放在WEB-INF的lib里面
3,在创建resources来连接数据库库:
创建mybatus-confing和database.properties就不会乱码:
(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)
<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