In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to operate mysql through ibatis. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The steps are as follows:
1. Create a new project ibatisnew in eclipse, and import the jar packages of mysql and ibatis into it. These two packages (ibatis-2.3.4.726.jar+mysql-connector-java-5.0.8-bin.jar) can be copied directly to the WEB-INF/lib directory from the Internet.
2. Create the SqlMapConfig.xml file
This file contains the configuration and the reference section of the xml corresponding to each data table.
File name: SqlMapConfig.xml
Contents of the file:
3. Create the User.xml file referenced in SqlMapConfig.xml
This file corresponds to the user table in the database, where aliases can be defined and sql statements can be written.
File name: User.xml
Contents of the file:
Select * from user
4. Create the class file specified in the user.xml file
This file is a javabean, corresponding to the fields of the table in the database, with set and get methods.
File name; User.java
Contents of the file:
Package test_ibatis
Import java.sql.Date
Public class User {
@ Override
Public String toString () {
/ / TODO Auto-generated method stub
String str = "id =" + this.id
Str + = "name =" + this.name
Str + = "birthday =" + this.birthday
Str + = "money =" + this.money
Return str
}
Private int id
Private String name
Private Date birthday
Private float money
Public int getId () {
Return id
}
Public void setId (int id) {
This.id = id
}
Public String getName () {
Return name
}
Public void setName (String name) {
This.name = name
}
Public Date getBirthday () {
Return birthday
}
Public void setBirthday (Date birthday) {
This.birthday = birthday
}
Public float getMoney () {
Return money
}
Public void setMoney (float money) {
This.money = money
}
}
5. Establish DAO program and implementation program.
API name: IUserDAO.java
Content:
Package test_ibatis
Import java.util.List
Public interface IUserDAO {
Public List getAllUser ()
}
Implementation class: IUserDAOImpl.java
Content:
Package test_ibatis
Import java.io.IOException
Import java.io.Reader
Import java.sql.SQLException
Import java.util.List
Import com.ibatis.common.resources.Resources
Import com.ibatis.sqlmap.client.SqlMapClient
Import com.ibatis.sqlmap.client.SqlMapClientBuilder
Public class IUserDAOImpl implements IUserDAO {
Private static SqlMapClient client = null
Static {
Try {
Reader reader = Resources.getResourceAsReader ("test_ibatis/SqlMapConfig.xml")
Client = SqlMapClientBuilder.buildSqlMapClient (reader)
} catch (IOException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
}
}
Public List getAllUser () {
/ / TODO Auto-generated method stub
Try {
Return client.queryForList ("selectAllUsers")
} catch (SQLException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
}
Return null
}
}
1. To use the functions provided by ibatis in the implementation class, read the configuration from the xml file and construct the object of SqlMapClient.
2. The specific implementation method. By calling the method provided by SqlMapClient and specifying the id in xml, the corresponding sql can be executed and the result is returned.
6. Test class
File name: UserDAO.java
Contents of the file:
Package test_ibatis
Public class UserDAO {
/ * *
* @ param args
, /
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
IUserDAO dao = new IUserDAOImpl ()
For (User user: dao.getAllUser ()) {
System.out.println (user)
}
& nbsp
The above content is how to operate mysql through ibatis. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.