Java通过MyBatis框架对MySQL数据进行增删查改的基本方法
Java通过MyBatis框架对MySQL数据进行增删查改的基本方法
1. 查询
除了单条记录的查询,这里我们来尝试查询一组记录。
IUserMapper接口添加下面方法:
List
在User.xml中添加:
<http://http://;id column="id" property="id" />
select * from `user` where name like #{name}
测试方法:
@Test
public void queryListTest() {
SqlSession session = sqlSessionFactory.openSession();
try {
IUserMapper mapper = session.getMapper(IUserMapper.class);
List
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
(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
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();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~