日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mybatis修改mysql变量_Java通过MyBatis框架对MySQL数据进行增删查改的基本方法

發布時間:2025/1/21 数据库 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis修改mysql变量_Java通过MyBatis框架对MySQL数据进行增删查改的基本方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 查詢

除了單條記錄的查詢,這里我們來嘗試查詢一組記錄。

IUserMapper接口添加下面方法: List getUsers(String name);

在User.xml中添加:

select * from `user` where name like #{name}

測試方法: @Test

public void queryListTest() {

SqlSession session = sqlSessionFactory.openSession();

try {

IUserMapper mapper = session.getMapper(IUserMapper.class);

List users = mapper.getUsers("%a%"); // %在sql里代表任意個字符。

for (User user : users) {

log.info("{}: {}", user.getName(), user.getAddress());

}

} finally {

session.close();

}

}

如果聯表查詢,返回的是復合對象,需要用association關鍵字來處理。

如User發表Article,每個用戶可以發表多個Article,他們之間是一對多的關系。

(1) 創建Article表,并插入測試數據: -- Drop the table if exists

DROP TABLE IF EXISTS `Article`;

-- Create a table named 'Article'

CREATE TABLE `Article` (

`id` int NOT NULL AUTO_INCREMENT,

`user_id` int NOT NULL,

`title` varchar(100) NOT NULL,

`content` text NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

-- Add several test records

INSERT INTO `article`

VALUES

('1', '1', 'title1', 'content1'),

('2', '1', 'title2', 'content2'),

('3', '1', 'title3', 'content3'),

('4', '1', 'title4', 'content4');

(2) com.john.hbatis.model.Article類: public class Article {

private int id;

private User user;

private String title;

private String content;

// Getters and setters are omitted

}

(3) 在IUserMapper中添加: List getArticlesByUserId(int id);

(4) 在User.xml中添加:

select u.id, u.name, u.age, u.address, a.id a_id, a.title, a.content

from article a

inner join user u

on a.user_id=u.id and u.id=#{id}

(5)測試方法: @Test

public void getArticlesByUserIdTest() {

SqlSession session = sqlSessionFactory.openSession();

try {

IUserMapper mapper = session.getMapper(IUserMapper.class);

List articles = mapper.getArticlesByUserId(1);

for (Article article : articles) {

log.info("{} - {}, author: {}", article.getTitle(), article.getContent(), article.getUser().getName());

}

} finally {

session.close();

}

}

附:

除了在association標簽內定義字段和屬性的映射外,還可以重用User的resultMap:

2. 新增

IUserMapper接口添加下面方法:

int addUser(User user);

User.xml添加:

insert into user(name,age,address) values(#{name},#{age},#{address})

測試方法: @Test

public void addUserTest() {

User user = new User("Lucy", 102, "Happy District");

SqlSession session = sqlSessionFactory.openSession();

try {

IUserMapper mapper = session.getMapper(IUserMapper.class);

int affectedCount = mapper.addUser(user);

session.commit(); // 默認為不自動提交。調用session.getConnection().getAutoCommit()查看

log.info("{} new record was inserted successfully whose id: {}", affectedCount, user.getId());

} finally {

session.close();

}

}

3. 更新

接口添加方法: int updateUser(User user);

User.xml添加:

update `user` set name=#{name}, age=#{age}, address=#{address}

where id=#{id}

測試方法: @Test

public void updateUserTest() {

SqlSession session = sqlSessionFactory.openSession();

try {

IUserMapper mapper = session.getMapper(IUserMapper.class);

User user = mapper.getUserById(8);

user.setAddress("Satisfied District");

int affectedCount = mapper.updateUser(user); // 除了要修改的屬性外,user的其它屬性也要賦值,否則這些屬性會被數據庫更新為初始值(null或0等),可以先查詢一次,但這樣會增加和數據庫不必要的交互。后面的條件判斷能避免此問題。

log.info("Affected count: {}", affectedCount);

session.commit();

} finally {

session.close();

}

}

4. 刪除

接口添加方法: int deleteUser(int id);

User.xml添加:

delete from `user` where id=#{id}

測試方法: @Test

public void deleteUserTest() {

SqlSession session = sqlSessionFactory.openSession();

try {

IUserMapper mapper = session.getMapper(IUserMapper.class);

int affectedCount = mapper.deleteUser(8);

log.info("Affected count: {}", affectedCount);

session.commit();

} finally {

session.close();

}

}

總結

以上是生活随笔為你收集整理的mybatis修改mysql变量_Java通过MyBatis框架对MySQL数据进行增删查改的基本方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。