Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report