本篇文章為大家展示了使用Mybatis如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫進(jìn)行增刪改查操作,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、梁山網(wǎng)絡(luò)推廣、小程序制作、梁山網(wǎng)絡(luò)營銷、梁山企業(yè)策劃、梁山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供梁山建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
什么是 MyBatis?
MyBatis 是支持普通 SQL 查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架。 MyBatis 消除了幾乎所有的 JDBC 代碼和參數(shù)的手工設(shè)置以及對(duì)結(jié)果集的檢索。MyBatis 可以使用簡單的XML 或注解用于配置和原始映射,將接口和 Java 的 POJO(Plain Old Java Objects,普通的Java對(duì)象)映射成數(shù)據(jù)庫中的記錄。
Mybatis實(shí)例
對(duì)一個(gè)User表的CRUD操作:
User表:
-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'summer', '30', 'shanghai'); INSERT INTO `user` VALUES ('2', 'test2', '22', 'suzhou'); INSERT INTO `user` VALUES ('3', 'test1', '29', 'some place'); INSERT INTO `user` VALUES ('4', 'lu', '28', 'some place'); INSERT INTO `user` VALUES ('5', 'xiaoxun', '27', 'nanjing');
在Src目錄下建一個(gè)mybatis的xml配置文件Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
定義User mappers的User.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>insert into user(userName,userAge,userAddress) values(#{userName},#{userAge},#{userAddress}) update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id} delete from user where id=#{id}
配置文件實(shí)現(xiàn)了接口和SQL語句的映射關(guān)系。selectUsersByName采用了2種方式實(shí)現(xiàn),注釋掉的也是一種實(shí)現(xiàn),采用resultMap可以把屬性和數(shù)據(jù)庫列名映射關(guān)系定義好,property為類的屬性,column是表的列名,也可以是表列名的別名!
User類的定義:
package com.mybatis.test; public class User { private int id; private String userName; private int userAge; private String userAddress; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } @Override public String toString(){ return this.userName+" "+this.userAge+" "+this.userAddress; } }
IUserOperaton定義:
package com.mybatis.test; import java.util.List; public interface IUserOperation { public User selectUserByID(int id); public ListselectUsersByName(String userName); public void addUser(User user); public void updateUser(User user); public void deleteUser(int id); }
IUserOperation為操作接口,函數(shù)名和mybatis的xml配置文件中的操作id名對(duì)應(yīng)。
測試類Test:
package com.mybatis.test; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("Configuration.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } public void getUserByID(int userID) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session .getMapper(IUserOperation.class); User user = userOperation.selectUserByID(userID); if (user != null) { System.out.println(user.getId() + ":" + user.getUserName() + ":" + user.getUserAddress()); } } finally { session.close(); } } public void getUserList(String userName) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session .getMapper(IUserOperation.class); Listusers = userOperation.selectUsersByName(userName); for (User user : users) { System.out.println(user.getId() + ":" + user.getUserName() + ":" + user.getUserAddress()); } } finally { session.close(); } } /** * 增加后要commit */ public void addUser() { User user = new User(); user.setUserAddress("place"); user.setUserName("test_add"); user.setUserAge(30); SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session .getMapper(IUserOperation.class); userOperation.addUser(user); session.commit(); System.out.println("新增用戶ID:" + user.getId()); } finally { session.close(); } } /** * 修改后要commit */ public void updateUser() { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session .getMapper(IUserOperation.class); User user = userOperation.selectUserByID(1); if (user != null) { user.setUserAddress("A new place"); userOperation.updateUser(user); session.commit(); } } finally { session.close(); } } /** * 刪除后要commit. * * @param id */ public void deleteUser(int id) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session .getMapper(IUserOperation.class); userOperation.deleteUser(id); session.commit(); } finally { session.close(); } } public static void main(String[] args) { try { Test test = new Test(); // test.getUserByID(1); // test.getUserList("test1"); // test.addUser(); // test.updateUser(); // test.deleteUser(6); } catch (Exception e) { System.out.println(e.getMessage()); } } }
上述內(nèi)容就是使用Mybatis如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫進(jìn)行增刪改查操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。