J2EE系列之MyBatis学习笔记(十)-- 使用注解配置sql映射器
2017-06-15 11:32
627 查看
前面的工程中我们都是使用的xml文件配置的sql映射器(比如StudentMapper.xml文件,就是配置Student相关的sql映射器),当然还可以使用注解的方式配置sql映射器(Hibernate也有使用注解进行配置)。以后的项目中还是推荐使用xml文件配置。
使用注解方式配置sql映射器比较方便,但是这种方式不够灵活,有一些功能实现不了,还是得使用映射文件。所以以后还是使用xml方式配置。这里讲使用注解方式了解一下即可。
一、使用注解方式的基本映射语句有@Insert,@Delete,@Update,@Select
新建工程MyBatisPro03,按照前面讲过的方法配置好mybatis。
1.修改t_student表为:
这里删除了其它的字段。
2.新建类Student:
3.新建接口StudentMapper:
这里只有添加、修改、删除、查询所有学生四种方法。
4.使用注解方式配置sql映射器的话,就不需要xml文件了。使用注解方式实现添加学生,对StudentMapper添加注解如下:
添加测试方法:
运行测试方法testInsert,学生信息添加到了数据库中。
5.同理使用注解实现更新学生:
添加测试方法:
6.使用注解实现删除学生:
添加测试方法:
运行测试方法,实现了学生信息的删除。
7.通过Id查询学生:
添加测试方法:
8.查询所有学生:使用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; } @Override public 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; @Before public void setUp() throws Exception { sqlSession = SqlSessionFactoryUtil.openSession(); studentMapper = sqlSession.getMapper(StudentMapper.class); } @After public void tearDown() throws Exception { sqlSession.close(); } @Test public 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);
添加测试方法:
@Test public 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);
添加测试方法:
@Test public void testDelete() { logger.info("注解方式删除学生"); studentMapper.deleteStudent(7); sqlSession.commit(); }
运行测试方法,实现了学生信息的删除。
7.通过Id查询学生:
@Select("select * from t_student where id=#{id}") public Student findStudentById(Integer id);
添加测试方法:
@Test public 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=" 4000 ;age",property="age") } ) public List<Student> findStudents();
添加测试方法:
@Test public void testFindStudents() { logger.info("注解方式查找所有学生"); List<Student> studentList = studentMapper.findStudents(); for(Student s:studentList){ System.out.println(s); } }
运行测试方法,查询到了所有学生的信息。
相关文章推荐
- J2EE系列之MyBatis学习笔记(十二)-- 使用注解配置sql映射器
- J2EE系列之MyBatis学习笔记(三)-- 使用XML配置SQL映射器
- J2EE系列之MyBatis学习笔记(十一)-- 使用注解配置sql映射器
- J2EE系列之MyBatis学习笔记(二)-- mybatis配置
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- Java Persistence with MyBatis 3(中文版) 第四章 使用注解配置SQL映射器
- 《Spring+Mybatis》Mybatis简单注解配置的用法学习笔记与代码
- J2EE系列之MyBatis学习笔记(七)-- 动态sql
- mybatis学习笔记--配置文件+注解完整案例
- 【MyBatis学习18】使用注解配置Mapper
- mybatis学习笔记(六)使用generator生成mybatis基础配置代码和目录结构
- 【spring学习笔记二】使用注解配置spring
- mybatis学习笔记之mybatis注解配置详解
- mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现
- 【MyBatis学习18】使用注解配置Mapper
- J2EE系列之MyBatis学习笔记(九)-- 杂项
- J2EE系列之MyBatis学习笔记(六)-- 动态sql
- J2EE系列之MyBatis学习笔记(四)-- mybatis关系映射(一对一映射)
- MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)