你的位置:首页 > Java教程

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


属性文件:SqlMap.properties

1 driver=com.mysql.jdbc.Driver2 url=jdbc:mysql://localhost:3306/ibatis3 username=root4 password=gys

SqlMapconfig.

<??><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN""http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig><!-- 引用jdbc属性的配置文件 -->  <properties resource="com/iflytek/entity/SqlMap.properties" />  <!-- 使用jdbc的事务管理 -->  <transactionManager type="JDBC">  <dataSource type="SIMPLE">    <property name="JDBC.Driver" value="${driver}" />    <property name="JDBC.ConnectionURL" value="${url}" />    <property name="JDBC.Username" value="${username}" />    <property name="JDBC.Password" value="${password}"/>  </dataSource>  </transactionManager>  <!-- 这里可以写多个实体的映射文件 -->  <sqlMap resource="com/iflytek/entity/Student. /></sqlMapConfig>

Student.

 1 <??> 2 <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 3 "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 4 <sqlMap> 5   <!-- 通过typeAlias使得我们在下面使用Student实体类的时候不需要写包名 --> 6   <typeAlias alias="Student" type="com.iflytek.entity.Student"/> 7   <!-- id表示select里的sql语句,resultClass表示返回结果的类型 --> 8   <select id="selectAllStudent" resultClass="Student"> 9     select * from tb1_student10   </select>11   <select id="selectStudentById" parameterClass="int" resultClass="Student">12   select * from tb1_student where id=#id#13   </select>14   <!-- 注意这里的resultClass类型,使用Student类型取决于queryForList还是queryForObject -->15   <select id="selectStudentByName" parameterClass="String" resultClass="Student">16     select name,birth,score from tb1_student where name like '%$name$%'17   </select>18   <insert id="addStudent" parameterClass="Student">19     insert into tb1_student (name,birth,score) values (#name#,#birth#,#score#)20     <selectKey resultClass="int" keyProperty="id">21       select @@identity as inserted22     </selectKey>23   </insert>24   <delete id="deleteStudentById" parameterClass="int">25     delete from tb1_student where id=#id#26   </delete>27   <update id="updateStudent" parameterClass="Student">28     update tb1_student set name=#name#,birth=#birth#,score=#score# where id=#id#29   </update>30 </sqlMap>

Student.java

 1 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.java

 1 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<Student> selectAllStudent();24   25   /*26    * 根据学生姓名模糊查询学生信息27   */28   public List<Student> selectStudentByName(String name);29   /*30    * 根据学生id查询学生信息31   */32   public Student selectStudentById(int id);33   34 }

StudentDaoImpl.java

 1 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<Student> selectAllStudent() { 75     List<Student> 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<Student> selectStudentByName(String name) { 86     List<Student> 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.java

 1 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<Student> list=studentDaoImpl.selectStudentByName("四");29     for(Student student:list){30       System.out.println(student);31     }32     33     //查询所有34     List<Student> 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 }