jdbc.properties
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/easier?useUnlcode=true&characterEncoding=UTF-8 username=root password=123456
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <properties resource="jdbc.properties"/> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driverClass}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> </dataSource> </transactionManager> <sqlMap resource="Student.xml"/> </sqlMapConfig>
Student.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Student"> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias="student" type="org.ibatis.bean.Student" /> <insert id="addStudent" parameterClass="student" > <!-- oracle database sequence <selectKey keyProperty="id" resultClass="int"> select studentPKSequence.nextVal from dual </selectKey> --> insert into student(sname,sex,birthday,score) values (#sname#,#sex#,#birthday#,#score#) </insert> <delete id="deleteStudentById" parameterClass="int" > delete from student where id=#id# </delete> <delete id="deleteStudentByIdAndBySName" parameterClass="student" > delete from student where id=#id# and sname=#sname# </delete> <!-- 这个#sname# 两边不要加 '' --> <update id="updateStudent" parameterClass="student" > update student set sname=#sname#, sex=#sex#, birthday=#birthday#, score=#score# where id=#id# </update> <select id="queryAllStudent" resultClass="student" > select * from student; </select> <!-- 这个like 后面一定要加 '' --> <select id="queryAllStudentBySname" resultClass="student" parameterClass="String" > select * from student where sname like '%$sname$%'; </select> <select id="queryStudent" resultClass="student" parameterClass="int" > select * from student where id=#id# </select> </sqlMap>
Student.java
/** * */ package org.ibatis.bean; import java.util.Date; /** * @author * */ public class Student { private Integer id; private String sname; private String sex; private Date birthday; private float score; public Student(){ } /** * @param sname * @param sex * @param birthday * @param score */ public Student(String sname, String sex, Date birthday, float score) { this.sname = sname; this.sex = sex; this.birthday = birthday; this.score = score; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } @Override public String toString() { String contact="id: "+id+"\t" + "sname: "+sname+"\t" + "sex: "+sex+"\t" + "birthday: "+birthday+"\t" + "score: "+score+"\t"; return contact; } }
IStudentDao.java
/** * */ package org.ibatis.dao; import java.util.List; import org.ibatis.bean.Student; /** * @author * */ public interface IStudentDao { public void addStudent(Student student); public void deleteStudentById(int id); public void updateStudent(Student student); public List<Student> queryAllStudent(); public List<Student> queryAllStudentBySname(String sname); public Student queryStudent(int id); public void deleteStudentByIdAndBySName(int id,String sname); }
IStudentDaoImpl.java
/** * */ package org.ibatis.dao; import java.sql.SQLException; import java.util.List; import org.ibatis.bean.Student; import org.ibatis.util.SQLUtil; /** * @author * */ public class IStudentDaoImpl extends SQLUtil implements IStudentDao { /** * */ @Override public void addStudent(Student student) { try { System.out .println(this.getSqlMapClient().insert("addStudent", student));; } catch (SQLException e) { e.printStackTrace(); } } /** * */ @Override public void deleteStudentById(int id) { try { System.out .println(this.getSqlMapClient().delete("deleteStudentById", id)); } catch (SQLException e) { e.printStackTrace(); } } /** * */ @Override public List<Student> queryAllStudent() { List<Student> students=null; try { students=(List<Student> )this.getSqlMapClient().queryForList("queryAllStudent"); } catch (SQLException e) { e.printStackTrace(); } return students; } /** * */ @Override public List<Student> queryAllStudentBySname(String sname) { List<Student> students=null; try { students=(List<Student> )this.getSqlMapClient().queryForList("queryAllStudentBySname",sname); } catch (SQLException e) { e.printStackTrace(); } return students; } /** * */ @Override public Student queryStudent(int id) { Student student=null; try { student=(Student)this.getSqlMapClient().queryForObject("queryStudent",id ); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return student; } /** * */ @Override public void updateStudent(Student student) { try { System.out .println(this.getSqlMapClient().update("updateStudent", student));; } catch (SQLException e) { e.printStackTrace(); } } /** * */ @Override public void deleteStudentByIdAndBySName(int id, String sname) { try { Student student=new Student(); student.setId(id); student.setSname(sname); System.out .println(this.getSqlMapClient().delete("deleteStudentByIdAndBySName", student)); } catch (SQLException e) { e.printStackTrace(); } } }
SQLUtil.java
/** * */ package org.ibatis.util; import java.io.IOException; import java.io.Reader; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; /** * @author * */ public abstract class SQLUtil { private static SqlMapClient sqlMapClient=null; static{ Reader reader; try { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader); } catch (IOException e) { System.out.println("加载SqlMapConfig失败!"); e.printStackTrace(); } } public static SqlMapClient getSqlMapClient(){ return sqlMapClient; } }
StudentTest.java
/** * */ package junit.test; import java.util.Date; import java.util.List; import org.ibatis.bean.Student; import org.ibatis.dao.IStudentDaoImpl; import org.junit.BeforeClass; import org.junit.Test; /** * @author * */ public class StudentTest { private static IStudentDaoImpl studentDao=null; /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { studentDao=new IStudentDaoImpl(); } @Test public void addStudent() { Student student=new Student("lisan2","男",new Date(),45); studentDao.addStudent(student); } @Test public void deleteStudentById() { studentDao.deleteStudentById(3); } @Test public void deleteStudentByIdAndBySName() { studentDao.deleteStudentByIdAndBySName(2,"lisan44"); } @Test public void updateStudent() { Student student=new Student("lisan44","女",new Date(),80); student.setId(2); studentDao.updateStudent(student); } @Test public void queryAllStudent() { List<Student> students=studentDao.queryAllStudent(); for(Student student:students){ System.out.println(student); } } @Test public void queryAllStudentBySname() { //如果里面不写成'%$sname$%'; //也可以在外面写成'%name%' List<Student> students=studentDao.queryAllStudentBySname("l"); for(Student student:students){ System.out.println(student); } } @Test public void queryStudent() { Student student=studentDao.queryStudent(2); System.out.println(student); } }
ibatis的好处与jdbc比较
1.简化了jdbc的打开和关闭连接
2.简单
3.sql代码和java中的代码分离
4.简化了项目分工
5.增强移植性
缺点
1.只能带一个参数数量,倒是可以带对象啊
2.要多写sql语句