一笑寥寥空万古!天涯目送飞鸿去

单纯的简单的ibatis

架包:sqljdbc4.jar+ibatis-2.3.4.jar+junit4.jar

数据库简要说明:

数据库名:ibatisTest 用户名:sa 密码:yyy

字段有:id(int),sname(varchar),birth(Date),score(float)

利用的是sqlserver2008

1.同一个包下:com.com

2.IbatisTest.java

 1 package com.com;
 2 
 3 import java.sql.Date;
 4 import java.util.List;
 5 
 6 import org.junit.Test;
 7 
 8 public class IbatisTest {
 9     @Test
10     public void IbatisTest() throws Exception
11     {
12         IStudentDAO iStudentDAO=new IStudentDAOImpl();
13         //1.查询所有
14         /*System.out.println("测试查询所有:");
15         List<Student> students=iStudentDAO.selectAllStudent();
16         for(Student student:students)
17             System.out.println(student.toString());*/
18         //2.插入
19         /*Student student=new Student();
20         student.setId(311);
21         student.setSname("依依");
22         student.setBirth(Date.valueOf("2013-5-5"));
23         student.setScore(100);
24         iStudentDAO.addStudent(student);*/
25         //3.修改
26         /*Student student=new Student();
27         student.setId(161);
28         student.setSname("依依");
29         student.setBirth(Date.valueOf("2013-12-12"));
30         iStudentDAO.updateStudent(student);*/
31         //4.删除
32         //iStudentDAO.deleteStudent(31);
33         //5.依名字查询  =查询:sname=#sname# 模糊:'%$sname$%'
34         /*List<Student> students=iStudentDAO.selectStudentByName("依依");
35         for(Student student:students)
36             System.out.println(student.toString());*/
37         //6.依Id查询,这个中唯一的。
38         System.out.println(iStudentDAO.selectStudentById(161));
39     }
40 
41 }
Test代码

 

3.IStudentDAO.java

 1 package com.com;
 2 
 3 import java.util.List;
 4 public interface IStudentDAO {
 5     public void addStudent(Student student);
 6     public void deleteStudent(int id);
 7     public void updateStudent(Student student);
 8     public List<Student> selectAllStudent();
 9     public List<Student> selectStudentByName(String sname);
10     public Student selectStudentById(int id);
11         
12 }
DAO代码

 

4.IStudentDAOImpl.java

 1 package com.com;
 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 
12 public class IStudentDAOImpl implements IStudentDAO {
13     private static SqlMapClient sqlMapClient=null;
14     static{
15         try {
16             Reader reader=Resources.getResourceAsReader("com/com/SqlMapConfig.xml");
17             sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
18             reader.close();
19         } catch (IOException e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         }
23         
24     }
25 
26 
27 
28     public void addStudent(Student student) {
29         try {
30             sqlMapClient.insert("addStudent", student);
31         } catch (SQLException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }
35     }
36 
37     public void deleteStudent(int id) {
38         // TODO Auto-generated method stub
39         try {
40             sqlMapClient.delete("deleteStudent", id);
41         } catch (SQLException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45     }
46 
47     public void updateStudent(Student student) {
48         // TODO Auto-generated method stub
49         try {
50             sqlMapClient.update("updateStudent", student);
51         } catch (SQLException e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55     }
56 
57     public List<Student> selectAllStudent() {
58         List<Student> students=null;
59         try {
60             students=sqlMapClient.queryForList("selectAllStudent");
61         } catch (SQLException e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         
66         
67         return students;
68     }
69 
70     public List<Student> selectStudentByName(String sname) {
71         // TODO Auto-generated method stub
72         List<Student> students=null;
73         try {
74             students= sqlMapClient.queryForList("selectStudentByName",sname);
75         } catch (SQLException e) {
76             // TODO Auto-generated catch block
77             e.printStackTrace();
78         }
79         return students;
80     }
81 
82     public Student selectStudentById(int id) {
83         // TODO Auto-generated method stub
84         Student student=null;
85         try {
86             student=(Student) sqlMapClient.queryForObject("selectStudentById", id);
87         } catch (SQLException e) {
88             // TODO Auto-generated catch block
89             e.printStackTrace();
90         }
91         return student;
92     }
93 
94 }
DAOImpl 代码

 

5.Student.java

 1 package com.com;
 2 
 3 import java.sql.Date;
 4 
 5 public class Student {
 6     private int id=0;
 7     private String sname=null;
 8     private Date birth=null;
 9     private float score=0;
10     
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getSname() {
18         return sname;
19     }
20     public void setSname(String sname) {
21         this.sname = sname;
22     }
23     public Date getBirth() {
24         return birth;
25     }
26     public void setBirth(Date birth) {
27         this.birth = birth;
28     }
29     public float getScore() {
30         return score;
31     }
32     public void setScore(float score) {
33         this.score = score;
34     }
35     public String toString()
36     {
37         return("id="+id+"\tsname="+sname+"\tbirth="+birth+"\tscore="+score);
38     }
39 
40 }
Student实体类代码

 

6.SqlMapConfig.propertyies

7.SqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
 3 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 4 <sqlMapConfig>
 5     <properties resource="com/com/SqlMapConfig.properties" />
 6     <transactionManager type="JDBC">
 7         <dataSource type="SIMPLE">
 8             <property name="JDBC.Driver" value="${driver}" />
 9             <property name="JDBC.ConnectionURL" value="${url}" />
10             <property name="JDBC.Username" value="${username}" />
11             <property name="JDBC.Password" value="${password}" />
12         </dataSource>
13     </transactionManager>
14 
15     <sqlMap resource="com/com/StudentSqlMap.xml"/>
16 </sqlMapConfig>
SqlMapConfig.xml配置

 

8.StudentSqlMap.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 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 alias="Student" type="com.com.Student"/>
 6   <insert id="addStudent" parameterClass="Student">
 7        insert into student(id,sname,birth,score)
 8        <!-- 这里不能#{id}#或#{id},否则会出现no readable property-->
 9                     values(#id#,#sname#,#birth#,#score#)
10   </insert>
11   <update id="updateStudent" parameterClass="Student">
12       update student 
13       set sname=#sname#,birth=#birth#,score=#score#
14       where id=#id#
15   </update>
16   
17   <delete id="deleteStudent" parameterClass="int">
18       delete from
19       student
20       where id=#id#
21   </delete>
22   <select id="selectAllStudent" resultClass="Student">
23        select *
24        from student
25   </select>
26   <select id="selectStudentByName" parameterClass="String" resultClass="Student">
27         select *
28         from student
29         <!-- 相等查询:可以用sname=#sname# 模糊查询:'%$sname$%'-->
30         where sname like '%$sname$%'
31   </select>
32   <select id="selectStudentById" parameterClass="int" resultClass="Student">
33   <!-- resultClass="student"也可以,不区分大小写,但不可用别的 -->
34          select *
35          from student
36          where id=#id#
37   </select>
38    
39    
40     
41     
42    
43 </sqlMap> 
sqlMap配置代码

 

posted @ 2013-07-20 16:18  yyy88168  阅读(189)  评论(0编辑  收藏  举报