IBatis simple getting started, ibatis getting started

Source: Internet
Author: User

IBatis simple getting started, ibatis getting started

IBatisIntroduction:

IBatis is an open-source apache project and an O/R Mapping solution. The biggest feature of iBatis is its small size and quick start. If you do not need many complex functions, iBatis is the simplest solution that can meet your requirements and be flexible enough. Now, iBatis has been renamed Mybatis.

Official Website: http://www.mybatis.org/

Build an iBatis development environment:

1. Import relevant jar packages, ibatis-2.3.0.677.jar, mysql-connector-java-5.1.6-bin.jar

2. Compile the configuration file:

Attribute file for Jdbc connection

Total configuration file, SqlMapConfig. xml

Ing files for each object (Map Files)

Demo:

1 package com. iflytek. entity; 2 import java. SQL. date; 3/** 4 * @ author xudongwang 2011-12-31 5*6 * Email: xdwangiflytek@gmail.com 7*8 */9 public class Student {10 // note here you need to ensure that there is a no-argument constructor, because mappings, including Hibernate, all use reflection, if no parameter is set, the following error may occur: 11 private int id; 12 private String name; 13 private Date birth; 14 private float score; 15 public int getId () {16 return id; 17} 18 public void setId (int id) {19 this. id = id; 20} 21 public String getName () {22 return name; 23} 24 public void setName (String name) {25 this. name = name; 26} 27 public Date getBirth () {28 return birth; 29} 30 public void setBirth (Date birth) {31 this. birth = birth; 32} 33 public float getScore () {34 return score; 35} 36 public void setScore (float score) {37 this. score = score; 38} 39 @ Override40 public String toString () {41 return "id =" + id + "\ tname =" + name + "\ tmajor =" + birth + "\ tscore =" 42 + score + "\ n "; 43} 44}

SqlMap. properties:

1 driver=com.mysql.jdbc.Driver2 url=jdbc:mysql://localhost:3306/ibatis3 username=root4 password=123
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE sqlMap PUBLIC "-// ibatis.apache.org//DTD SQL Map 2.0 // EN" 3 "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 4 <sqlMap> 5 <! -- Using typeAlias makes it unnecessary to write the package name --> 6 <typeAlias alias = "Student" type = "com. iflytek. entity. student "/> 7 <! -- After changing the SQL statement, you do not need to modify the java code --> 8 <! -- Id indicates the SQL statement in select, resultClass indicates the type of the returned result --> 9 <select id = "selectAllStudent" resultClass = "Student"> 10 select * from11 tbl_student12 </select> 13 <! -- ParameterClass indicates the parameter content --> 14 <! -- # Indicates that this is a parameter to be passed in for an external call, it can be understood as a placeholder --> 15 <select id = "selectStudentById" parameterClass = "int" resultClass = "Student"> 16 select * from tbl_student where id = # id #17 </select> 18 <! -- Note that the resultClass type here. The use of the Student type depends on queryForList or queryForObject --> 19 <select id = "selectStudentByName" parameterClass = "String" 20 resultClass = "Student"> 21 select name, birth, score from tbl_student where name like22 '% $ name $ %' 23 </select> 24 <insert id = "addStudent" parameterClass = "Student"> 25 insert into 26 tbl_student (name, birth, score) values27 (# name #, # birth #, # score #); 28 <selectKey resultClass = "int" KeyProperty = "id"> 29 select @ identity as inserted30 <! -- Here we need to describe the generation of different database primary keys, which have different methods for their respective databases: --> 31 <! -- Mysql: SELECT LAST_INSERT_ID () as value --> 32 <! -- Mssql: select @ IDENTITY as value --> 33 <! -- Oracle: select stockidsequence. nextval as value from dual --> 34 <! -- Note that the primary keys generated by different database manufacturers are different. Some are pre-generated (pre-generate) Primary keys, such as Oracle and PostgreSQL. 35 Some are post-generate primary keys, such as MySQL and SQL Server, you need to write selectKey before insert --> 36 </selectKey> 37 </insert> 38 <delete id = "deleteStudentById" parameterClass = "int"> 39 <! -- # Id # can be retrieved at will, but the above insert will have an impact, because the above name will be searched from the attributes in Student --> 40 <! -- We can also understand that if there is a # placeholder, ibatis calls the attribute in parameterClass to assign values --> 41 delete from tbl_student where id = # id #42 </delete> 43 <update id = "updateStudent" parameterClass = "Student"> 44 update tbl_student set45 name = # name #, birth = # birth #, score = # score # where id = # id #46 </update> 47 </sqlMap>

Note:

If there is no ibatis prompt in xml, window --> Preference --> XML Catalog ---> click add

Select uri URI: select a local file system

IBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd file;

Key Type: Select Schema Location;

Key: network connection is required. It is not recommended;

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE sqlMapConfig PUBLIC "-// ibatis.apache.org//DTD SQL Map Config 2.0 // EN" 3 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 4 <sqlMapConfig> 5 <! -- Reference the configuration file of the JDBC Property --> 6 <properties resource = "com/iflytek/entity/SqlMap. properties"/> 7 <! -- Use JDBC Transaction Management --> 8 <transactionManager type = "JDBC"> 9 <! -- Data source --> 10 <dataSource type = "SIMPLE"> 11 <property name = "JDBC. driver "value =" $ {driver} "/> 12 <property name =" JDBC. connectionURL "value =" $ {url} "/> 13 <property name =" JDBC. username "value =" $ {username} "/> 14 <property name =" JDBC. password "value =" $ {password} "/> 15 </dataSource> 16 </transactionManager> 17 <! -- Here you can write a ing file for multiple objects --> 18 <sqlMap resource = "com/iflytek/entity/Student. xml"/> 19 </sqlMapConfig>
Package com. iflytek. dao; import java. util. list; import com. iflytek. entity. student;/*** @ author xudongwang 2011-12-31 ** Email: xdwangiflytek@gmail.com **/public interface StudentDao {/*** add student information ** @ param Student * student Entity * @ return returns whether added successfully */public boolean addStudent (student Student ); /*** Delete student information by student id ** @ param id * student id * @ return: whether the deletion is successful */public boolean deleteStudentById (int id ); /*** update student information ** @ param Student * student Entity * @ return indicates whether the update is successful */public boolean updateStudent (student Student ); /*** query all Student information ** @ return returns to the Student List */public List <Student> selectAllStudent (); /*** fuzzy query of Student information by Student name ** @ param name * Student name * @ return Student information List */public List <Student> selectStudentByName (String name ); /*** query Student information by Student id ** @ param id * Student id * @ return Student object */public Student selectStudentById (int id );}
1 package com. iflytek. daoimpl; 2 import java. io. IOException; 3 import java. io. reader; 4 import java. SQL. SQLException; 5 import java. util. list; 6 import com. ibatis. common. resources. resources; 7 import com. ibatis. sqlmap. client. sqlMapClient; 8 import com. ibatis. sqlmap. client. sqlMapClientBuilder; 9 import com. iflytek. dao. studentDao; 10 import com. iflytek. entity. student; 11/** 12 * @ author xudongwang 2 011-12-31 13*14 * Email: xdwangiflytek@gmail.com 15*16 */17 public class StudentDaoImpl implements StudentDao {18 private static SqlMapClient sqlMapClient = null; 19 // read the configuration file 20 static {21 try {22 Reader reader = Resources 23. getResourceAsReader ("com/iflytek/entity/SqlMapConfig. xml "); 24 sqlMapClient = SqlMapClientBuilder. buildSqlMapClient (reader); 25 reader. close (); 26} catch (IOExcepti On e) {27 e. printStackTrace (); 28} 29} 30 public boolean addStudent (Student student) {31 Object object = null; 32 boolean flag = false; 33 try {34 object = sqlMapClient. insert ("addStudent", student); 35 System. out. println ("returned value for adding student information:" + object); 36} catch (SQLException e) {37 e. printStackTrace (); 38} 39 if (object! = Null) {40 flag = true; 41} 42 return flag; 43} 44 public boolean deleteStudentById (int id) {45 boolean flag = false; 46 Object object Object = null; 47 try {48 object = sqlMapClient. delete ("deleteStudentById", id); 49 System. out. println ("Return Value for deleting student information:" + object + ", here the number of affected rows is returned"); 50} catch (SQLException e) {51 e. printStackTrace (); 52} 53 if (object! = Null) {54 flag = true; 55} 56 return flag; 57} 58 public boolean updateStudent (Student student) {59 boolean flag = false; 60 Object object = false; 61 try {62 object = sqlMapClient. update ("updateStudent", student); 63 System. out. println ("returned value of updating student information:" + object + ", returned Number of affected rows"); 64} catch (SQLException e) {65 e. printStackTrace (); 66} 67 if (object! = Null) {68 flag = true; 69} 70 return flag; 71} 72 public List <Student> selectAllStudent () {73 List <Student> students = null; 74 try {75 students = sqlMapClient. queryForList ("selectAllStudent"); 76} catch (SQLException e) {77 e. printStackTrace (); 78} 79 return students; 80} 81 public List <Student> selectStudentByName (String name) {82 List <Student> students = null; 83 try {84 students = sqlMapClient. queryForList ("selectStudentByName", name); 85} catch (SQLException e) {86 e. printStackTrace (); 87} 88 return students; 89} 90 public Student selectStudentById (int id) {91 Student student = null; 92 try {93 student = (Student) sqlMapClient. queryForObject (94 "selectStudentById", id); 95} catch (SQLException e) {96 e. printStackTrace (); 97} 98 return student; 99} 100}
1 package com. iflytek. test; 2 import java. SQL. date; 3 import java. util. list; 4 import com. iflytek. daoimpl. studentDaoImpl; 5 import com. iflytek. entity. student; 6/** 7 * @ author xudongwang 2011-12-31 8*9 * Email: xdwangiflytek@gmail.com10 * 11 */12 public class TestIbatis {13 public static void main (String [] args) {14 StudentDaoImpl studentDaoImpl = new StudentDaoImpl (); 15 System. out. println ("test insertion"); 16 Student addStudent = new Student (); 17 addStudent. setName ("Li Si"); 18 addStudent. setBirth (Date. valueOf ("2011-09-02"); 19 addStudent. setScore (88); 20 System. out. println (studentDaoImpl. addStudent (addStudent); 21 System. out. println ("test query by id"); 22 System. out. println (studentDaoImpl. selectStudentById (1); 23 System. out. println ("test fuzzy search"); 24 List <Student> mohuLists = studentDaoImpl. selectStudentByName ("Li"); 25 for (Student student: mohuLists) {26 System. out. println (student); 27} 28 System. out. println ("test query all"); 29 List <Student> students = studentDaoImpl. selectAllStudent (); 30 for (Student student: students) {31 System. out. println (student); 32} 33 System. out. println ("deleting student information by id"); 34 System. out. println (studentDaoImpl. deleteStudentById (1); 35 System. out. println ("test updating Student information"); 36 Student updateStudent = new Student (); 37 updateStudent. setId (1); 38 updateStudent. setName ("Li Si 1"); 39 updateStudent. setBirth (Date. valueOf ("2011-08-07"); 40 updateStudent. setScore (21); 41 System. out. println (studentDaoImpl. updateStudent (updateStudent); 42} 43}

 


Ibatis getting started

Www.verycd.com/topics/284519/
IBATIS frame technology video tutorial

Ftp: // 222.214.218.61/book2/20070509/14 dee5a5-c7d0-43be-b3f9-71f730e0c9bf133
Ibatis Development Guide (Chinese Version)

Who do you think is the NBA championship this year?

Spurs !!!!!!!!!!!
My favorite !!!!!!!
It has won now !!!!!!!!
 

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.