Mybatis的CRUD案例详解编程语言
一、Mybatis增删改查案例
上一节《Mybatis入门和简单Demo》讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作(重复的动作如环境搭建,引入依赖,mybatis.xml的配置,通用加载工具类的编写等参照上一节。
(1)编写需要CRUD操作的实体类Student
package com.jyk.mybatis.crud; public class Student { private String id; private String name; private String age; private String sex; public String getId() { return id; public void setId(String id) { this.id = id; public String getName() { return name; public void setName(String name) { this.name = name; public String getAge() { return age; public void setAge(String age) { this.age = age; public String getSex() { return sex; public void setSex(String sex) { this.sex = sex; public Student(String id, String name, String age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; public Student() { super(); }
(2)配置用于编写SQL语句的StudentMapper文件(路径com/jyk/mybatis/crud/StudentMapper.xml),并将mapper文件路径以及实体类别名加入到mybatis总配置文件中
?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" mapper namespace="com.jyk.mybatis.crud.Student" !-- 当实体属性名与表字段名不一样时,以下代码必须书写 当实体属性名与表字段名一样时,以下代码可选 -- resultMap type="crudstudent" id="studentMap" id property="id" column="id"/ result property="name" column="name"/ result property="age" column="age"/ result property="sex" column="sex"/ /resultMap !-- 添加操作 -- insert id="add" parameterType="crudstudent" insert into student(id,name,age,sex) values(#{id},#{name},#{age},#{sex}) /insert !-- 根据id查询操作: 如果参数不是一个实体,只是一个普通参数,例如int,double,string 这里的#{中间的变量名可以随便写},不过建议用方法的形式参数 select id="findById" parameterType="int" resultType="crudstudent" select id,name,age,sex from student where id = #{id} /select !-- 查询所有操作 -- select id="findall" resultType="crudstudent" select id,name,age,sex from student /select !-- 更新操作 -- update id="update" parameterType="crudstudent" update student set name=#{name},age=#{age},sex=#{sex} where id=#{id} /update !-- 删除操作 -- delete id="delete" parameterType="crudstudent" delete from student where id=#{id} /delete !-- 增删改查注意事项 :insert/update/delete标签只是一个模板,在做操作时,其实是以sql语句为核心的 即当做增/删/改时,insert/update/delete标签可通用,但是提倡做什么操作就使用什么标签 但做查询时只能使用select标签 !-- mybatis会将查询出来的表记录和resultMap值对应的映射结果互相匹配 -- select id="findById" parameterType="int" resultMap="studentMap" select id,name,age,sex from student where id = #{id} /select /mapper
?xml version="1.0" encoding="UTF-8"? !DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" configuration !-- 加载类路径下的属性文件 -- properties resource="db.properties" /properties !-- 设置类型别名 -- typeAliases typeAlias type="com.jyk.mybatis.crud.Student" alias="crudstudent"/ !-- typeAlias type="com.jyk.mybatis.page.Student" alias="pagestudent"/ typeAlias type="com.jyk.mybatis.dynamic.Student" alias="dynamicstudent"/ -- /typeAliases !-- 设置一个默认的连接环境信息 -- environments default="mysql_env" !-- 连接环境信息,取一个唯一的编号 -- environment id="mysql_env" !-- mybatis使用的jdbc事务管理方式 -- transactionManager type="jdbc" /transactionManager !-- mybatis使用连接池方式来获取链接 -- dataSource type="pooled" !-- 配置与数据库交互的四个属性 -- property name="driver" value="${mysql.driver}"/ property name="url" value="${mysql.url}"/ property name="username" value="${mysql.username}"/ property name="password" value="${mysql.password}"/ /dataSource /environment /environments mappers mapper resource="com/jyk/mybatis/crud/StudentMapper.xml"/ !-- mapper resource="com/jyk/mybatis/page/StudentMapper.xml"/ mapper resource="com/jyk/mybatis/dynamic/StudentMapper.xml"/ -- /mappers /configuration
(3)编写DAO,通过Java代码控制Mybatis进行增删改查,MyBatisUtil的编写参照上一节
package com.jyk.mybatis.crud; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.jyk.mybatis.util.MyBatisUtil; public class StudentDao { * 增加的方法1 public void add(Student stu) SqlSession sqlSession = null; try{ sqlSession = MyBatisUtil.getSqlSession(); int i = sqlSession.insert(Student.class.getName()+".add", stu); System.out.println("本次操作影响了"+i+"行数据"); //事务提交 sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); //事务回滚 sqlSession.rollback(); throw e; }finally{ MyBatisUtil.closeSqlSession(); * 根据ID查找 public Student findById(int id) SqlSession sqlSession = null; try{ sqlSession = MyBatisUtil.getSqlSession(); Student stu = sqlSession.selectOne(Student.class.getName()+".findById", id); return stu; }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MyBatisUtil.closeSqlSession(); * 查询所有学生 public List Student findall() SqlSession sqlSession = null; try{ sqlSession = MyBatisUtil.getSqlSession(); return sqlSession.selectList(Student.class.getName()+".findall"); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MyBatisUtil.closeSqlSession(); * 更新学生信息 public void update(Student stu) SqlSession sqlSession = null; try{ sqlSession = MyBatisUtil.getSqlSession(); sqlSession.update(Student.class.getName()+".update", stu); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MyBatisUtil.closeSqlSession(); * 删除操作 public void delete(Student stu) SqlSession sqlSession = null; try{ sqlSession = MyBatisUtil.getSqlSession(); sqlSession.update(Student.class.getName()+".delete", stu); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MyBatisUtil.closeSqlSession(); }
上面增删改查操作案例需要关心的几个地方:
(1)mapper文件的名称需要唯一,所以一般以操作的实体类全路径为名称空间
(2)当实体属性名与表字段名不一样时,resultMap必须书写,当实体属性名与表字段名一样时,resultMap可选,resultMap的type代表参数的类型,可供parameterType,resultType直接名称引用,id作为唯一标识,可被resultMap使用
(3)如果参数不是一个实体,只是一个普通参数,例如int,double,string,这里的#{中间的变量名可以随便写},不过建议用方法的形式参数
select id="findById" parameterType="int" resultType="crudstudent" select id,name,age,sex from student where id = #{id} /select
(4)增删改查注意事项 :insert/update/delete标签只是一个模板,在做操作时,其实是以sql语句为核心的,即当做增/删/改时,insert/update/delete标签可通用,但是提倡做什么操作就使用什么标签,但做查询时只能使用select标签
(5)parameterType:指入参类型,可引用resultMap的type属性
resultType:指出参类型,可引用resultMap的id属性
resultMap:mybatis会将查询出来的表记录和resultMap值对应的映射结果互相匹配
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20515.html
cjavamysqlxml相关文章
- Spartan-6开发案例使用手册——嵌入式AD模块/接口基础测试(下)
- MyBatis——【第二章】mybatis动态sql(分页)
- MyBatis框架:第五章:源码解析及Mapper接口方式的mybatis的增,删,改,查实现
- MyBatis框架:第五章:源码解析及Mapper接口方式的mybatis的增,删,改,查实现
- 23个项目管理经典案例_交互动画
- MyBatis-Plus 入门案例
- MyBatis-Plus联表查询(Mybatis-Plus-Join)
- 广域铭岛出席“百城千园行”重庆站活动 分享流程行业数字化转型案例
- Mybatis-Plus入门案例(springboot项目实现)
- 【愚公系列】2023年01月 .NET CORE工具案例-使用pythonnet实现C#和Python的融合调用
- 【愚公系列】2023年02月 .NET CORE工具案例-Photino跨平台桌面应用程序
- 【数字信号处理】线性时不变系统 LTI “ 输入 “ 与 “ 输出 “ 之间的关系 ( 线性卷积计算方法列举 | 线性卷积计算案例一 | 根据 线性卷积 定义直接计算 卷积 )
- MyBatis学习总结(七)——Mybatis缓存详解编程语言
- 广告、案例刷爆网络 知名企业涌入市场 快速“躺瘦”真的靠谱吗?
- 实战案例:实现CentOS 7编译安装基于httpd 模块方式的LAMP
- tomcat实战案例:手动的应用部署
- 25个Oracle应用案例,助力企业升华业务(oracle的应用案例)
- 【信息诈骗典型案例】之”猜猜我是谁?”