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 原创作品,允 ...
随机推荐
- Linux的视频编程(V4L2编程)【转】
本文转载自:http://blog.csdn.net/tommy_wxie/article/details/11472073 一.什么是video4linuxVideo4linux2(简称V4L2), ...
- php用jquery-ajax上传多张图片限制图片大小
php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...
- mysql中时间类型datetime,timestamp与int的区别
在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...
- Delphi内嵌汇编语言BASM精要(转帖)
1 BASM概念简要 汇编语句由指令和零至三个表达式构成.表达式由常数(立即数).寄存器和标识符构成.例如: movsb // 单指令语句 jmp @Here // 一个表达式: ...
- Hibernate,JPA注解@Embeddable
JPA嵌入式对象(又名组件) 在实体中可以定义一个嵌入式组件(embedded component), 甚至覆盖该实体中原有的列映射. 组件类必须在类一级定义@Embeddable注解. 在特定的实体 ...
- PHP易混淆函数的区分
常量定义自定义常量 常量名区分大小写系统的魔术常量不区分大小写 __DIR__ __dir__变量定义变量名是区分大小写变量名声明时用$符号开头, 而且要符合变量名的命名规则$i;var_dump($ ...
- Unix/Linux编程实践教程(一:进程、管道)
execvp在程序中启动新程序: 用fork创建新进程: forkdemo2代码: 测试fork的时候参考<Linux权威指南>阅读笔记(3) 使用了patch: [root@local ...
- php变量内存完全释放
<?php echo memory_get_usage().PHP_EOL;$a = 1;$b = $a;$a = null;$b = null;unset($a);unset($b);echo ...
- js认清this的第一步
学习 this 的第一步是明白 this 既不指向函数自身也不指向函数的词法作用域, 你也许被这样的解释误导过, 但其实它们都是错误的.this 实际上是在函数被调用时发生的绑定, 它指向什么完全取决 ...
- ubuntu-E:Encountered a section with no Package: header的解决办法
刚才打开ubuntu,我的版本是12.04.正想使用sudo apt-get install build-essential 时,出现了如下错误: E:Encountered a section wi ...