Java Interview Essentials---Introduction to the use of Ibatis framework---update at any time

Source: Internet
Author: User

The basic use of 1.ibatIS, here through an example to learn
A. Dream Technology QQ Communication Group: credream:251572072
--------------------------------------------------
A. New Ibatistest project
/ibatistest/src/com/credream/test/testimpl.java
Package com.credream.test;

Import Java.sql.Date;
Import Com.credream.IStudentDao;
Import Com.credream.IStudentDaoImpl;
Import com.credream.Student;
public class Testimpl {
public static void Main (string[] args) {
Istudentdao dao=new Istudentdaoimpl ();
Test Queryallstudent () method
/*for (Student student:dao.queryAllStudent ()) {
SYSTEM.OUT.PRINTLN (student);
}*/
Test Querystudentbyid () method
/*SYSTEM.OUT.PRINTLN ("ID is 1:" +dao.querystudentbyid (1));
Test addstudent (Student Student) method
/*student student=new Student ();
Student.setsid (4);
Student.setsname ("Xushu");
Student.setmajor ("Games");
Student.setbirth (date.valueof ("2012-12-22"));
Student.setscore ((float) 18.5);
Dao.addstudent (student);
Test Deltestudentbyid (int id) method
Dao.deltestudentbyid (3);
Test Updatestudentbyid (Student Student) method;
/*student student=new Student ();
Student.setsid (4);
Student.setsname ("Xushen");
Student.setmajor ("Games");
Student.setbirth (date.valueof ("2012-12-22"));
Student.setscore ((float) 18.5);
Dao.updatestudentbyid (student);
Test Querystudentbyname (String name) method
/*list<student> students=dao.querystudentbyname ("x");
for (Student student:students) {
SYSTEM.OUT.PRINTLN (student);
}*/
Test: Addstudentbysequence (Student Student) method
Student student=new Student ();
Student.setsid (4);
Student.setsname ("Xushen");
Student.setmajor ("Games");
Student.setbirth (date.valueof ("2012-12-22"));
Student.setscore ((float) 18.5);
Dao.addstudentbysequence (student);

}
}
-------------------------------
2./ibatistest/src/com/credream/util/dbutil.java
Package com.credream.util;
public class Dbutil {

}
---------------------------------------------------------
3./ibatistest/src/com/credream/istudentdao.java
Package com.credream;

Import java.util.List;

Public interface Istudentdao {
public void Addstudent (Student Student);
public void Addstudentbysequence (Student Student);
public void Deltestudentbyid (int id);
public void Updatestudentbyid (Student Student);
Public list<student> queryallstudent ();
Public list<student> querystudentbyname (String name);
Public Student Querystudentbyid (int id);
}
------------------------------------------------
4./ibatistest/src/com/credream/istudentdaoimpl.java
Package com.credream;

