3.mybatis注解
在上篇2.mybatis入门实例(一) 连接数据库进行查询的基础上
1.添加Mapper接口:UserMapper接口,并使用mybatis的注解
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; import com.mlxs.mybatis.test1.User; /**
* 采用注解方式
* @author 魅力_小生
*
*/
public interface UserMapper {
@Insert("insert into users(name,age) values(#{name},#{age})")
public int add(User user); @Delete("delete from users where id=#{id}")
public int delete(int id); @Update("update users set name=#{name}, age=#{age} where id=#{id}")
public int update(User user); @Select("select * from users where id=#{id}")
public User get(int id); @Select("select * from users")
public List<User> getAll();
}
封装获取session的静态类:
public class MyBatisUtil { //创建sqlSessionFactory
private static SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(getConfig()); private static Reader getConfig(){
//加载conf.xml文件
try {
return Resources.getResourceAsReader("conf.xml");
} catch (IOException e) {
e.printStackTrace();
}
return null;
} /**
* 单例,对外开放,获取session工厂
* @return
*/
public static SqlSessionFactory getSessionFactory(){
return sessionFactory;
}
}
2.在conf.xml的mappers标签中添加上面的Mapper注解接口:
<mapper class="com.mlxs.mybatis.test2.UserMapper" />
3.测试
import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import com.mlxs.mybatis.test1.User;
import com.mlxs.mybatis.util.MyBatisUtil; public class _Test3Annotation { @Test
public void addUser(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
int add = mapper.add(new User(0, "llp", 26));
System.out.println("add--->"+add);
session.close();
}
@Test
public void delUser(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
int del = mapper.delete(6);
System.out.println("del--->"+del);
session.close();
}
@Test
public void updateUser(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
int update = mapper.update(new User(7, "llp2", 27));
System.out.println("update--->"+update);
session.close();
}
@Test
public void getUser(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.get(2);
System.out.println("user--->"+user);
session.close();
}
@Test
public void getAll(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.getAll();
System.out.println("list--->"+list);
session.close();
} }
3.mybatis注解的更多相关文章
- mybatis注解详解
首 先当然得下载mybatis-3.0.5.jar和mybatis-spring-1.0.1.jar两个JAR包,并放在WEB-INF的lib目录下 (如果你使用maven,则jar会根据你的pom配 ...
- mybatis 注解快速上手
一.mybatis 简单注解 关键注解词 : @Insert : 插入sql , 和xml insert sql语法完全一样 @Select : 查询sql, 和xml select sql语法完全一 ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- mybatis注解开发,动态sql
在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...
- 使用mybatis注解@Options实现添加记录时返回主键值
官网:http://www.mybatis.org/mybatis-3/index.html 在使用mybatis作为ORM框架时,我通常更喜欢使用注解而非xml配置文件的方式.业务场景:添加记录之后 ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...
- Mybatis注解开发模糊查询
Mybatis注解开发模糊查询 一般在使用mybatis时都是采用xml文件保存sql语句 这篇文章讲一下在使用mybatis的注解开发时,如何进行模糊查询 模糊查询语句写法(在@Select注解中) ...
- SpringBoot使用Mybatis注解进行一对多和多对多查询(2)
SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...
- MyBatis注解Annotation介绍及Demo
MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...
随机推荐
- IE11下,IIS7.5不自动生成__doPostBack事件
MS在 2013/10/8 已出补丁 http://www.microsoft.com/zh-cn/download/details.aspx?id=39257 https://support.mic ...
- Serialize Documents with the C# Driver
1.介绍 该文档是1.8版本的C#驱动. 本节教程讨论C#类和BSON文档之间的序列化和反序列化.序列化是将对象映射成一个可以存储在MongoDB中的BSON文档的过程,反序列化是从一个BSON文档重 ...
- 利用OpenStack Rest API 创建镜像
服务端点: image API: POST /v2/images Request1: Method:Post Url: http://192.168.31.147:9292/v ...
- 2>&1 的用法说明
经常关注linux脚本的人,一定看到过 2>&1 这样的用法,最初一定不明白其中的含义以及为什么是这样的一种组合.昨天偶然间再次看到了这个 2>&1 的写法,遂下决心搞明白 ...
- ecshop后台增加模块菜单详细教程(图)
我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...
- sharepoint 开发
1. 客户端界面搜索 <div> 业务员:<).match(reg); ]);return null; } function search() { var k=document.ge ...
- jquery ui 常用(二)(对话框 | 旋转器 | 工具提示框(表单) | 特效(百叶窗) )
一.添加信息的对话框 http://www.w3cschool.cc/try/tryit.php?filename=jqueryui-example-dialog-modal-form. 模态表单 ...
- jquery函数和javascript函数的区别
一.窗口加载:http://www.w3school.com.cn/js/js_library_jquery.asp 在 JavaScript 中,您可以分配一个函数以处理窗口加载事件: JavaSc ...
- linux 网络通信
网络命令 历史上最早的即时通信 1 write linzhiling (内容) ctrl+D结束 这样一封信就写出去了,注意:只有用户在线才可以发送 2 wall (write all) 给所有的用户 ...
- 2016年10月30日 星期日 --出埃及记 Exodus 19:15
2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...