如果不需要太多复杂的功能,ibatis是能满足你的要求又足够灵活的最简单的解决方案。
ibatis
搭建环境
配置文件
读取配置
基本的CURD操作
模糊查询
自动主键selectKey
建立一个工程目录大致如下:
搭建环境:导入相关的jar包
oracle的jdbc驱动包,ibatis的jar包
配置文件:SqlMap.properties
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0.1:1521:orcl username=ar password=ar
总配置文件: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="com/itcast/SqlMap.properties"/> <transactionManager type="JDBC" commitRequired="false"> <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> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="com/itcast/Student.xml"/> </sqlMapConfig>
<?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"> <!-- 取别名--> <typeAlias alias="Student" type="com.itcast.Student" /> <select id="selectAllStudent" resultClass="Student"> select * from student </select> <select id="selectStudentById" parameterClass="int" resultClass="Student"> select * from student where sid=#sid# </select> <!-- 模糊查询使用$符号 --> <select id="queryStudentByName" parameterClass="String" resultClass="Student">select * from studentwhere sname like '%$sname$%'</select><insert id="insertStudent" parameterClass="Student">insert into student(sid,sname,major,score,birth)values(#sid#,#sname#,#major#,#score#,#birth#)</insert> <insert id="insertStudentBySequence" parameterClass="Student"> <!-- 使用自动主键,使用sequence实现主键自增 --> <selectKey resultClass="int" keyProperty="sid" >select studentPKSequence.nextValfrom dual</selectKey>insert into student(sid,sname,major,birth,score)values(#sid#,#sname#,#major#,#birth#,#score#)</insert><delete id="deleteStudentById" parameterClass="int">delete from studentwhere sid=#sid#</delete><update id="updateStudentById" parameterClass="Student"> update studentset sname=#sname#, major=#major#, birth=#birth#, score=#score#where sid=#sid#</update></sqlMap>
IStudentDAO.java
package com.itcast; import java.util.List; public interface IStudentDAO { public void addSutdent(Student student); public void addStudentBySequence(Student student); public void deleteStudentById(int id); public void updateStudentById(Student student); public List<Student> queryAllStudent(); public List<Student> queryStudentByName(String name); public Student queryStudentById(int id); }
IStudentDAOImpl.java
package com.itcast; import java.io.IOException; import java.io.Reader; import java.math.BigDecimal; import java.sql.Date; import java.sql.SQLException; import java.util.List; import com.ibatis.sqlmap.client.SqlMapClient; public class IStudentDAOImpl implements IStudentDAO { private static SqlMapClient sqlMapClient = null; static { try { Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("com/itcast/SqlMapConfig.xml"); sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void addSutdent(Student student) { try { sqlMapClient.insert("insertStudent", student); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void addStudentBySequence(Student student) { try { sqlMapClient.insert("insertStudentBySequence", student); System.out.println("sid=" + student.getSid()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void deleteStudentById(int id) { try { System.out.println(sqlMapClient.delete("deleteStudentById", id)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void updateStudentById(Student student) { try { System.out.println("更新行数:" + sqlMapClient.update("updateStudentById", student)); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public List<Student> queryAllStudent() { List<Student> studentList = null; try { studentList = sqlMapClient.queryForList("selectAllStudent"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return studentList; } @Override public List<Student> queryStudentByName(String name) { List<Student> list = null; try { list = sqlMapClient.queryForList("queryStudentByName", name); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } @Override public Student queryStudentById(int id) { Student student = null; try { student = (Student)sqlMapClient.queryForObject("selectStudentById",id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return student; } public static void main(String[] args) { IStudentDAO dao = new IStudentDAOImpl(); // for(Student student : dao.queryAllStudent()) // { // System.out.println(student); // } // Student student = dao.queryStudentById(2); // Student student = new Student(); // student.setSid(4); // student.setSname("sname"); // student.setMajor("major"); // student.setScore(new BigDecimal(98)); // //student.setBirth(new Date(System.currentTimeMillis())); // student.setBirth(Date.valueOf("2013-05-30")); // // dao.addSutdent(student); // for(Student stu : dao.queryStudentByName("lei")) // { // System.out.println(stu); // } // dao.deleteStudentById(4); // Student student = new Student(); // student.setSname("wwwwlei"); // student.setMajor("games"); // student.setBirth(Date.valueOf("2011-05-23")); // student.setScore(new BigDecimal(86)); // student.setSid(2); // dao.updateStudentById(student); Student student = new Student(); student.setSname("wwwwlei"); student.setMajor("games"); student.setBirth(Date.valueOf("2011-05-23")); student.setScore(new BigDecimal(88)); dao.addStudentBySequence(student); } }
Student.java
package com.itcast; import java.math.BigDecimal; import java.sql.Date; public class Student { private int sid = 0; private String sname = null; private String major = null; private Date birth = null; private BigDecimal score = BigDecimal.ZERO; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public BigDecimal getScore() { return score; } public void setScore(BigDecimal score) { this.score = score; } @Override public String toString() { String content = "sid="+sid +"\tsname="+sname +"\tmajor="+major +"\tbirth="+birth +"\tscore="+score; return content; } }
CRUD的操作:
借助SqlMapClient中的方法:
queryForObject
queryForList
insert
delete
update
create table student
(
sid number not null,
sname varchar2(20),
major varchar2(20),
birth date,
score number
)
创建序列:
create sequence studentPKSequence start with 1 increment by 1;