Java通过MyBatis框架对MySQL数据进行增删查改的基本方法
作者:czj4451 时间:2024-01-17 10:27:12
1. 查询
除了单条记录的查询,这里我们来尝试查询一组记录。
IUserMapper接口添加下面方法:
List<User> getUsers(String name);
在User.xml中添加:
<resultMap type="User" id="userList"><!-- type为返回列表元素的类全名或别名 -->
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="address" property="address" />
</resultMap>
<select id="getUsers" parameterType="string" resultMap="userList"><!-- resultMap为上面定义的User列表 -->
select * from `user` where name like #{name}
</select>
测试方法:
@Test
public void queryListTest() {
SqlSession session = sqlSessionFactory.openSession();
try {
IUserMapper mapper = session.getMapper(IUserMapper.class);
List<User> 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<Article> getArticlesByUserId(int id);
(4) 在User.xml中添加:
<resultMap type="com.john.hbatis.model.Article" id="articleList">
<id column="a_id" property="id" />
<result column="title" property="title" />
<result column="content" property="content" />
<association property="user" javaType="User"><!-- user属性映射到User类 -->
<id column="id" property="id" />
<result column="name" property="name" />
<result column="address" property="address" />
</association>
</resultMap>
<select id="getArticlesByUserId" parameterType="int" resultMap="articleList">
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}
</select>
(5)测试方法:
@Test
public void getArticlesByUserIdTest() {
SqlSession session = sqlSessionFactory.openSession();
try {
IUserMapper mapper = session.getMapper(IUserMapper.class);
List<Article> articles = mapper.getArticlesByUserId(1);
for (Article article : articles) {
log.info("{} - {}, author: {}", article.getTitle(), article.getContent(), article.getUser().getName());
}
} finally {
session.close();
}
}
附:
除了在association标签内定义字段和属性的映射外,还可以重用User的resultMap:
<association property="user" javaType="User" resultMap="userList" />
2. 新增
IUserMapper接口添加下面方法:
int addUser(User user);
User.xml添加:
<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id"><!-- useGeneratedKeys指定myBatis使用数据库自动生成的主键,并填充到keyProperty指定的属性上。如果未指定,返回对象拿不到生成的值 -->
insert into user(name,age,address) values(#{name},#{age},#{address})
</insert>
测试方法:
@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 id="updateUser" parameterType="User">
update `user` set name=#{name}, age=#{age}, address=#{address}
where id=#{id}
</update>
测试方法:
@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 id="deleteUser" parameterType="int">
delete from `user` where id=#{id}
</delete>
测试方法:
@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();
}
}
标签:Java,MyBatis,MySQL
0
投稿
猜你喜欢
Python爬取视频时长场景实践示例
2021-08-14 01:32:56
PyQt打开保存对话框的方法和使用详解
2023-05-19 08:23:43
Django + Taro 前后端分离项目实现企业微信登录功能
2023-05-31 18:48:46
Python无损音乐搜索引擎实现代码
2021-01-28 19:19:02
一个PHP的QRcode类与大家分享
2023-06-24 05:39:33
Python实现冒泡,插入,选择排序简单实例
2022-01-07 06:47:25
Python中导入模块的几种方式总结
2023-08-04 17:03:54
SQL Server Agent 服务启动后又停止问题
2024-01-25 21:56:49
Python入门教程(一)Python简单介绍
2023-10-25 03:19:16
CSS Sprites
2007-10-10 13:21:00
python去掉 unicode 字符串前面的u方法
2021-03-02 20:53:33
python win32 简单操作方法
2022-05-23 01:47:42
基于Python编写一个刷题练习系统
2022-10-01 13:28:34
Python实现随机生成任意数量车牌号
2022-08-04 04:53:17
php5.2 Json不能正确处理中文、GB编码的解决方法
2023-10-26 13:49:28
Python程序设计入门(4)模块和包
2023-06-18 11:37:36
几行Python代码爬取3000+上市公司的信息
2022-11-17 22:04:09
在macOS上搭建python环境的实现方法
2021-10-07 07:29:56
Python 中的pass语句语法详析
2023-02-11 03:17:44
Python3中常用的处理时间和实现定时任务的方法的介绍
2022-05-13 09:25:25