J2EE系列之MyBatis学习笔记(十)-- 使用注解配置sql映射器
来源:互联网 发布:知金教育保过 编辑:程序博客网 时间:2024/05/16 02:42
前面的工程中我们都是使用的xml文件配置的sql映射器(比如StudentMapper.xml文件,就是配置Student相关的sql映射器),当然还可以使用注解的方式配置sql映射器(Hibernate也有使用注解进行配置)。以后的项目中还是推荐使用xml文件配置。
使用注解方式配置sql映射器比较方便,但是这种方式不够灵活,有一些功能实现不了,还是得使用映射文件。所以以后还是使用xml方式配置。这里讲使用注解方式了解一下即可。
一、使用注解方式的基本映射语句有@Insert,@Delete,@Update,@Select
新建工程MyBatisPro03,按照前面讲过的方法配置好mybatis。
1.修改t_student表为:
这里删除了其它的字段。
2.新建类Student:
package com.test.model;import java.util.Arrays;public class Student {private Integer id;private String name;private Integer age;public Student(String name, Integer age) {super();this.name = name;this.age = age;}public Student() {super();// TODO Auto-generated constructor stub}public Student(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}
3.新建接口StudentMapper:
package com.test.mappers;import java.util.List;import java.util.Map;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.session.RowBounds;import com.test.model.Student;public interface StudentMapper {public int insertStudent(Student student);public int updateStudent(Student student);public int deleteStudent(Integer id);public Student findStudentById(Integer id);public List<Student> findStudents();}
这里只有添加、修改、删除、查询所有学生四种方法。
4.使用注解方式配置sql映射器的话,就不需要xml文件了。使用注解方式实现添加学生,对StudentMapper添加注解如下:
package com.test.mappers;import java.util.List;import java.util.Map;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.session.RowBounds;import com.test.model.Student;public interface StudentMapper {@Insert("insert into t_student values(null,#{name},#{age})")public int insertStudent(Student student);public int updateStudent(Student student);public int deleteStudent(Integer id);public List<Student> findStudents();}这里把@Insert注解添加到添加学生方法的上面即可,这个注解里面直接是要运行的sql语句。
添加测试方法:
package com.test.service;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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.test.mappers.StudentMapper;import com.test.model.Student;import com.test.util.SqlSessionFactoryUtil;public class StudentTest {private static Logger logger = Logger.getLogger(StudentTest.class);private SqlSession sqlSession = null;private StudentMapper studentMapper = null;@Beforepublic void setUp() throws Exception {sqlSession = SqlSessionFactoryUtil.openSession();studentMapper = sqlSession.getMapper(StudentMapper.class);}@Afterpublic void tearDown() throws Exception {sqlSession.close();}@Testpublic void testInsert() {logger.info("注解方式添加学生");Student student = new Student("小七",17);studentMapper.insertStudent(student);sqlSession.commit();}}
运行测试方法testInsert,学生信息添加到了数据库中。
5.同理使用注解实现更新学生:
@Update("update t_student set name=#{name},age=#{age} where id=#{id}")public int updateStudent(Student student);
添加测试方法:
@Testpublic void testUpdate() {logger.info("注解方式更新学生");Student student = new Student(7,"小七2",20);studentMapper.updateStudent(student);sqlSession.commit();}运行测试方法,更新了数据库中相关数据。
6.使用注解实现删除学生:
@Delete("delete from t_student where id=#{id}")public int deleteStudent(Integer id);
添加测试方法:
@Testpublic void testDelete() {logger.info("注解方式删除学生");studentMapper.deleteStudent(7);sqlSession.commit();}
运行测试方法,实现了学生信息的删除。
7.通过Id查询学生:
@Select("select * from t_student where id=#{id}")public Student findStudentById(Integer id);
添加测试方法:
@Testpublic void testGetById() {logger.info("注解方式通过id查找学生");Student student = studentMapper.findStudentById(1);System.out.println(student);}查询学生就用不到提交事务了。运行测试函数可以查询到相应的学生信息。
8.查询所有学生:使用xml文件配置的时候,查询所有学生需要定义一个结果映射。这里使用注解方式的话,通过如下方式进行配置:
@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();
添加测试方法:
@Testpublic void testFindStudents() {logger.info("注解方式查找所有学生");List<Student> studentList = studentMapper.findStudents();for(Student s:studentList){System.out.println(s);}}
运行测试方法,查询到了所有学生的信息。
阅读全文
0 0
- J2EE系列之MyBatis学习笔记(十)-- 使用注解配置sql映射器
- J2EE系列之MyBatis学习笔记(十一)-- 使用注解配置sql映射器
- J2EE系列之MyBatis学习笔记(十二)-- 使用注解配置sql映射器
- J2EE系列之MyBatis学习笔记(三)-- 使用XML配置SQL映射器
- MyBatis学习笔记(六)--使用注解配置SQL映射器
- MyBatis学习笔记(十)注解映射
- J2EE系列之MyBatis学习笔记(二)-- mybatis配置
- J2EE系列之MyBatis学习笔记(四)-- mybatis关系映射(一对一映射)
- J2EE系列之MyBatis学习笔记(五)-- mybatis关系映射(一对多映射)
- J2EE系列之MyBatis学习笔记(六)-- 动态sql
- J2EE系列之MyBatis学习笔记(七)-- 动态sql
- Mybatis(十)-注解配置SQL映射器(一)
- MyBatis学习笔记(三)--使用XML配置SQL映射器
- J2EE系列之Hibernate4学习笔记(十三)-- -一对一映射
- J2EE系列之Hibernate4学习笔记(十)--Hibernate继承关系映射(每个具体类对应一个表)
- J2EE系列之MyBatis学习笔记(一)-- 简介
- J2EE系列之MyBatis学习笔记(八)-- 杂项
- J2EE系列之MyBatis学习笔记(九)-- 杂项
- SAP 系统 MIRO 时替代 Recon. Account 的解决方案
- vue之watch用法
- 线程
- sklearn数据集随机切分(train_test_split)
- CentOS下配置完全分布式hadoop2.6.0-cdh5.6.0(1)
- J2EE系列之MyBatis学习笔记(十)-- 使用注解配置sql映射器
- unity _game窗口图片精灵拖拽(背包物品,或者技能)
- git
- Android 生成系统签名的KeyStore 转
- .NET、C#和ASP.NET之间的区别
- websphere服务(node agent,deploy manager)的启动问题
- 在linux上安装nginx
- 内部排序算法:希尔排序
- Struts2 的入门