In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.