MyBatis and additions and deletions change

Source: Internet
Author: User

Take Stu as an example, the Stu.xml configuration file is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Mybatis-3-mapper.dtd" > <mapper namespace= "stu" > &L t;! --Query results using SQL statements--<select id= "FindAll" resulttype= "Stu" > select * from Stu </select> <!--result Map--<resultmap type= "Stu" id= "Stumap" > <id property= "Sid" Column= "s_id"/> <result property= "sn Ame "column=" S_name "/> <result property=" ssex "column=" S_sex "/> <result property=" Sbirth "column=" S_birt
		H "/> </resultMap> <!--resultmap ID value for Resultmap-<select id=" findAll2 "resultmap=" Stumap "> SELECT * from Stu </select> <!--parameter query 1 normal type (parameter any name)--<select id= "findparams1" resultmap= "Stumap" pa rametertype= "int" > select * from stu s where s.s_id = #{id} </select> <!--parameter Query 2 class type (parameter Class property name)--&lt ; Select Id= "Findparams2" resultmap= "Stumap" parametertype= "Stu" > select * from Stu s wherE s.s_id = #{sid} </select> <!--parameter Query 3 map type (parameter is key value)--<!--usage most flexible ...
	--<select id= "Findparams3" resultmap= "Stumap" parametertype= "Map" > select * from stu s where s.s_id = #{id} </select> <!--Query one--<select id= "FindOne" resultmap= "Stumap" parametertype= "int" > select * Fro M stu s where s.s_id = #{id} </select> <!--fuzzy Query 1--<select id= "Findlike" resultmap= "Stumap" Paramet Ertype= "String" > select * from Stu s where s.s_name like "%" #{xxx} "%" </select> <!--fuzzy Query 2--<s Elect id= "FindLike2" resultmap= "Stumap" parametertype= "String" > select * from Stu s where s.s_name like #{xxx} </ Select> <!--add-<insert id= "Add" parametertype= "Stu" > INSERT into Stu (S_name,s_sex,s_birth) Valu
		ES (#{sname},#{ssex},#{sbirth}) </insert> <!--update--<update id= "Update" parametertype= "Stu" > Update Stu Set s_name=#{sname},s_sex=#{ssex},s_birth=#{sbirth} where S_id=#{sid} </update> <!--Delete--<delete id= "delete" parametertype= "int" > de
 Lete from Stu where s_id=#{xx} </delete> </mapper>
The corresponding test classes are as follows:

Package com.it.test;
Import Java.io.Reader;
Import Java.sql.Date;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import Org.apache.ibatis.session.SqlSessionFactory;

Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
Import Com.it.entity.Stu;

Import Com.it.util.MyBatisUtil; public class Test {//query all public static void FindAll () {try {reader reader = Resources.getresourceasreader ("Myb
			Atis.xml ");
			Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);
			sqlsession session = Factory.opensession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findall"); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace (); }}//query all 2 public static void FindAll2 () {try {Reader reader = Resources.getresourceasreader ("Mybatis.xml");
			Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);
			sqlsession session = Factory.opensession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findall2"); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//query all 3 public static void FindAll3 () {try {sqlsession session = Mybatisutil.getsession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findall2"); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace (); }}//query according to parameter 1 public static void FindParams1 () {try {reader reader = Resources.getresourceasreader ("MybaTis.xml ");
			Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);
			sqlsession session = Factory.opensession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findparams1", 1); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace (); }}//query according to parameter 2 public static void FindParams2 () {try {reader reader = Resources.getresourceasreader ("Mybatis.xm
			L ");
			Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);
			sqlsession session = Factory.opensession ();
			Stu Stu = new Stu ();
			Stu.setsid (3);
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findparams2", Stu); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close (); } catch (Exception e) {e.printstacktrace (); }}//query according to parameter 3 public static void FindParams3 () {try {reader reader = Resources.getresourceasreader ("Mybatis.xm
			L ");
			Sqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);
			sqlsession session = Factory.opensession ();
			Stu Stu = new Stu ();
			Stu.setsid (3);
			map<string, object> map = new hashmap<string, object> ();
			Map.put ("id", 2);
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("STU.FINDPARAMS3", map); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//query a public static void FindOne () {try {sqlsession session = Mybatisutil.getsession ();
			The SQL statement corresponding to the Entity class mapping file Stu s = Session.selectone ("Stu.findone", 1); System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth ());
		Session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//Fuzzy query 1 public static void Findlike () {try {sqlsession session = Mybatisutil.getsession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findlike", "O"); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//Fuzzy query 2 public static void FindLike2 () {try {sqlsession session = Mybatisutil.getsession ();
			The SQL statement corresponding to the Entity class mapping file list<stu> ls = session.selectlist ("Stu.findlike2", "%y%"); for (Stu s:ls) {System.out.println (S.getsid () + "\ T" + s.getsname () + "\ T" + s.getssex () + "\ T" + S.getsbirth
			());
		} session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//Add public static void Add () {try {sqlsession session = Mybatisutil.getsession (); The Entity class mapping file corresponds to the sqL statement Stu s = new Stu (0, "Zake", "Male", date.valueof ("1990-10-11"));
			int r = Session.insert ("Stu.add", s);
			System.out.println (R);
			Submit Session.commit ();
		Session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//Modify public static void Update () {try {sqlsession session = Mybatisutil.getsession ();
			The Entity class mapping file corresponds to the SQL statement Stu s = new Stu (4, "Zakey", "Male", date.valueof ("1995-10-11"));
			Session.update ("Stu.update", s);
			Session.commit ();
		Session.close ();
		} catch (Exception e) {e.printstacktrace ();
			}}//delete public static void Delete () {try {sqlsession session = Mybatisutil.getsession ();
			The Entity class mapping file corresponds to the SQL statement session.delete ("Stu.delete", 4);
			Session.commit ();
		Session.close ();
		} catch (Exception e) {e.printstacktrace ();
		 }} public static void Main (string[] args) {/* * * FINDPARAMS1 (); FindParams2 (); FindParams3 ();
		*///FINDALL3 ();
		FindOne ();
		Findlike ();
	ADD ();
 }

}



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.