话不多说,直接上代码!
实体类
User.java
@Data
public class User {
private String userId;
private String username;
private String password;
private String age;
}
数据操作层
UserDao.java
@Mapper
public interface UserDao {
/**
* 新增
*/
Integer insert(User user);
/**
* 动态新增
*/
Integer insertUser(User user);
/**
* 批量新增
*/
Integer insertUserList(List users);
/**
* 删除
*/
Integer deleteByUsername(String userId);
/**
* 根据userID批量删除
*/
Integer batchDeleteByUserId(List userId);
/**
* 查询
*/
User selectByUsername(String username);
/**
* 模糊查询
*/
List selectUser(User user);
/**
* 根据用户Id批量查询
*/
List selectByUserIdList(List ids);
/**
* 更新
*/
Integer update(String userId, String password);
/**
* 动态更新
*/
Integer updateUser(User user);
mapper
UserMapper.xml
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert
into msk.user(userId, username, password, sex)
values(#{userId}, #{username}, #{password}, #{sex})
insert into msk.user
username,
password,
sex,
username = #{username, jdbcType="VARCHAR"}
password = #{password, jdbcType="VARCHAR"}
sex = #{sex, jdbcType="VARCHAR"}
insert into msk.user
(userId, username, password, sex)
values
(
#{user.userId}, #{user.username}, #{user.password}, #{user.sex}
)
delete msk.user where userId=#{userId}
delete
from msk.user
where userId in
(
#{userId}
)
select
userId, username, password, sex
from msk.user where username=#{username}
select
userId, username, password, sex
from msk.user
where 1=1
and username like '%' || #{username} || '%'
and password like '%' || #{password} || '%'
and sex like '%' || #{sex} || '%'
select
userId, username, password, sex
from msk.user
where userId in
#{userId}
update
msk.user
set password = #{password}
where userId=#{userId}
update msk.user
username = #{username, jdbcType="VARCHAR"}
password = #{password, jdbcType="VARCHAR"}
sex = #{sex, jdbcType="VARCHAR"}
where userId = #{userId, jdbcType=VARCHAR}
以上这些日常操作基本够用,其他数据库基本也差不多是这样,根据需要稍作修改即可。如有不足,还请指正