Get the App
SLTechnology News&Howtos  ›  Database  › 

Mybatis- ordinary sq adds, deletes, modifies and examines study notes

Shulou Source: shulou.com Published: 2022-06-01 12:38:11 09月20日 Update

Import java.util.*;import cn.mybatis.entity.Student;import cn.mybatis.util.MybatisUtil;import org.apache.ibatis.session.SqlSession;public class StudentDao {/ * add students * / public void add (Student student) throws Exception {SqlSession sqlSession = null; try {sqlSession = MybatisUtil.getSqlSession () / / transaction start (default) / / read the SQL statement int I = sqlSession.insert (Student.class.getName () + ".add", student) in the StudentMapper.xml mapping file; System.out.println ("this operation affects" + I + "line"); / / transaction commit sqlSession.commit () } catch (Exception e) {e.printStackTrace (); / / transaction rollback sqlSession.rollback (); throw e;} finally {/ / MybatisUtil.closeSqlSession ();}} / * query students according to ID * / public Student findById (int id) throws Exception {SqlSession sqlSession = null Try {sqlSession = MybatisUtil.getSqlSession (); Student student = sqlSession.selectOne (Student.class.getName () + ".findById", id); sqlSession.commit (); return student;} catch (Exception e) {e.printStackTrace (); sqlSession.rollback (); throw e } finally {MybatisUtil.closeSqlSession ();}} / * query all students * / public List findAll () throws Exception {SqlSession sqlSession = null; try {sqlSession = MybatisUtil.getSqlSession (); return sqlSession.selectList (Student.class.getName () + ".findAll") } catch (Exception e) {e.printStackTrace (); throw e;} finally {MybatisUtil.closeSqlSession ();}} / * Update students * / public void update (Student student) throws Exception {SqlSession sqlSession = null; try {sqlSession = MybatisUtil.getSqlSession () SqlSession.update (Student.class.getName () + ".update", student); sqlSession.commit ();} catch (Exception e) {e.printStackTrace (); sqlSession.rollback (); throw e;} finally {MybatisUtil.closeSqlSession () }} / * Delete students * / public void delete (Student student) throws Exception {SqlSession sqlSession = null; try {sqlSession = MybatisUtil.getSqlSession (); sqlSession.delete (Student.class.getName () + ".delete", student); / / transaction sqlSession.commit () } catch (Exception e) {e.printStackTrace (); / / rollback sqlSession.rollback (); throw e;} finally {MybatisUtil.closeSqlSession ();}} public static void main (String [] args) throws Exception {StudentDao dao = new StudentDao () / / dao.add (new Student (3, "Beautiful", 70030.3)); / / dao.add (new Student (4, "come on", 70030.3)); / / dao.add (new Student (5, "relationship", 70030.3)); / / dao.add (new Student (6, "Rule", 70030.3)); / / dao.add (new Student (7, "Gulin", 70030.3)) / / List studentslist = dao.findAll (); / / for (Student student: studentslist) {/ / System.out.print (student.getId () + ":" + student.getName () + ":" + student.getSal ()); / /} / / Student student = dao.findById (4); / / student.setName ("liwen"); / / dao.update (student) }} insert into students (id,name,sal) values (# {id}, # {name}, # {sal}) select id,name,sal from students where id = # {id} select id,name,sal from students update students set name=# {name} Sal=# {sal} where id=# {id} delete from students where id=# {id} select id,name,sal from students limit # {pstart}, # {psize}

Tags: Students transactions queries just beauty documents theories types rules statements paths path names Gulin influence updates ordinary notes learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple Shulou Technology MariaDB Docker