mybatis-注解实现crud
1.在接口上注解sql
package com.java1234.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.java1234.model.Student;
public interface StudentMapper {
@Insert("insert into t_student values(null,#{name},#{age})")
public int insertStudent(Student student);
@Update("update t_student set name=#{name},age=#{age} where id=#{id}")
public int updateStudent(Student student);
@Delete("delete from t_student where id=#{id}")
public int deleteStudent(int id);
@Select("select * from t_student where id=#{id}")
public Student getStudentById(Integer id);
@Select("select * from t_student")
@Results(
{
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age")
}
)
public List<Student> findStudents();
}
2.测试:
package com.java1234.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.java1234.mappers.StudentMapper;
import com.java1234.model.Student;
import com.java1234.util.SqlSessionFactoryUtil;
public class StudentTest {
private static Logger logger=Logger.getLogger(StudentTest.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
/**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
/**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testInsert() {
logger.info("添加学生");
Student student=new Student("琪琪",11);
studentMapper.insertStudent(student);
sqlSession.commit();
}
@Test
public void testUpdate() {
logger.info("更新学生");
Student student=new Student(6,"琪琪2",12);
studentMapper.updateStudent(student);
sqlSession.commit();
}
@Test
public void testDelete() {
logger.info("删除学生");
studentMapper.deleteStudent(6);
sqlSession.commit();
}
@Test
public void testGetById() {
logger.info("通过ID查找学生");
Student student=studentMapper.getStudentById(1);
System.out.println(student);
}
@Test
public void testFindStudents() {
logger.info("查找所有学生");
List<Student> studentList=studentMapper.findStudents();
for(Student student:studentList){
System.out.println(student);
}
}
}
mybatis-注解实现crud的更多相关文章
- mybatis 注解开发CRUD
mybatis 的常用注解说明 @Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与@Res ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_4 mybatis注解开发CRUD的其他操作
delete 51已经被删除掉了. 查询一个 findUserByName模糊查询 带百分号的情况 value这个参数是固定的 返回值为int类型的
- mybatis(二)执行CRUD操作的两种方式配置和注解
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- Mybatis注解开发单表CRUD
Mybatis注解开发单表CRUD mybatis注解开发和xml开发不可兼容,要么全部使用注解,要么全部使用xml,个人建议注解,简单. 当实体类属性名称和数据库表属性名称一致时:无需配置Resul ...
- 使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...
- MyBatis入门学习教程-使用MyBatis对表执行CRUD操作
上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...
- MyBatis学习总结_02_使用MyBatis对表执行CRUD操作
一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...
随机推荐
- 释放License命令
lmutil_x86.exe lmremove -c 1055@Vappcloud-WinS acfd Administrator VappCloud_Win7a.vancloud-corp.com ...
- Free Style Structure text
最近用了很多文本配置输入,考虑一个最简单的格式,适合C语言scanf读写数据. 基本数据类型直接使用常用形式. double 直接用小数,对应%f读取. int 直接用十进制整数,对应%d读取. ch ...
- 利用MVC的Area作为二级域名
此处使用的域名是我改了系统的hosts文件达到的 测试成功! 全局的注册方式 在Area的注册文件里进行配置 一个Area和一个外部的Controller 废话不多说,提供DEMO 下载地址
- Linux——DNS
正向解析和逆向解析 正向域名解析,即DNS解析,是通过域名查询IP的解析方式. 逆向域名解析,即反向DNS解析,是通过IP地址查询域名. [root@localhost ~]# cp /var ...
- [FZU 1901]Period II KMP
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- Openjudge jubeeeeeat
jubeeeeeat 题目链接 总时间限制: 1000ms 内存限制: 256000kB 描述 众所周知,LZF很喜欢打一个叫Jubeat的游戏.这是个音乐游戏,游戏界面是4×4的方阵,会根据音乐 ...
- [Xcode 实际操作]五、使用表格-(2)设置UITableView单元格高度
目录:[Swift]Xcode实际操作 本文将演示如何制作一个自定义行高的表格视图 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...
- oracle 重做日志
原创转载请注明出处 重做日志:记录数据库数据的变化(DDL,DML) 重做日志组:由一个或者多个完全一样的重做日志文件组成,如果一个日志组有多个日志文件,后台进程LGWR会把事务变化写到同一个日志组的 ...
- Python+Selenium----处理登录图片验证码
1.说明 在做自动化测试的时候,经常会遇到登录,其中比较麻烦的就是验证码的处理,现在比较常用的图形验证码,每次刷新,得到的验证码不一致,所以,一般来说,获取验证码图片有两种方式: (1)拿到验证码的图 ...
- Java集合——集合框架Set接口
1.Set接口 一个不包含重复元素的collecyion.更确切的讲,set不包含满足e1.equals(e2)的元素e1和e2,并且最多包含一个null元素. 2.HashSet 类实现Set接口, ...