The first mybaits Program (add, delete, modify, and query)

Source: Internet
Author: User

1. First establish project import MySQL driver jar package mybatis-3.1.1.jar

2. Edit the configuration file config. properties, mybatis-config.xml, studentmapper. xml

Config. Properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/studentuser=rootpassword=123

Mybatis-config.xml (core configuration files for database connections, transaction management, etc)

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configurationpublic "-// mybatis.org//dtd config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <properties resource =" com/ibatis/config. properties "> <! -- Read the property file --> </Properties> <environments default = "development"> <environment id = "development"> <transactionmanager type = "JDBC"/> <datasource type =" pooled "> <property name =" driver "value =" $ {driver} "/> <property name =" url "value =" $ {URL} "/> <property name = "username" value = "$ {user}"/> <property name = "password" value = "$ {password}"/> </datasource> </environment> </environments> <mappers> <mapper resource = "com/ibatis/studentmapper. XML "/> </mappers> </configuration>

Studentmapper. XML (ing file)

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. ibatis "> <select id =" selectallstudent "resulttype =" com. ibatis. student "> select * from student <! -- Student here is the database's student table --> </SELECT> <select id = "selectonestudent" resulttype = "com. ibatis. student "parametertype =" int "> select * from student where student. id = # {ID}; </SELECT> <insert id = "addstudent" parametertype = "com. ibatis. student "usegeneratedkeys =" true "keyproperty =" ID "> insert into student (ID, name, age, mail) values (# {ID}, # {name }, # {age },# {mail}); </Insert> <Delete id = "deletestudent" parametertype = "int"> Delete from student where student. id = # {ID} </delete> <update id = "updatestudent" parametertype = "com. ibatis. student "> Update student set student. name = # {name}, student. age = # {age}, student. mail = # {mail} Where student. id =#{ ID} </update> </mapper>

Studentdaoimpl. Java

 

package com.Dao.Impl;import java.io.IOException;import java.io.InputStream;import java.util.List;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.Dao.*;import com.Ibatis.Student; public class StudentDaoImpl implements StudentDao { private static SqlSessionFactory ssf ;  static{  String resource ="com/Ibatis/mybatis-config.xml";  InputStream stream;try {stream = Resources.getResourceAsStream(resource); ssf= new SqlSessionFactoryBuilder().build(stream); stream.close();} catch (IOException e) {e.printStackTrace();}  } public List<Student> GetAllStduent() {List<Student> list = null;SqlSession session = null; try { session = ssf.openSession(); list= session.selectList("com.Ibatis.SelectAllStudent");} catch (Exception e) {e.printStackTrace();}finally{session.close();} return list;}public void AddStudent(Student student) { SqlSession session = null; try { session = ssf.openSession();session.insert("com.Ibatis.AddStudent",student); session.commit();} catch (Exception e) {e.printStackTrace();}finally{session.close();}}public void DeleteStudent(int id) { SqlSession session = null; try { session = ssf.openSession(); session.delete("com.Ibatis.DeleteStudent", id); session.commit();} catch (Exception e) {e.printStackTrace();}finally{session.close();}}public Student GetOneStudent(int id) { Student student = null;  SqlSession session = null; try { session = ssf.openSession(); student =session.selectOne("com.Ibatis.SelectOneStudent", id);} catch (Exception e) {e.printStackTrace();}finally{session.close();} return student;}public void UpdateStudent(Student student) {SqlSession session = null; try { session = ssf.openSession(); session.update("com.Ibatis.UpdateStudent", student); session.commit();} catch (Exception e) {e.printStackTrace();}finally{session.close();}}}

Studenttest. Java

Public static void main (string [] ARGs) {studentdao Dao = new studentdaoimpl (); system. out. println ************************ ***"); list <student> List = Dao. getallstduent (); For (student s: List) {system. out. println (S. tostring ();} system. out. println ("************* Add a new record ********************* ***"); student student1 = new student (); student1.setage (45); student1.setname ("ant"); student1.setmail ("ant@sina.com"); Dao. addstudent (student1); system. out. println ("************** query 12th records before deletion ***************"); student student3 = Dao. getonestudent (12); If (student3 = NULL) {system. out. println ("12th records deleted");} else {system. out. println (student3.tostring ();} system. out. println ("*************** Delete 12th records *****************"); dao. deletestudent (12); system. out. println ("**************** query 12th records after deletion **************"); student student2 = Dao. getonestudent (12); If (student2 = NULL) {system. out. println ("12th records deleted");} else {system. out. println (student2.tostring ();} system. out. println ("*************** obtain the record before the update ****************** **"); student student4 = Dao. getonestudent (4); system. out. println (student4.getname (); student4.setname ("My name is changed"); Dao. updatestudent (student4); system. out. println ("*************** obtain the updated record ****************** **"); student student5 = Dao. getonestudent (4); system. out. println (student5.getname ());}

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.