同一个包下:com.com
数据库简要说明:
数据库名:ibatisTest 用户名:sa 密码:yyy
字段有:id(int),sname(varchar),birth(Date),score(float)
利用的是sqlserver2008
1.IStudentDAO.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.com; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Insert; 6 public interface IStudentDAO { 7 public void insert(Student student); //插入 8 public void update(Student student);//修改 9 public void delete(String sname);//删除 10 public int countAll();//计算总的条目数 11 public List<Student> selectAll();//得到所有数据 12 public Student findBySname(String sname);//依据单一字段查找 13 14 }
2.IStudentDAOTest.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.com; 2 import java.io.Reader; 3 import java.text.SimpleDateFormat; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.junit.Test; 12 public class IStudentDAOTest { 13 @Test 14 public void IStudentDAOTest()throws Exception 15 { 16 String resource = "com/com/mybatis-config.xml"; 17 Reader reader = Resources.getResourceAsReader(resource); 18 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); 19 SqlSessionFactory factory = builder.build(reader); 20 21 // factory.getConfiguration().addMapper(IStudentDAO.class);//利用注解要加上这句是注册,否则不用加 22 SqlSession session = factory.openSession(); 23 IStudentDAO iStudentDAO = session.getMapper(IStudentDAO.class); 24 25 //1.insert: 26 //(错)不可为何不能让接口函数是public int insert(Student student);会出现如下 27 //(错)org.apache.ibatis.binding.BindingException: 28 //(错)映射法'com.com.IStudentDAO.insert试图从一种原始的返回类型(int)方法返回null。 29 //(错)只能用void类型 30 //上面的说法是错的,是我在mapper.xml中写错了,本来是<insert>,让我写成了<selcet> 31 //经我验证可以用int也可以void。 32 33 //但也可以知道用<select>来插入,那样就把接口中的函数定义为void型 34 Student student=new Student(); 35 student.setId(17); 36 student.setSname("xiaoming"); 37 student.setBirth(java.sql.Date.valueOf("2012-9-8")); 38 student.setScore(89); 39 iStudentDAO.insert(student); 40 41 //2.update 42 /*Student dStudent=new Student(); 43 dStudent.setSname("dada2"); 44 dStudent.setId(16); 45 dStudent.setBirth(java.sql.Date.valueOf("2013-7-1")); 46 dStudent.setScore(48); 47 iStudentDAO.update(dStudent);*/ 48 49 //3.delete 50 /*iStudentDAO.delete("dada2");*/ 51 52 //1.记录的条数: 53 /*iStudentDAO.countAll(); 54 System.out.println("记录条数:"+iStudentDAO.countAll()); */ 55 //2.所有记录的列举: 56 /*List<Student> students=iStudentDAO.selectAll(); 57 Iterator<Student> iter=students.iterator(); 58 while(iter.hasNext()) 59 { 60 Student st=iter.next(); 61 //编号与数据库中的id不一样就会全为0,而不是具体的数字。 62 System.out.println("编号:"+st.getId()+" 学生名:"+st.getSname()+"出生日期:"+st.getBirth()+"成绩:"+st.getScore()); 63 }*/ 64 //4.findBySname 65 /* Student findStudent=iStudentDAO.findBySname("caocao"); 66 System.out.println(findStudent.getBirth());*/ 67 68 69 session.commit(); 70 session.close(); 71 72 } 73 74 75 }
3.Student.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ackage 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 36 37 }
4.mybatis-config.properties
5.mybatis-config.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="com/com/mybatis-config.properties"/> 7 <environments default="development"> 8 <environment id="development"> 9 <transactionManager type="JDBC" /> 10 <dataSource type="POOLED"> 11 <property name="driver" value="${driver}"/> 12 <property name="url" value="${url}"/> 13 <property name="username" value="${username}"/> 14 <property name="password" value="${password}"/> 15 </dataSource> 16 </environment> 17 </environments> 18 <mappers> 19 <mapper resource="com/com/StudentMapper.xml" /> 20 </mappers> 21 </configuration>
6.StudentMapper.xm
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.com.IStudentDAO"> 5 <select id="countAll" resultType="int"> 6 select count(*) c 7 from student; 8 </select> 9 <select id="selectAll" resultType="com.com.Student"> 10 select * 11 from student 12 order by sname asc 13 14 </select> 15 16 <insert id="insert" parameterType="com.com.Student"> 17 insert into student(id,sname,birth,score) 18 values(#{id},#{sname},#{birth},#{score}) 19 </insert> 20 21 <update id="update" parameterType="com.com.Student"> 22 update student 23 set sname=#{sname},birth=#{birth},score=#{score} 24 where id=#{id} 25 </update> 26 27 <delete id="delete" parameterType="String"> 28 delete from student 29 where sname=#{sname} 30 </delete> 31 32 <select id="findBySname" parameterType="String" resultType="com.com.Student"> 33 select * 34 from student 35 where sname=#{sname} 36 </select> 37 38 39 40 41 </mapper>