在上篇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注解的更多相关文章

  1. mybatis注解详解

    首 先当然得下载mybatis-3.0.5.jar和mybatis-spring-1.0.1.jar两个JAR包,并放在WEB-INF的lib目录下 (如果你使用maven,则jar会根据你的pom配 ...

  2. mybatis 注解快速上手

    一.mybatis 简单注解 关键注解词 : @Insert : 插入sql , 和xml insert sql语法完全一样 @Select : 查询sql, 和xml select sql语法完全一 ...

  3. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  4. mybatis注解开发,动态sql

    在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...

  5. 使用mybatis注解@Options实现添加记录时返回主键值

    官网:http://www.mybatis.org/mybatis-3/index.html 在使用mybatis作为ORM框架时,我通常更喜欢使用注解而非xml配置文件的方式.业务场景:添加记录之后 ...

  6. 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 ...

  7. SpringBoot使用Mybatis注解开发教程-分页-动态sql

    代码示例可以参考个人GitHub项目kingboy-springboot-data 一.环境配置 1.引入mybatis依赖 compile( //SpringMVC 'org.springframe ...

  8. Mybatis注解开发模糊查询

    Mybatis注解开发模糊查询 一般在使用mybatis时都是采用xml文件保存sql语句 这篇文章讲一下在使用mybatis的注解开发时,如何进行模糊查询 模糊查询语句写法(在@Select注解中) ...

  9. SpringBoot使用Mybatis注解进行一对多和多对多查询(2)

    SpringBoot使用Mybatis注解进行一对多和多对多查询 GitHub的完整示例项目地址kingboy-springboot-data 一.模拟的业务查询 系统中的用户user都有唯一对应的地 ...

  10. MyBatis注解Annotation介绍及Demo

     MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...

随机推荐

  1. GPS模块的AT指令集

    AT+CPIN? 查询sim卡状态. 接着: AT+CGCLASS="B"AT+CGDCONT=1,"IP","CMNET"AT+CGATT ...

  2. Linux之awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  3. Backup: Numbers in Perl6

    Perl6 is a new language, not a improved version of Perl5. Perl6 inherits many good features from man ...

  4. Temporary InMemory Tables [AX 2012]

    Temporary InMemory Tables [AX 2012] This topic has not yet been rated - Rate this topic Updated: Oct ...

  5. 使用BBCP来提升跨互联网的数据传输速度

    背景介绍: 目前项目在美国东西部以及欧洲都有服务器节点,跨互联网的数据传输速度很不稳定,之前我们主要是通过SCP以及Rsync等方式进行数据传输的. 无意间发现了BBCP这个软件之后,经过测试,效果非 ...

  6. Redis群集实现Asp.net Mvc分布式Session

    Session的缺点 众所周知Asp.net Session默认存储在IIS中,IIS的重启会导致Session丢失. 如果你的网站使用了session,当网站并发过大时可能引起溢出. 配置Redis ...

  7. xtjh

    <div onkeydown="javascript:if(event.keyCode==13) search()"> <br > ISBN<span ...

  8. css弹性盒子学习

    css3弹性盒子是一种布局方式,在适应不同的屏幕大小的时候,能够确保元素拥有更恰当的排布行为.它只是视觉呈现上的,即显示顺序适应显示空间,与源代码无关,源代码顺序不受影响. 定义一个弹性盒子: 在cs ...

  9. css杂记

    1,font-variant: 设置文本是否为小型的大写字母,值可以为normal,small-caps; 2,a:link: 未访问过的 a:visited: 访问过的 a:active: 活动的链 ...

  10. mini.open打开窗口时传递参数

    mini.open({ url: "xxx.html", showMaxButton: false, allowResize: false, title: '标题', width: ...