欢迎访问 生活随笔!

ag凯发k8国际

当前位置: ag凯发k8国际 > 运维知识 > 数据库 >内容正文

数据库

c ibatis mysql实例-ag凯发k8国际

发布时间:2024/10/14 数据库 37 豆豆
ag凯发k8国际 收集整理的这篇文章主要介绍了 c ibatis mysql实例_[java教程]java程序中的ibatis连接mysql的基本实例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

[java教程]java程序中的ibatis连接mysql的基本实例

0 2015-03-16 00:01:32

属性文件:sqlmap.properties1 driver=com.mysql.jdbc.driver2 url=jdbc:mysql://localhost:3306/ibatis3 username=root4 password=gys

sqlmapconfig.?> 4 5 6 7 8 9 select * from tb1_student10 11 12 select * from tb1_student where id=#id#13 14 15 16 select name,birth,score from tb1_student where name like '%$name$%'17 18 19 insert into tb1_student (name,birth,score) values (#name#,#birth#,#score#)20 21 select @@identity as inserted22 23 24 25 delete from tb1_student where id=#id#26 27 28 update tb1_student set name=#name#,birth=#birth#,score=#score# where id=#id#29 30

student.java1 package com.iflytek.entity; 2 3 import java.sql.date; 4 5 public class student { 6 private int id; 7 private string name; 8 private date birth; 9 private float score;10 11 public student(){}12 13 public int getid() {14 return id;15 }16 public void setid(int id) {17 this.id = id;18 }19 public string getname() {20 return name;21 }22 public void setname(string name) {23 this.name = name;24 }25 public date getbirth() {26 return birth;27 }28 public void setbirth(date birth) {29 this.birth = birth;30 }31 public float getscore() {32 return score;33 }34 public void setscore(float score) {35 this.score = score;36 }37 @override38 public string tostring(){39 return "id=" id "\t name" name "\t ajor=" birth "\t score=" score "\n";40 }41 42 }

istudentdao.java1 package com.iflytek.dao; 2 3 import java.util.list; 4 5 import com.iflytek.entity.student; 6 7 public interface istudentdao { 8 /* 9 * 添加学生信息10 */11 public boolean addstudent(student student);12 /*13 * 根据id删除学生信息14 */15 public boolean deletestudentbyid(int id);16 /*17 * 更新学生信息18 */19 public boolean updatestudent(student student);20 /*21 * 查询全部学生信息22 */23 public list selectallstudent();24 25 /*26 * 根据学生姓名模糊查询学生信息27 */28 public list selectstudentbyname(string name);29 /*30 * 根据学生id查询学生信息31 */32 public student selectstudentbyid(int id);33 34 }

studentdaoimpl.java1 package com.iflytek.daoimpl; 2 3 import java.io.ioexception; 4 import java.io.reader; 5 import java.sql.sqlexception; 6 import java.util.list; 7 8 import com.ibatis.common.resources.resources; 9 import com.ibatis.sqlmap.client.sqlmapclient; 10 import com.ibatis.sqlmap.client.sqlmapclientbuilder; 11 import com.iflytek.dao.istudentdao; 12 import com.iflytek.entity.student; 13 14 public class studentdaoimpl implements istudentdao { 15 private static sqlmapclient sqlmapclient = null; 16 17 // 读取配置文件 18 static { 19 try { 20 reader reader = resources.getresourceasreader("com/iflytek/entity/sqlmapconfig.); 21 sqlmapclient = sqlmapclientbuilder.buildsqlmapclient(reader); 22 reader.close(); 23 } catch (ioexception e) { 24 e.printstacktrace(); 25 } 26 } 27 28 public boolean addstudent(student student) { 29 object object = null; 30 boolean flag = false; 31 try { 32 object = sqlmapclient.insert("addstudent", student); 33 system.out.println("添加学生信息的返回值:" object); 34 } catch (sqlexception e) { 35 e.printstacktrace(); 36 } 37 if (object != null) { 38 flag = true; 39 } 40 return flag; 41 } 42 43 public boolean deletestudentbyid(int id) { 44 boolean flag = false; 45 object object = null; 46 try { 47 object = sqlmapclient.delete("deletestudentbyid", id); 48 system.out.println("删除学生信息的返回值:" object ",这里返回的是影响的函数"); 49 } catch (sqlexception e) { 50 // todo auto-generated catch block 51 e.printstacktrace(); 52 } 53 if (object != null) { 54 flag = true; 55 } 56 return flag; 57 } 58 59 public boolean updatestudent(student student) { 60 boolean flag = false; 61 object object = false; 62 try { 63 object = sqlmapclient.update("updatestudent", student); 64 system.out.println("更新学生信息的返回值:" object ",返回影响的行数"); 65 } catch (sqlexception e) { 66 e.printstacktrace(); 67 } 68 if(object!=null){ 69 flag=true; 70 } 71 return flag; 72 } 73 74 public list selectallstudent() { 75 list students=null; 76 try { 77 students=sqlmapclient.queryforlist("selectallstudent"); 78 } catch (sqlexception e) { 79 // todo auto-generated catch block 80 e.printstacktrace(); 81 } 82 return students; 83 } 84 85 public list selectstudentbyname(string name) { 86 list students=null; 87 try { 88 students=sqlmapclient.queryforlist("selectstudentbyname", name); 89 } catch (sqlexception e) { 90 e.printstacktrace(); 91 } 92 return students; 93 } 94 95 public student selectstudentbyid(int id) { 96 student student=null; 97 try { 98 student=(student)sqlmapclient.queryforobject("selectstudentbyid",id); 99 } catch (sqlexception e) { 100 e.printstacktrace();101 }102 return student;103 }104 105 }

testibatis.java1 package com.iflytek.test; 2 3 import java.sql.date; 4 import java.util.list; 5 6 import com.iflytek.daoimpl.studentdaoimpl; 7 import com.iflytek.entity.student; 8 9 public class testibatis {10 public static void main(string[] args) {11 studentdaoimpl studentdaoimpl=new studentdaoimpl();12 13 //测试插入14 student addstudent=new student();15 addstudent.setname("李四");16 addstudent.setbirth(date.valueof("2011-09-02"));17 addstudent.setscore(88);18 system.out.println(studentdaoimpl.addstudent(addstudent)); 19 20 addstudent.setname("李四2");21 addstudent.setbirth(date.valueof("1990-09-02"));22 addstudent.setscore(98);23 system.out.println(studentdaoimpl.addstudent(addstudent));24 //根据id查询25 system.out.println(studentdaoimpl.selectstudentbyid(2));26 27 //根据姓名查询28 list list=studentdaoimpl.selectstudentbyname("四");29 for(student student:list){30 system.out.println(student);31 }32 33 //查询所有34 list list=studentdaoimpl.selectallstudent();35 for(student student:list){36 system.out.println(student);37 }38 39 //更新信息40 student updatestudent=new student();41 updatestudent.setid(1);42 updatestudent.setname("李四1 ");43 updatestudent.setbirth(date.valueof("1990-09-07"));44 updatestudent.setscore(24);45 system.out.println(studentdaoimpl.updatestudent(updatestudent));46 47 //删除数据48 boolean b=studentdaoimpl.deletestudentbyid(1);49 system.out.println("删除结果:" b);50 51 } 52 }

本文网址:http://www.shaoqun.com/a/112243.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系ag凯发k8国际:admin@shaoqun.com。

java

0

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是ag凯发k8国际为你收集整理的c ibatis mysql实例_[java教程]java程序中的ibatis连接mysql的基本实例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得ag凯发k8国际网站内容还不错,欢迎将ag凯发k8国际推荐给好友。

网站地图