通过MyBatis进行CRUD操作
项目结构
|-- mybatis-01
|-- mybatis-01.iml
|-- pom.xml
|-- src
| |-- main
| | |-- java
| | | |-- com
| | | |-- sky
| | | |-- dao
| | | | |-- UserMapper.java
| | | | |-- UserMapper.xml
| | | |-- pojo
| | | | |-- User.java
| | | |-- utils
| | | |-- MybatisUtils.java
| | |-- resources
| | |-- mybatis-config.xml
| |-- test
| |-- java
| |-- com
| |-- sky
| |-- dao
| |-- UserMapperTest.java
数据库
---- -------------- ----------
| id | name | password |
---- -------------- ----------
| 1 | 鸡你太美 | Music |
| 2 | 影流之主 | 1234 |
---- -------------- ----------
以下对应属性均在mapper.xml中
namespace
- tips:namespace中的包名要和Dao/mapper接口的包名保持一致
select
- 选择查询语句
- select语句有很多属性可以详细配置每一条SQL语句
- id
- 与接口文件中所写方法一一对应
- 命名空间中唯一的标识符
- parameterType
- 传入SQL语句中的参数类型 (int,map,com.sky.pojo.User))
- resultType
- SQL语句返回的参数类型 (类型同parameterType))
- id
需求:根据ID查询用户
实现:
- 在UserMapper接口中添加相应方法
package com.sky.dao;
import com.sky.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
// 根据id查询用户信息
User selectUserById(int id);
}
- 在UserMapper.xml中添加select标签语句
<select id="selectUserById" resultType="com.sky.pojo.User">
select * from mybatis.User where id =#{id}
</select>
- 编写测试类
@Test
public void testSelectUserById(){
SqlSession session = MybatisUtils.getSqlSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectUserById(1);
System.out.println(user);
session.close();
}
- 测试
User{id=1, name='鸡你太美', password='Music'}
insert
- 使用insert标签进行插入操作,它的配置和select标签差不多
- id
- 与接口文件中所写方法一一对应
- 命名空间中唯一的标识符
- parameterType
- 传入SQL语句中的参数类型 (int,map,com.sky.pojo.User))
- id
- 一般insert语句除判断是否成功外不需要返回相应数值 故没有设置resultType
需求:给数据库增加一个用户
实现:
- 在UserMapper接口中添加相应方法
// 添加用户信息
int addUser(User user);
- 在UserMapper.xml中添加insert标签语句
<insert id="addUser" parameterType="com.sky.pojo.User">
insert into User (id, name, password) values (#{id},#{name},#{password})
</insert>
- 编写测试类
增删改都需要commit
@Test
public void testAddUser(){
//增删改需要commit
SqlSession session = MybatisUtils.getSqlSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = new User(3, "老八秘制小憨堡", "1234");
int i = mapper.addUser(user);
System.out.println(i);
session.commit();
session.close();
}
- 测试
返回数值:1
查看数据库
---- ----------------------- ----------
| id | name | password |
---- ----------------------- ----------
| 1 | 鸡你太美 | Music |
| 2 | 影流之主 | 1234 |
| 3 | 老八秘制小憨堡 | 1234 |
---- ----------------------- ----------
update
- 使用update标签进行更新操作,它的配置和select标签差不多
- id
- 与接口文件中所写方法一一对应
- 命名空间中唯一的标识符
- parameterType
- 传入SQL语句中的参数类型 (int,map,com.sky.pojo.User))
- id
- 一般update语句除判断是否成功外不需要返回相应数值 故没有设置resultType
需求:修改用户的信息
实现:
- 在UserMapper接口中添加相应方法
//更新用户信息
int updateUser(User user);
- 在UserMapper.xml中添加update标签语句
<update id="updateUser" parameterType="com.sky.pojo.User">
update User set name=#{name}, password=#{password} where id= #{id}
</update>
- 编写测试类
同样需要commit
@Test
public void testUpdateUser(){
SqlSession session = MybatisUtils.getSqlSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.selectUserById(2);
user.setPassword("难道这就是你分手的接口💃💃💃");
int i = mapper.updateUser(user);
System.out.println(i);
session.commit();
session.close();
}
4.测试
返回数值:1
查看数据库
-----------------------------------------------
| id | name | password |
-----------------------------------------------
| 1 | 鸡你太美 | Music |
| 2 | 影流之主 |难道这就是你分手的接口💃💃💃 |
| 3 | 老八秘制小憨堡| 1234 |
-----------------------------------------------
delete
- 使用delete标签进行删除操作,它的配置和select标签差不多
- id
- 与接口文件中所写方法一一对应
- 命名空间中唯一的标识符
- parameterType
- 传入SQL语句中的参数类型 (int,map,com.sky.pojo.User))
- id
- 一般delete语句除判断是否成功外不需要返回相应数值 故没有设置resultType
需求:根据id删除一个用户
实现:
- 在UserMapper接口中添加相应方法
//删除用户信息
int deleteUser(int id);
- 在UserMapper.xml中添加delete标签语句
<delete id="deleteUser" parameterType="int">
delete from mybatis.User where id= #{id}
</delete>
- 编写测试类
同commit
@Test
public void testDeleteUser(){
SqlSession session = MybatisUtils.getSqlSession();
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.deleteUser(3);
System.out.println(i);
session.commit();
session.close();
- 测试
返回数值:1
查看数据库
-----------------------------------------------
| id | name | password |
-----------------------------------------------
| 1 | 鸡你太美 | Music |
| 2 | 影流之主 |难道这就是你分手的接口💃💃💃 |
-----------------------------------------------
注意:
1. 除select查询语句之外,增删改操作都需要用sqlseeion调用commit方法,否则修改不会生效。
2. 注意每个属性需要一一对应
本文由博客一文多发平台 OpenWrite 发布!