Mybatis之使用注解开发CRUD
2017-10-08 13:40
337 查看
Mybatis之使用注解开发CRUD
1、创建接口
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();
}
2、在配置文件中注册
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
<mapper class="com.bird.mybatis.bean.UserMapper"/>
</mappers>
3、使用
@Test
public void testAdd2() {
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.add(new Users(-1,"娃娃",99));
openSession.commit();
openSession.close();
} 转载地址:http://blog.csdn.net/a352193394/article/details/39940259
1、创建接口
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();
}
2、在配置文件中注册
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
<mapper class="com.bird.mybatis.bean.UserMapper"/>
</mappers>
3、使用
@Test
public void testAdd2() {
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.add(new Users(-1,"娃娃",99));
openSession.commit();
openSession.close();
} 转载地址:http://blog.csdn.net/a352193394/article/details/39940259
相关文章推荐
- Mybatis之使用注解开发CRUD
- Mybatis之使用注解开发CRUD
- Mybatis之使用注解开发CRUD
- Mybatis使用注解开发
- mybatis使用注解编写crud后,不配置mapping方法。
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
- 使用MyBatis(七)使用java注解完成CRUD
- Mybatis使用注解的方式CRUD包括批量插入更新
- 使用MyBatis对表执行CRUD操作——基于注解的实现
- 从零开始学JAVA(09)-使用SpringMVC4 + Mybatis + MySql 例子(注解方式开发)
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
- MyBatis使用注解进行开发
- 使用注解开发 MyBatis
- Struts2零配置开发(注解Annotation的使用)一
- MyBatis 1章 入门(使用MyBatis完成CRUD)
- 使用mybatis实现CRUD
- Java Persistence with MyBatis 3(中文版) 第四章 使用注解配置SQL映射器