使用MyBatis编写Dao的两种语法
程序员文章站
2022-04-14 16:20:15
原文链接:http://www.yiidian.com/mybatis/mybatis dao.html 在MyBatis中,我们有两种Dao的写法,一种叫传统Dao写法,一种叫Mapper代理接口。下面看看如何实现。 1 传统Dao写法 1.1 编写CustomerDao接口 1.2 编写Cust ......
原文链接:
在mybatis中,我们有两种dao的写法,一种叫传统dao写法,一种叫mapper代理接口。下面看看如何实现。
1 传统dao写法
1.1 编写customerdao接口
package com.yiidian.dao; import com.yiidian.domain.customer; import java.util.list; /** * dao接口 *一点教程网 - www.yiidian.com */ public interface customerdao { /** * 查询所有用户 */ public list<customer> findall(); /** * 添加 */ public void save(customer customer); /** * 修改 */ public void update(customer customer); /** * 查询一个 */ public customer findbyid(integer id); /** * 条件查询 */ public list<customer> findbyname(string name); /** * 删除 */ public void delete(integer id); }
1.2 编写customerdao实现类
package com.yiidian.dao.impl; import com.yiidian.dao.customerdao; import com.yiidian.domain.customer; import com.yiidian.utils.mybatisutils; import org.apache.ibatis.session.sqlsession; import java.util.list; /** * dao实现类 */ public class customerdaoimpl implements customerdao{ @override public list<customer> findall() { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); return sqlsession.selectlist("com.yiidian.dao.customerdao.findall"); } catch (exception e) { e.printstacktrace(); } finally{ sqlsession.close(); } return null; } @override public void save(customer customer) { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); sqlsession.insert("com.yiidian.dao.customerdao.save", customer); sqlsession.commit(); } catch (exception e) { e.printstacktrace(); sqlsession.rollback(); } finally{ sqlsession.close(); } } @override public void update(customer customer) { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); sqlsession.update("com.yiidian.dao.customerdao.update", customer); sqlsession.commit(); } catch (exception e) { e.printstacktrace(); sqlsession.rollback(); } finally{ sqlsession.close(); } } @override public customer findbyid(integer id) { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); return sqlsession.selectone("com.yiidian.dao.customerdao.findbyid",id); } catch (exception e) { e.printstacktrace(); } finally{ sqlsession.close(); } return null; } @override public list<customer> findbyname(string name) { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); return sqlsession.selectlist("com.yiidian.dao.customerdao.findbyname",name); } catch (exception e) { e.printstacktrace(); } finally{ sqlsession.close(); } return null; } @override public void delete(integer id) { sqlsession sqlsession = null; try { sqlsession = mybatisutils.getsession(); sqlsession.delete("com.yiidian.dao.customerdao.delete", id); sqlsession.commit(); } catch (exception e) { e.printstacktrace(); sqlsession.rollback(); } finally{ sqlsession.close(); } } }
传统方式的重点在于dao实现类,在dao实现类中,手动调用sqlsession提供的方法直接执行映射文件的sql语句。
1.3 编写customerdao.xml映射
<?xml version="1.0" encoding="utf-8"?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace: 用于指定该映射文件需要映射的dao接口 --> <mapper namespace="com.yiidian.dao.customerdao"> <!--查询所有--> <select id="findall" resulttype="com.yiidian.domain.customer"> select * from t_customer </select> <!--1.添加方法--> <insert id="save" parametertype="com.yiidian.domain.customer"> insert into t_customer(name,gender,telephone) values(#{name},#{gender},#{telephone}) </insert> <!--2.修改方法--> <update id="update" parametertype="com.yiidian.domain.customer"> update t_customer set name = #{name}, gender = #{gender}, telephone = #{telephone} where id = #{id} </update> <!--查询一个--> <select id="findbyid" parametertype="integer" resulttype="com.yiidian.domain.customer"> select * from t_customer where id = #{id} </select> <!--条件查询--> <select id="findbyname" parametertype="string" resulttype="com.yiidian.domain.customer"> select * from t_customer where name like #{name} </select> <!--删除--> <delete id="delete" parametertype="integer"> delete from t_customer where id = #{id} </delete> </mapper>
1.4 编写测试类
package com.yiidian.mybatis; import com.yiidian.dao.customerdao; import com.yiidian.dao.impl.customerdaoimpl; import com.yiidian.domain.customer; import com.yiidian.utils.mybatisutils; 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 org.junit.test; import java.io.ioexception; import java.io.inputstream; import java.util.list; /** * mybatis测试类 - 传统dao写法 * 一点教程网 - www.yiidian.com */ public class testcustomerdao { /** * 添加 */ @test public void testsave(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用save方法 customer customer = new customer(); customer.setname("小苍"); customer.setgender("女"); customer.settelephone("15755556666"); customerdao.save(customer); //4.关闭连接 session.close(); } /** * 修改 */ @test public void testupdate(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用update方法 customer customer = new customer(); customer.setid(5); customer.setname("小泽"); customer.setgender("女"); customer.settelephone("15755556666"); customerdao.update(customer); session.commit(); //4.关闭连接 session.close(); } /** * 查询所有 */ @test public void testfindall(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用findall方法 list<customer> list = customerdao.findall(); for(customer cust:list){ system.out.println(cust); } //4.关闭连接 session.close(); } /** * 查询一个 */ @test public void testfindbyid(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用findbyid方法 customer customer = customerdao.findbyid(3); system.out.println(customer); //4.关闭连接 session.close(); } /** * 条件查询 */ @test public void testfindbyname(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用findbyname方法 list<customer> list = customerdao.findbyname("%小%"); for(customer cust:list){ system.out.println(cust); } //4.关闭连接 session.close(); } /** * 删除 */ @test public void testdelete(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.创建传统dao实现类对象 customerdao customerdao = new customerdaoimpl(); //3.调用findbyname方法 customerdao.delete(5); // 提交事务 session.commit(); //4.关闭连接 session.close(); } }
2 mapper代理接口
2.1 编写customerdao接口
package com.yiidian.dao; import com.yiidian.domain.customer; import java.util.list; /** * dao接口 *一点教程网 - www.yiidian.com */ public interface customerdao { /** * 查询所有用户 */ public list<customer> findall(); /** * 添加 */ public void save(customer customer); /** * 修改 */ public void update(customer customer); /** * 查询一个 */ public customer findbyid(integer id); /** * 条件查询 */ public list<customer> findbyname(string name); /** * 删除 */ public void delete(integer id); }
2.2 编写customerdao.xml映射
<?xml version="1.0" encoding="utf-8"?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace: 用于指定该映射文件需要映射的dao接口 --> <mapper namespace="com.yiidian.dao.customerdao"> <!--查询所有--> <select id="findall" resulttype="com.yiidian.domain.customer"> select * from t_customer </select> <!--1.添加方法--> <insert id="save" parametertype="com.yiidian.domain.customer"> insert into t_customer(name,gender,telephone) values(#{name},#{gender},#{telephone}) </insert> <!--2.修改方法--> <update id="update" parametertype="com.yiidian.domain.customer"> update t_customer set name = #{name}, gender = #{gender}, telephone = #{telephone} where id = #{id} </update> <!--查询一个--> <select id="findbyid" parametertype="integer" resulttype="com.yiidian.domain.customer"> select * from t_customer where id = #{id} </select> <!--条件查询--> <select id="findbyname" parametertype="string" resulttype="com.yiidian.domain.customer"> select * from t_customer where name like #{name} </select> <!--删除--> <delete id="delete" parametertype="integer"> delete from t_customer where id = #{id} </delete> </mapper>
2.3 编写测试类
package com.yiidian.mybatis; import com.yiidian.dao.customerdao; import com.yiidian.dao.impl.customerdaoimpl; import com.yiidian.domain.customer; import com.yiidian.utils.mybatisutils; import org.apache.ibatis.session.sqlsession; import org.junit.test; import java.util.list; /** * mybatis测试类 - mapper代理接口 * 一点教程网 - www.yiidian.com */ public class testcustomerdao2 { /** * 添加 */ @test public void testsave(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用save方法 customer customer = new customer(); customer.setname("小苍"); customer.setgender("女"); customer.settelephone("15755556666"); customerdao.save(customer); //4.关闭连接 session.close(); } /** * 修改 */ @test public void testupdate(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用update方法 customer customer = new customer(); customer.setid(5); customer.setname("小泽"); customer.setgender("女"); customer.settelephone("15755556666"); customerdao.update(customer); session.commit(); //4.关闭连接 session.close(); } /** * 查询所有 */ @test public void testfindall(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用findall方法 list<customer> list = customerdao.findall(); for(customer cust:list){ system.out.println(cust); } //4.关闭连接 session.close(); } /** * 查询一个 */ @test public void testfindbyid(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用findbyid方法 customer customer = customerdao.findbyid(5); system.out.println(customer); //4.关闭连接 session.close(); } /** * 条件查询 */ @test public void testfindbyname(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用findbyname方法 list<customer> list = customerdao.findbyname("%小%"); for(customer cust:list){ system.out.println(cust); } //4.关闭连接 session.close(); } /** * 删除 */ @test public void testdelete(){ //1.获取sqlsession对象 sqlsession session = mybatisutils.getsession(); //2.生成dao代理对象 customerdao customerdao = session.getmapper(customerdao.class); //3.调用findbyname方法 customerdao.delete(5); // 提交事务 session.commit(); //4.关闭连接 session.close(); } }
源码下载:https://pan.baidu.com/s/1zsm5cau026zff3wzea-afq
欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
如果您对我的系列教程感兴趣,也可以关注我的网站:
上一篇: 凤凰蛋是毛蛋吗?吃它有营养吗?
推荐阅读
-
MyBatis Dao层的编写
-
BAT文件语法和技巧(bat文件的编写及使用
-
BAT文件语法和技巧(bat文件的编写及使用)
-
写一个简单的Ajax练习出错 使用mybatis的注入dao为null值,java.lang.NullPointerException
-
Mybatis(8)DAO实现类的使用方式
-
Mybatis(3)-编写dao实现类的方式
-
14-Mybatis中like使用的两种情况
-
使用MyBatis编写Dao的两种语法
-
MyBatis——dao代理的使用、深入理解参数(传递一个参数、传递多个参数、使用entity实体类传递、使用自定义类传递、按位置传递、使用Map传递)
-
BAT文件语法和技巧(bat文件的编写及使用)