Singerw's Repository Singerw's Repository
首页
  • 相关文章

    • HTML相关文章
    • CSS相关文章
    • JavaScript相关文章
  • 学习笔记

    • JavaScript笔记
    • ES6笔记
    • Vue笔记
  • 相关文章

    • Spring相关文章
    • SpringBoot相关文章
    • MyBatis相关文章
    • MySQL相关文章
  • 学习笔记

    • SpringBoot笔记
    • Spring笔记
    • MyBatis笔记
    • MySQL笔记
    • JavaWeb笔记
    • JavaCore笔记
  • 学习笔记

    • Linux笔记
    • Git笔记
    • 技术文档
  • 偏门技术

    • GitHub技巧
    • 博客搭建
    • 科学上网
  • 安装教程

    • JDK
    • MySQL
    • Node.js
    • Linux
  • 终身学习
  • 面试人生
  • 心情杂货
  • 生活随笔
  • 归档
  • 标签
GitHub (opens new window)

Singerw

谁能够凭爱意将富士山私有
首页
  • 相关文章

    • HTML相关文章
    • CSS相关文章
    • JavaScript相关文章
  • 学习笔记

    • JavaScript笔记
    • ES6笔记
    • Vue笔记
  • 相关文章

    • Spring相关文章
    • SpringBoot相关文章
    • MyBatis相关文章
    • MySQL相关文章
  • 学习笔记

    • SpringBoot笔记
    • Spring笔记
    • MyBatis笔记
    • MySQL笔记
    • JavaWeb笔记
    • JavaCore笔记
  • 学习笔记

    • Linux笔记
    • Git笔记
    • 技术文档
  • 偏门技术

    • GitHub技巧
    • 博客搭建
    • 科学上网
  • 安装教程

    • JDK
    • MySQL
    • Node.js
    • Linux
  • 终身学习
  • 面试人生
  • 心情杂货
  • 生活随笔
  • 归档
  • 标签
GitHub (opens new window)
  • MyBatis学习笔记

    • 《MyBatis》学习笔记
    • MyBatis简介
    • 第一个MyBatis程序
    • 生命周期和作用域
    • 配置文件之基本优化
    • XML实现CRUD
    • Annotation实现CRUD
      • 1、增加操作
      • 2、删除操作
      • 3、修改操作
      • 4、查询操作
    • 万能Map
    • Mapper-XML文件
    • ResultMat的使用
    • 实现日志功能
    • MyBatis动态SQL
    • 实现分页功能
    • MyBatis缓存
    • MyBatis中配置文件示例
    • MyBatis工具类
    • generator插件自动生成代码
  • MyBatis-Plus学习笔记

  • 《MyBatis》学习笔记
  • MyBatis学习笔记
Singerw
2021-09-25

Annotation实现CRUD

# MyBatis实现注解形式的CURD

# 1、增加操作

【示例:UserMapper.java】

public interface UserMapper {

    @Insert(value = "INSERT INTO g_users VALUE (NULL,#{username},#{userphone},#{userpassword},0,now(),now(),1)")
    boolean addUser(User user);

}
1
2
3
4
5
6

【示例:UserMapperTest.java】

public class UserMapperTest {

    private SqlSession sqlSession = MyBatisUtils.getSqlSession();
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Test
    public void addUser() {
        User user = new User("张欣", "5465432543453", "assam1314520");
        boolean flag = userMapper.addUser(user);
        sqlSession.commit();
        System.out.println(flag ? "增加成功" : "增加失败");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2、删除操作

【示例:UserMapper.java】

public interface UserMapper {

    @Delete(value = "DELETE FROM goku.g_users WHERE userid=#{userid}")
    boolean delUser(int userid);
}
1
2
3
4
5

【示例:UserMapperTest.java】

public class UserMapperTest {

    private SqlSession sqlSession = MyBatisUtils.getSqlSession();
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Test
    public void delUser() {
        boolean flag = userMapper.delUser(15);
        //必须要提交
        sqlSession.commit();
        System.out.println(flag ? "删除成功" : "删除失败");
        sqlSession.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 3、修改操作

【示例:UserMapper.java】

public interface UserMapper {

    @Update(value = "UPDATE g_users SET username=#{username},userphone=#{userphone},userpassword=#{userpassword},jurisdiction=#{jurisdiction},userstatus=#{userstatus} WHERE userid=#{userid}")
    boolean updateUser(@Param("username") String username, @Param("userphone") String userphone, @Param("userpassword") String userpassword, @Param("jurisdiction") int jurisdiction, @Param("userstatus") int userstatus, @Param("userid") int userid);


}
1
2
3
4
5
6
7

【示例:UserMapperTest.java】

public class UserMapperTest {

    private SqlSession sqlSession = MyBatisUtils.getSqlSession();
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Test
    public void updateUser() {
        boolean flag = userMapper.updateUser("测试测试测试", "1444444444", "123456", 1, 1, 5);
        //必须要提交
        sqlSession.commit();
        System.out.println(flag ? "修改成功" : "修改失败");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 4、查询操作

【示例:UserMapper.java】

public interface UserMapper {
    
    @Select(value = "select * from goku.g_users")
    List<User> getUsers();

    @Select(value = "select * from goku.g_users where userid = #{userid}")
    User getUser(int userid);
}
1
2
3
4
5
6
7
8

【示例:UserMapperTest.java】

public class UserMapperTest {

    private SqlSession sqlSession = MyBatisUtils.getSqlSession();
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Test
    public void getUsers() {
        List<User> users = userMapper.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void getUser() {
        User user = userMapper.getUser(2);
        System.out.println(user);
        sqlSession.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
编辑 (opens new window)
#MyBatis
XML实现CRUD
万能Map

← XML实现CRUD 万能Map→

最近更新
01
Maven资源导出问题终极版
10-12
02
《MyBatis-Plus》学习笔记
10-07
03
MyBatis-Plus—配置日志
10-07
更多文章>
Theme by Vdoing | Copyright © 2020-2021 版权所有 | repository.singerw.com
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×