ibatis-2.3.3学习
来源:互联网 发布:淘宝详情页和主图视频 编辑:程序博客网 时间:2024/06/07 09:43
工程目录结构如下:
sql语句如下:
/*SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.1.33-community-log : Database - test**********************************************************************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `test`;/*Table structure for table `student` */DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `sid` int(10) unsigned NOT NULL AUTO_INCREMENT, `sname` varchar(30) DEFAULT NULL, `major` varchar(30) DEFAULT NULL, `birth` date DEFAULT NULL, `score` float DEFAULT NULL, PRIMARY KEY (`sid`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;/*Data for the table `student` */insert into `student`(`sid`,`sname`,`major`,`birth`,`score`) values (1,'mary','nv','2013-01-01',90),(2,'tom','nan','2012-01-01',91),(3,'aaa','nan','2011-01-01',66),(4,'mm','nv','2015-11-11',100),(5,'cate','nv','2020-11-11',100);/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
1. Eclipse导入ibatis-2.3.3.720.jar和mysql-connector-java-5.1.28-bin.jar两个jar包。
2. src目录下建立SqlMap.properties文件,内容如下:
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/testusername=rootpassword=3. 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> <properties resource="SqlMap.properties"/> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <!--<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test"/> <property name="JDBC.Username" value="root"/> <property name="JDBC.Password" value=""/> --> <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="Student.xml"/></sqlMapConfig>4. 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><select id="selectAllStudent" resultClass="mybatis.Student">select * from Student</select><select id="selectStudentById" parameterClass="int" resultClass="mybatis.Student">select * from student where sid=#sid#</select><insert id="insertStudent" parameterClass="mybatis.Student"> insert into Student(sid,sname,major,birth,score) values (#sid#,#sname#,#major#,#birth#,#score#)</insert><delete id="deleteStudentById" parameterClass="mybatis.Student"> delete from student where sid=#sid#</delete><update id="updateStudentById" parameterClass="mybatis.Student">update student set sname=#sname#,major=#major#,score=#score#,birth=#birth# where sid=#sid#</update><select id="selectStudentByName" parameterClass="String" resultClass="mybatis.Student">select * from student where sname like '%$sname$%'</select></sqlMap>5. IStudentDAO.java内容如下:
package mybatis;import java.util.List;public interface IStudentDAO{public void addStudent(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);}6. IStudentDAOImpl.java内容如下:
package mybatis;import java.io.IOException;import java.io.Reader;import java.sql.Date;import java.sql.SQLException;import java.util.List;import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;public class IStudentDAOImpl implements IStudentDAO{private static SqlMapClient sqlMapClient = null;static{try{Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);reader.close();}catch (IOException e){e.printStackTrace();}}@Overridepublic void addStudent(Student student){try {sqlMapClient.insert("insertStudent", student);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void addStudentBySequence(Student student) {// TODO Auto-generated method stub}@Overridepublic void deleteStudentById(int id) {// TODO Auto-generated method stubtry {sqlMapClient.delete("deleteStudentById", id);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void updateStudentById(Student student){// TODO Auto-generated method stubtry {sqlMapClient.update("updateStudentById", student);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@SuppressWarnings("unchecked")@Overridepublic List<Student> queryAllStudent(){try {List<Student> studentList = sqlMapClient.queryForList("selectAllStudent");return studentList;} catch (SQLException e) {e.printStackTrace();}return null;}@SuppressWarnings("unchecked")@Overridepublic List<Student> queryStudentByName(String name){// TODO Auto-generated method stubList<Student> studentList = null;try {studentList = sqlMapClient.queryForList("selectStudentByName", name);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return studentList;}@Overridepublic Student queryStudentById(int id){Student student = null;try {student = (Student) sqlMapClient.queryForObject("selectStudentById", id);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return student;}public static void main(String[] args){IStudentDAO dao = new IStudentDAOImpl();/*for(Student student:dao.queryAllStudent()){System.out.println(student);}*///System.out.println(dao.queryStudentById(2));/*Student student = new Student();student.setSid(5);student.setSname("cate");student.setMajor("nv");student.setBirth(Date.valueOf("1999-11-11"));student.setScore(100);dao.addStudent(student);*///dao.deleteStudentById(3);/*Student s = new Student();s.setSid(3);s.setSname("aaa");s.setMajor("nan");s.setScore(66);s.setBirth(Date.valueOf("2011-01-01"));dao.updateStudentById(s);*/System.out.print(dao.queryStudentByName("mary"));}}7. Student.java内容如下:
package mybatis;import java.sql.Date;public class Student {private int sid = 0;private String sname = null;private String major = null;private Date birth = null;private float score = 0;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 float getScore() {return score;}public void setScore(float score) {this.score = score;}@Overridepublic String toString(){// TODO Auto-generated method stubString content = "sid="+sid+" sname="+sname+" major="+major+" birth="+birth+"score="+score;return content;}}
0 0
- ibatis-2.3.3学习
- ibatis 学习笔记 3
- IBatis-学习-3
- ibatis-2.3.4.726学习(小结)
- ibatis 3 beta2学习笔记
- ibatis 3 学习笔记1
- ibatis 3 学习笔记 2
- ibatis 3 学习笔记 5
- ibatis 3 学习笔记 6
- ibatis学习
- iBatis学习
- ibatis 学习
- 学习ibatis
- iBatis 学习
- ibatis学习
- ibatis学习
- ibatiS学习
- Ibatis 学习
- Java正则表达式之代码实战
- VB动态调用外部API函数的方法
- 黑马程序员_反射知识总结
- sod
- 关于stream!=NULL问题
- ibatis-2.3.3学习
- 【Java】如何终止一个线程
- sicily 1031.Campus
- boost多线程库使用指南
- C++中的运算符重载(二)
- Invalid file name: must contain only [a-z0-9_.]
- hadoop简介
- WPF下、使用Double作为点坐标带来的问题或line直线颜色不明显的问题的解决方法
- 【MoreWindows工作笔记11】EnumClipboardFormats 剪切板内容的数据格式