In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Copy the required jar (mysql-connector-java-5.1.18-bin.jar and mybatis-3.3.0.jar) to the lib directory
two。 Write the entity class UserInfo corresponding to the database
3. Create a mybatis-config.xml profile:
4. Create a DBFactory class to get SqlSessionFactory
/ * access database class * / public class DBFactory {public static SqlSessionFactory sqlSessionFactory = null; static {try {String resource = "com/wc/config/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream (resource); sqlSessionFactory = new SqlSessionFactoryBuilder () .build (inputStream, "mysql") / / second parameter: specify the database configuration to be connected} catch (IOException e) {e.printStackTrace ();}} public static SqlSessionFactory getFactory () {return sqlSessionFactory;}
Or
/ * access database class * / public class DBAccess {public SqlSession getSqlSession () throws IOException {/ / (1) obtain database connection related information through configuration file
Reader reader = Resources.getResourceAsReader ("com/wc/config/Configuration.xml"); / / (2) build SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (reader, "mysql") through configuration information; / / (3) Open database via SqlSessionFactory SqlSession sqlSession = sqlSessionFactory.openSession (); return sqlSession;}}
5. Create a sql mapping file userinfo.xml for mybatis
: corresponding interface, unified namespace, important!
ResultMap: property corresponds to entity class attributes, and column corresponds to database fields
Sql statement:
Id= "selectUserByInterface" unique identity
Type of parameterType= "java.lang.Integer" parameter
ResultMap= "userResult" return type
Select from user_info order by user_id
User_id,user_name,user_sex
Select from user_info order by user_id select from user_info where user_id=# {userID}
6. Call the query statement of XML through interface (interface-oriented programming)
Public interface IUserInfo {/ * query single information based on user_id * / public UserInfo selectOneUserByInterface (int I); / * * query all information * / public List selectUserByInterface ();}
7. Get SqlSession:DBFactory.getFactory (). OpenSession () from SqlSessionFactory
After getting the addition, deletion, modification and check from SqlSession, a series of methods
/ * all queries are made using the API * / @ Testpublic void selectUserByInterface () {List userInfo = new ArrayList (); / / get sqlSession SqlSession session = null; try {/ / get SqlSession session = DBFactory.getFactory (). OpenSession () from SqlSessionFactory / / Interface IUserInfo iUserInfo = session.getMapper (IUserInfo.class); userInfo = iUserInfo.selectUserByInterface (); if (userInfo! = null & & userInfo.size () > 0) {for (UserInfo user: userInfo) {System.out.println (user) } catch (Exception e) {} finally {if (session! = null) {session.close () } / * make a single query using the interface * / @ Testpublic void selectOneUserByInterface () {/ / get sqlSession SqlSession session = null; try {/ / get SqlSession session = DBFactory.getFactory () .openSession () from SqlSessionFactory / / UserInfo userInfo = session.selectOne ("userinfo.selectOneUser", 3); userinfo is the previous namespace IUserInfo iUserInfo = session.getMapper (IUserInfo.class); UserInfo userInfo = iUserInfo.selectOneUserByInterface (3); System.out.println (userInfo) } catch (Exception e) {} finally {if (session! = null) {session.close ();}
Finally, close SqlSession.
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.
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.