In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use the Hibernate,MyBatis,Bee framework". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Hibernate,MyBatis,Bee framework".
1. Examples of getting started with Hibernate
1.1 Code example
Import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Client {public static void main (String [] args) {SessionFactory sf = new Configuration (). Configure (). BuildSessionFactory (); / / Why should there be such a template-like statement Session s = null; Transaction t = null; try {/ / prepare data UserModel um = new UserModel (); um.setUuid ("1"); um.setUserId ("id1") Um.setName ("name1"); s = sf.openSession (); / / Why there is such a template-like statement t = s.beginTransaction (); / / Why only one statement s.save (um) also needs to show the declaration transaction s.save (um); t.commit (); / / Why only one statement s.save (um) also needs to show the commit transaction} catch (Exception err) {t.rollback () Err.printStackTrace ();} finally {s.close ();}
1.2 configure xxx.hbm.xml
1: has the same name as the described class, such as: UserModel.hbm.xml
2: the storage location is in the same folder as the described class.
3: it is mainly configured in the following four parts:
(1) Mapping of classes and tables
(2) Mapping of primary keys
(3) Mapping between attributes of class and fields in DB
(4) Mapping of relations.
1.3 the log is not humanized enough to understand.
Test: run the Client file directly in Elipse. At the end of the run, you will see the output in console: "Hibernate: insert into tbl_user (userId, name, age, uuid) values", open the database table, and you will see that a value has been added.
Reference: https://www.jb51.net/article/123469.htm
II. Examples of getting started with MyBatis
2.1 Code example
9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;// insert user 55 public void testInsertUser () {56 SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory (); / / 1 this kind of code can be written without programmers 57 SqlSession sqlSession = sqlSessionFactory.openSession (); / / 2 this kind of code can be written without programmers 58 59 User user = new User () 60 user.setUsername ("Diao Chan"); 61 user.setSex ("0"); 62 user.setBirthday (new Date ()); 63 user.setAddress ("Lv Bu"); 64 65 / execute insert statement 66 sqlSession.insert ("user.insertUser", user); 67 / commit transaction 68 sqlSession.commit () / / 3 this kind of code can be written without programmers 69 / / release resources 70 sqlSession.close (); / / 4 this kind of code can be written without programmers 71} public class SqlSessionFactoryUtils {2 private SqlSessionFactoryUtils () {} 3 4 private static class SqlSessionFactoryInstance {5 6 public static SqlSessionFactory sqlSessionFactory 7 8 static {9 try {10 sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (Resources.getResourceAsStream ("SqlMapConfig.xml")); 11} catch (IOException e) {12 e.printStackTrace (); 13} 14} 15} 16 17 public static SqlSessionFactory getSqlSessionFactory () {18 return SqlSessionFactoryInstance.sqlSessionFactory;19} 20 21}
2.2 write mapper configuration file
Create a UserMapper.xml mapping file
It's so simple, you have to write SQL statements!
37 38 INSERT INTO USER (`username`, `roomday`, `sex`, `address`) 39 VALUES (# {username}, # {birthday}, # {sex}, # {address}) 40
2.3 logs are also placeholders, although there are more display parameter types. But it's still the same as Hibernate: the log is not human enough to understand.
There is a difference between # {} and ${}, one of which has injection risk.
Reference: https://www.cnblogs.com/gdwkong/p/8734020.html
III. Examples of getting started with Bee
3.1 example code
Two lines of Bee-related code can insert the data into the DB.
Import org.teasoft.bee.osql.Suid;import org.teasoft.exam.bee.osql.entity.Orders;import org.teasoft.honey.osql.core.BeeFactoryHelper;public class SimpleExam {public static void main (String [] args) {Suid suid=BeeFactoryHelper.getSuid (); / / 1 Orders orders1=new Orders (); orders1.setId (100001L) Orders1.setName ("Bee (ORM Framework)"); suid.insert (orders1); / / 2}}
3.2 xml profile is not required for Bee
3.3 log-friendly, convenient for development and debugging
3.3.1
[INFO] [Bee] insert SQL: insert into orders (id,name) values [values]: 100001Bee (ORM Framework)
[INFO] [Bee] |
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: 265
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.