How to use ibatis combined with MySQL database

Source: Internet
Author: User

Ibatis is the predecessor of MyBatis, which is an open source persistence layer framework. At its core, sqlmap--maps the entity bean to the relational database, separating the business code from the writing of the SQL statement. Ibatis is the "semi-automated" ORM persistence layer framework. This "semi-automation" is the "fully automated" ORM implementation that provides a comprehensive database encapsulation mechanism relative to hibernate, and the "fully automatic" ORM implements the mapping between the Pojo and the database table fields and enables the automatic generation and execution of SQL. The focus of Ibatis is the mapping between Pojo and SQL, which means that Ibatis does not automatically generate and execute SQL for programmers at run time, and specific SQL statements need to be written by programmers. The parameters required by the SQL statement and the returned result fields are then mapped to the specified Pojo through the mapping configuration file. This blog will demonstrate how Ibatis combines MySQL database usage:

Engineering structures such as:


As this example introduces the more comprehensive, the file is more, here only the marked two files in the code, the full source can be downloaded by clicking the hyperlink at the bottom of this article:

Code in the Studentdao.java file:

Package Com.ghj.dao.imp;import Java.io.ioexception;import Java.io.reader;import java.sql.sqlexception;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Com.ghj.dao.istudentdao;import Com.ghj.vo.student;import Com.ibatis.common.resources.resources;import com.ibatis.sqlmap.client.SqlMapClient; Import com.ibatis.sqlmap.client.sqlmapclientbuilder;/** * Student Management data Access Layer Interface implementation class * * @author Gao Yingjie */public class Studentdao Implem Ents Istudentdao {private sqlmapclient sqlmapclient;public Studentdao () {String resource = "Config/sqlmapconfig.xml"; try {Reader reader = resources.getresourceasreader (Resource);//Read config file Sqlmapclient = Sqlmapclientbuilder.buildsqlmapclient (reader);} catch (IOException e) {e.printstacktrace ();}} /** * Add Student information * * @author Gao Yingjie */@Overridepublic Boolean Add (Student Student) throws Sqlexception{return Sqlmapclient.updat E ("Add", Student) > 0;} /** * Delete Student information based on username * * @author Gao Yingjie */@Overridepublic Boolean deletebyusername (String userName) throws Sqlexception{retuRN Sqlmapclient.delete ("Deletebyusername", UserName) > 0;} /** * Update password according to username * * @author Gao Yingjie */@Overridepublic Boolean updatepasswordbyusername (String userName, string password) thro WS sqlexception{map<string, object> params = new hashmap<string, object> ();p arams.put ("UserName", UserName );p arams.put ("password", password), return sqlmapclient.update ("Updatepasswordbyusername", params) > 0;} /** * Check student information according to student's username * * @author Gao Yingjie */@Overridepublic Student findbyusername (String userName) throws Sqlexception{return (Student) Sqlmapclient.queryforobject ("Findbyusername", UserName);} /** * Check all student information * * @author Gao Yingjie */@Override @suppresswarnings ("unchecked") public list<student> FindAll () throws Sqlex Ception{return sqlmapclient.queryforlist ("FindAll");}}

Code in the Student.xml file:

<?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><!--set an alias for the student class--><typealias alias= "student" type= "com.ghj.vo.Student"/><!-- Mapping relationship between configuration table and entity Bean--><resultmap id= "Studentmap" class= "com.ghj.vo.Student" ><result property= "id" column = "id"/><result property= "UserName" column= "user_name"/><result property= "password" column= "password"/ ><result property= "state" column= "state"/></resultmap><!--add student information--><insert id= "Add" parameterclass= "Student" >insert into student values (#id #, #userName #, #password #, #state #) </insert><!-- Delete Student information based on user name--><delete id= "Deletebyusername" parameterclass= "java.lang.String" > Delete from student where US Er_name= #userName # </delete><!--update password based on user name--><update id= "Updatepasswordbyusername" parameterclass= " Java.util.HashMap "> UpdaTe student set password= #password # where user_name= #userName # </update><!--query Student information based on student username--><select id= "Findbyusername" parameterclass= "string" resultmap= "Studentmap" >select * from student where user_name= #userName # </select><!--Check all student information--><select id= "FindAll" resultmap= "Studentmap" >select * from student</ Select></sqlmap>
0 min Download the sample code

How to use ibatis combined with MySQL database

Related Article

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.