上一篇演示了如何使用xml来操作mybatis实现crud,但是大量的xml配置文件的编写是非常烦人的。因此 Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。 首先是创建一个接口。 package com.bird.mybatis.bean;import j
上一篇演示了如何使用xml来操作mybatis实现crud,但是大量的xml配置文件的编写是非常烦人的。因此
Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。
首先是创建一个接口。
package com.bird.mybatis.bean; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; public interface UserMapper { @Insert("insert into users(name, age) values(#{name}, #{age})") public int add(Users user); @Delete("delete from users where id = #{id}") public int deleteById(int id); @Update("update users set name = #{name}, age = #{age} where id = #{id}") public int update(Users user); @Select("select * from users where id = #{id}") public Users getUserById(int id); @Select("select * from users") public List<Users> getAllUsers(); }
<mappers> <mapper resource="com/bird/mybatis/bean/userMapper.xml" /> <mapper class="com.bird.mybatis.bean.UserMapper"/> </mappers>
@Test public void testAdd2() { SqlSession openSession = factory.openSession(); UserMapper mapper = openSession.getMapper(UserMapper.class); mapper.add(new Users(-1,"娃娃",99)); openSession.commit(); openSession.close(); }
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号