Mybatis 除了可以在xml定义sql外, 还可以通过java来生成sql.
生成sql的java类EntityProvider:
MemberMapper:
其中@InsertProvider,@UpdateProvider,@SelectProvide, @DeleteProvider这几个注解告诉mapper sql将会从哪个类的哪个方法动态生成.
测试类TestProvider:
生成sql的java类EntityProvider:
// Copyright (c) 1998-2013 Core Solutions Limited. All rights reserved. package com.core.traininig.mybatisquiz; import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN; import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM; import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO; import static org.apache.ibatis.jdbc.SqlBuilder.SET; import static org.apache.ibatis.jdbc.SqlBuilder.SQL; import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE; import static org.apache.ibatis.jdbc.SqlBuilder.VALUES; import static org.apache.ibatis.jdbc.SqlBuilder.WHERE; import org.apache.ibatis.jdbc.SelectBuilder; import com.core.traininig.entity.Member; import com.core.traininig.entity.Task; /** * @author aron.tang */ public class EntityProvider { public static String getInsertSql(final Task task) { BEGIN(); INSERT_INTO("TASK"); VALUES("TASK_ID", "#{taskId,jdbcType=INTEGER}"); VALUES("MEMBER_ID", "#{memberId,jdbcType=INTEGER}"); VALUES("CONTENT", "#{content,jdbcType=VARCHAR}"); VALUES("DUE_DATE", "#{dueDate,jdbcType=DATE}"); return SQL(); } public static String getUpdateSql(final Member member) { BEGIN(); UPDATE("MEMBER"); SET("FIRST_NAME=#{firstName,jdbcType=VARCHAR}"); SET("LAST_NAME=#{lastName,jdbcType=VARCHAR}"); SET("ADDRESS=#{address,jdbcType=VARCHAR}"); SET("PHONE=#{phone,jdbcType=VARCHAR}"); WHERE("MEMBER_ID=#{memberId,jdbcType=INTEGER}"); return SQL(); } public static String getSelectSql(final int memberId) { SelectBuilder.BEGIN(); SelectBuilder.SELECT("MEMBER_ID AS memberId, FIRST_NAME AS firstName, LAST_NAME AS lastName" + ", ADDRESS AS address, PHONE AS phone "); SelectBuilder.FROM("MEMBER"); SelectBuilder.WHERE("MEMBER_ID=#{memberId,jdbcType=INTEGER}"); return SelectBuilder.SQL(); } public static String getDelSql(final int taskId) { BEGIN(); DELETE_FROM("TASK"); WHERE("TASK_ID=#{#taskId,jdbcType=INTEGER}"); return SQL(); } }
MemberMapper:
// Copyright (c) 1998-2012 Core Solutions Limited. All rights reserved. package com.core.traininig.mybatisquiz; import java.util.List; /** * @author aron.tang * */ public interface MemberMapper { List<Member> getAllMembers(); @InsertProvider(method = "getInsertSql", type = EntityProvider.class) void createTask(Task task); @UpdateProvider(method = "getUpdateSql", type = EntityProvider.class) void updateMember(Member member); @SelectProvider(method = "getSelectSql", type = EntityProvider.class) Member getMemberById(int memberId); @DeleteProvider(method = "getDelSql", type = EntityProvider.class) void delTask(int taskId); }
其中@InsertProvider,@UpdateProvider,@SelectProvide, @DeleteProvider这几个注解告诉mapper sql将会从哪个类的哪个方法动态生成.
测试类TestProvider:
// Copyright (c) 1998-2013 Core Solutions Limited. All rights reserved. package com.core.traininig.mybatisquiz; import java.io.IOException; import java.io.InputStream; import java.util.Date; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.core.traininig.entity.Member; import com.core.traininig.entity.Task; /** * @author aron.tang */ public class TestProvider { private static SqlSessionFactory sqlSessionFactory; public static void main(final String[] args) throws IOException { //testCreateTask(); //testUpdateMember(); testGetMemberById(4); // testDelTask(10); } /** * @param i * @throws IOException */ private static void testDelTask(final int id) throws IOException { final SqlSession session = getSession(); final MemberMapper m = session.getMapper(MemberMapper.class); m.delTask(id); System.out.println("succeed to cancel task."); session.commit(); } /** * @param i * @throws IOException */ private static void testGetMemberById(final int id) throws IOException { final SqlSession session = getSession(); final Member m = session.getMapper(MemberMapper.class).getMemberById(id); System.out.println(m.getFirstName() + "." + m.getLastName()); } /** * @throws IOException * */ private static void testUpdateMember() throws IOException { final Member m = new Member(); m.setMemberId(4); m.setFirstName("dickson"); m.setLastName("wong"); m.setAddress("usa"); m.setPhone("376529081"); final SqlSession session = getSession(); final MemberMapper mapper = session.getMapper(MemberMapper.class); mapper.updateMember(m); System.out.println("succeed to update member " + m); session.commit(); session.close(); } private static SqlSession getSession() throws IOException { final String resource = "com/core/traininig/mybatisquiz/mybatis-config.xml"; final InputStream inputStream = Resources.getResourceAsStream(resource); // build the SqlSessionFactory instance which manage the session when access DB. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); final SqlSession session = sqlSessionFactory.openSession(); return session; } private static void closeSession() { final SqlSession session = sqlSessionFactory.openSession(); if (session != null) { session.close(); } } private static void testCreateTask() throws IOException { final Task t = new Task(); t.setTaskId(10); t.setMemberId(4); t.setContent("doing mybatis test1"); t.setDueDate(new Date()); final SqlSession session = getSession(); final MemberMapper m = session.getMapper(MemberMapper.class); m.createTask(t); session.commit(); System.out.println("create Task successfully."); } }
相关推荐
java资源之Mybatis基础入门学习笔记,详细解析,适合新手。
基于Mysql数据库的MyBatis经典入门案例(初学者必备),导入项目即可运行测试,内附Mysql驱动jar包、sql资源语句!
本书中从一个简单的MyBatis查询入手,搭建起学习MyBatis的基础开发环境。通过全面的示例代码和测试讲解了在MyBatis XML方式和注解方式中进行增、删、改、查操作的基本用法,介绍了动态SQL在不同方面的应用以及在使用...
java资源之Mybatis基础入门学习笔记,详细解析,适合新手,第二天进阶笔记。
java资源之Mybatis基础入门学习笔记,详细解析,适合新手,第四天进阶笔记。
java资源之Mybatis基础入门学习笔记,详细解析,适合新手,第三天进阶笔记。
mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中...《MyBatis-Plus入门文档》主要介绍了MyBatis-Plus入门使用,以及关于mybatis-plus的更多介绍及特性,感兴趣的可以下载学习一下
Mybatis 入门到理解篇,Mybatis 入门到精通,基础知识
ssm框架SpringMVC+Mybatis非常好的入门视频,由浅入深,包含源码,配置文件。链接失效请复制到地址栏打开,如果打不开请私信或底下评论,我会更新链接!
第2篇是MyBatis技术入门,包括剖析JDBC的弊端、MyBatis的背景介绍、入门程序的编写、配置文件的剖析、高级映射及缓存结构的讲解,最后还介绍了MyBatis与Spring框架的整合。第3篇是Spring MVC技术入门,包括Spring ...
NULL 博文链接:https://aron-tang.iteye.com/blog/1844877
Mybatis框架基础入门,适合新手看
《MyBatis 从入门到精通》本书中从一个简单的 MyBatis 查询入手,搭建起学习 MyBatis 的基础开发环境。通过全面的示例代码和测试讲解了在 MyBatis XML 方式和注解方式中进行增、删、改、查操作的基本用法,介绍了...
MyBatis3(入门教程)讲述的是MtBatis的基础用法,内容不错.拿来分享
在SpringMVC项目中使用mybatis,零基础人员可迅速掌握
电子工业出版社的《MyBatis 从入门到精通》的源代码。本书中从一个简单的 MyBatis 查询入手,搭建起学习 MyBatis 的基础开发环境。通过全面的示例代码和测试讲解了在 MyBatis XML 方式和注解方式中进行增、删、改、...
mybatis的基础入门搭建,主要是使用了mybatis搭建dao层,idea环境,数据库自行根据实体类编写
MyBatis是一款优秀的ORM框架,本课程讲解MyBatis的基础知识,主要内容包括:MyBatis增、删、改、查,MyBatis与Spring集成,association和collection映射,声明式事务管理,MyBatis参数传递的几种方式。
MyBatis是一款优秀的ORM框架,本课程为走进Mybatis世界系列课程第二讲第二章:MyBatis与Spring集成。本章主要内容:MyBatis与Spring如何集成,声明式事务管理,MapperScannerConfigurer本次课程,在YY上进行,YY频道...
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。