• 增删改查


    MyBatis 映射并不能用注解来构建

    1|11. select

    接口方法声明:

    import com.yyh.pojo.User; import org.apache.ibatis.annotations.Select; public interface UserMapper2 { @Select("select * from kuser where id = #{id}") public User selectUserByID(int id); }

    测试方法:

    @Test public void selectTest(){ SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); User user = mapper.selectUserByID(2); System.out.println(user); session.close(); }

    不要忘记注册这个接口到全局配置中

    <mappers> <mapper class="com.yyh.mapper.UserMapper2"/> </mappers>

    1|22. sql语句多个参数

    接口方法声明:

    @Select("select * from kuser where sex = #{sex} and name like '%${name}%'") public List<User> selectUserBySexAndName(@Param("sex") String sex, @Param("name") String name);

    测试方法:

    @Test public void selectTest2(){ SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); List<User> users = mapper.selectUserBySexAndName("1","张"); System.out.println(users); session.close(); }

    当sql语句需要多个参数时则必须将多个参数打包到一个对象中,通常是POJO或Map,上面的案例中使用了@Param注解本质就是告诉MyBatis有多个参数MyBatis会自动创建一个Map,然后将@Param的值作为Key,然后将Map传给sql,你也可以手动传一个Map

    接口方法声明:

    @Select("select * from kuser where sex = #{sex} and name like '%${name}%'") public List<User> selectUserBySexAndName(Map<String,Object> params);

    测试方法:

    @Test public void selectTest2(){ SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); Map<String,Object> params = new HashMap<>(); //存储参数的Map params.put("sex","1"); params.put("name","张"); List<User> users = mapper.selectUserBySexAndName(params); System.out.println(users); session.close(); }

    1|33. insert

    接口方法声明:

    @Insert("insert into kuser values(null,#{name},#{birthday},#{sex},#{address})") public int insertUser(User user);

    测试方法:

    @Test public void insertTest() { SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); User user = new User(); user.setName("jerry"); user.setSex("1"); user.setAddress("亚洲中国"); user.setBirthday(new Date()); mapper.insertUser(user); session.commit(); session.close(); }

    1|0获取自增主键

    在@select注解的下方添加@selectKey注解来完成对自增主键的获取

    @Insert("insert into kuser values(null,#{name},#{birthday},#{sex},#{address})") @SelectKey(statement = "select last_insert_id()",keyProperty = "id",resultType = Integer.class,before = false) public int insertUser(User user);

    在执行insert之后执行last_insert_id()函数获取自增值,然后放入对象的某个属性中;

    1|44. update

    接口方法声明:

    @Update("update kuser set name = #{name}, birthday = #{birthday}, sex = #{sex}, address = #{address} where id = #{id}") public int updateUser(User u); // 如果一行太长可以换行些 但是要注意sql语法

    测试方法:

    @Test public void updateTest() { SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); User user = mapper.selectUserByID(1); user.setName("王六子"); mapper.updateUser(user); session.commit(); session.close(); }

    1|55.delete

    接口方法声明:

    @Delete("delete from kuser where id = #{id}") public int deleteUserByID(int id);

    测试方法:

    @Test public void deleteTest() { SqlSession session = factory.openSession(); UserMapper2 mapper = session.getMapper(UserMapper2.class); mapper.deleteUserByID(12); session.commit(); session.close(); }
  • 相关阅读:
    Array对象---添加或删除数组中的元素->splice()
    微信小程序-文本作用域
    微信小程序-模块化
    属性动画的核心方法:ValueAnimator.ofInt(int... values)
    关于原生+WebView js交互、数据传输问题
    Beam Search快速理解及代码解析
    vue项目启动报错 spawn cmd ENOENT errno
    TortoiseGit提交每次都需要输入账号密码的解决办法
    Grpc.Core.RpcException: Status(StatusCode=DeadlineExceeded, Detail="Deadline Exceeded")
    公司限制网络,不能访问b站..etc,怎么办?搞起来
  • 原文地址:https://www.cnblogs.com/huaobin/p/14162752.html
Copyright © 2020-2023  润新知