首页 技术 正文
技术 2022年11月15日
0 收藏 820 点赞 2,311 浏览 2356 个字

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);
}
}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845