Import java.io.IOException;
Import Java.io.Reader;
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{
Reader reader;
try {
Reader = Com.ibatis.common.resources.Resources.getResourceAsReader ("Com/credream/sqlmapconfig.xml");
Sqlmapclient=com.ibatis.sqlmap.client.sqlmapclientbuilder.buildsqlmapclient (reader);
Reader.close ();
catch (IOException e) {
E.printstacktrace ();
}
}

public void Addstudent (Student Student) {
try {
Sqlmapclient.insert ("Insertstudent", student);
catch (SQLException e) {

E.printstacktrace ();
}

}

public void Addstudentbysequence (Student Student) {
try {
1. Get the primary key value from the database sequence
2. Insert a record in the student table
Sqlmapclient.insert ("Insertstudentbysequence", student);
System.out.println ("sid=" +student.getsid ());
catch (SQLException e) {
E.printstacktrace ();
}
}

public void Deltestudentbyid (int id) {
try {
System.out.println ("Start deletion ...");
System.out.println (Sqlmapclient.delete ("Deltestudentbyid", id));
System.out.println ("Delete successful ...");
catch (SQLException e) {

E.printstacktrace ();
}

}

Public list<student> queryallstudent () {
List<student> Studentlist=null;
try {
Studentlist=sqlmapclient.queryforlist ("Selectallstudent");
catch (SQLException e) {
E.printstacktrace ();
}
return studentlist;
}

Public Student Querystudentbyid (int id) {
Student Student=null;
try {
student= (student) sqlmapclient.queryforobject ("Selectstudentbyid", id);
catch (SQLException e) {
E.printstacktrace ();
}
return student;
}
Public list<student> querystudentbyname (String name) {
List<student> Students=null;
try {
Students=sqlmapclient.queryforlist ("Querystudentbyname", name);
catch (SQLException e) {

E.printstacktrace ();
}
return students;
}
public void Updatestudentbyid (Student Student) {
try {
System.out.println (Sqlmapclient.update ("Updatestudentbyid", student));
catch (SQLException e) {
E.printstacktrace ();
}
}
}
---------------------------------------------------------------------------
5./ibatistest/src/com/credream/student.java
Package com.credream;

Import Java.util.Date;

public class Student {
/**
* This place is best not to overwrite the constructor method of the parent class
*/
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;
}
@Override
Public String toString () {
String content= "sid=" +sid+ "/sname" +sname+ "tmajor:" +major+ "Birth:" +
Birth;
return content;
}


}
-------------------------------------------------------------------------
6./ibatistest/src/com/credream/sqlmap.propertiesdriver=com.mysql.jdbc.driver
Url=jdbc\:mysql\://localhost\:3306/test
Username=root
password=1234
-------------------------------------------------
7./ibatistest/src/com/credream/sqlmapconfig.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE sqlmapconfig Public "-//ibatis.com//dtd SQL Map Config 2.0//en"
"Http://www.ibatis.com/dtd/sql-map-config-2.dtd" >
<!--<! DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en"
"Http://ibatis.apache.org/dtd/sql-map-2.dtd" >
-->
<sqlMapConfig>
<properties resource= "Com/credream/sqlmap.properties"/>
<transactionmanager type= "JDBC" >
<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>
<sqlmap resource= "Com/credream/student.xml"/>
</sqlMapConfig>
-------------------------------------------------------------
8./ibatistest/src/com/credream/student.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE sqlmap Public "-//ibatis.com//dtd SQL Map 2.0//en" "Http://www.ibatis.com/dtd/sql-map-2.dtd" >
<sqlMap>
<typealias alias= "Student" type= "Com.credream.Student"/>
<select id= "selectallstudent" resultclass= "Student" >
SELECT * from student;
</select>
<select id= "Selectstudentbyid" parameterclass= "int" resultclass= "Student" >
SELECT * FROM student where sid= #sid #
</select>
<insert id= "insertstudent" parameterclass= "Student" >
Insert into Student (Sid,sname,major,birth,score) VALUES (#sid #, #sname #, #major #, #birth #, #score #)
</insert>

<delete id= "Deltestudentbyid" parameterclass= "int" >
Delete from student where sid= #sid #
</delete>

<update id= "Updatestudentbyid" parameterclass= "Student" >
Update Student set sname= #sname #,major= #major #,score= #score #,birth= #birth #
where Sid= #sid #
</update>


<select id= "Querystudentbyname" parameterclass= "string" resultclass= "Student" >
Select Sid,sname,major,birth,score from student where sname like '% $sname $% '

</select>


<insert id= "insertstudentbysequence" parameterclass= "Student" >
<selectkey resultclass= "int" keyproperty= "Sid" >
Select Studentpksequence.netval from dual
</selectKey>
INSERT into student (Sid,sname,birth,major,score) VALUES (#sid #, #sname #,
#birth #, #major #, #score #)
</insert>

</sqlMap>
------------------------------------------------------
9.g:\javalib\mysqldriver\mysql-connector-java-5.1.16-bin.jar
G:\javalib\ibatis\ibatis-2.3.0.677.jar
--------------------------------------------------------------------------
2. Principle Understanding:
A. Dream Technology QQ Communication Group: credream:251572072
Ibatis is not the best choice for the project.
Ibatis is a data mapping tool. Its function is to establish a mapping relationship between the database query and the object's attributes. If the project to be developed is based on a business object (including mapping or
Data Dictionary object), then Ibatis can be considered a good choice. When the application is layered design implementation, it is a good choice, so that the business layer can and the user interface
Layers apart.
-------------------------------------------
3.private static sqlmapclient Sqlmapclient=null;
static{
Reader reader;
try {
Reader = Com.ibatis.common.resources.Resources.getResourceAsReader ("Com/credream/sqlmapconfig.xml");
Sqlmapclient=com.ibatis.sqlmap.client.sqlmapclientbuilder.buildsqlmapclient (reader);
Reader.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
-------------------------------------------------------------------
4.public void Addstudent (Student Student) {
try {
Sqlmapclient.insert ("Insertstudent", student);
catch (SQLException e) {

E.printstacktrace ();
}

}
-------------------------------------------
Public list<student> queryallstudent () {
List<student> Studentlist=null;
try {
Studentlist=sqlmapclient.queryforlist ("Selectallstudent");
catch (SQLException e) {
E.printstacktrace ();
}
----------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.