
MyBatis——(5)MyBatis_映射配置文件_增删改查以及获取自增主键值
发布日期:2021-05-07 02:35:48
浏览次数:20
分类:精选文章
本文共 4796 字,大约阅读时间需要 15 分钟。
思路:
1:根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息 2:sql映射文件;配置了每一个sql,以及sql的封装规则等。,并写一个接口与它对应 3:将sql映射文件注册在全局配置文件中 4:写代码: 1)、根据全局配置文件得到SqlSessionFactory; 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查一个 sqlSession就是代表和数据库的一次会话,用完关闭 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。创建employee表
CREATE TABLE Employee( id INTEGER PRIMARY KEY auto_increment, lastName VARCHAR(20) NOT NULL, email VARCHAR(20) NOT NULL, gendar VARCHAR(20) NOT NULL )
Employee表
package com.atstudying.mybatis.bean;import org.apache.ibatis.type.Alias;public class Employee { private Integer id; private String lastName; private String email; private String gendar; private Department dept; public Employee() { super(); } public Employee(Integer id, String lastName, String email, String gender) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gendar = gender; } public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gendar; } public void setGender(String gender) { this.gendar = gender; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gendar + "]"; } }
1:根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
mybatis-config.xmlMyBatisTest.java
public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); }
2:sql映射文件;配置了每一个sql,以及sql的封装规则等。,并写一个接口与它对应
EmployeeMapper.javapackage com.atstudying.mybatis.dao;import java.util.List;import java.util.Map;import org.apache.ibatis.annotations.MapKey;import org.apache.ibatis.annotations.Param;import com.atguigu.mybatis.bean.Employee;public interface EmployeeMapper { public Employee getEmpById(Integer id); public Long addEmp(Employee employee); public boolean updateEmp(Employee employee); public Integer deleteEmpById(Integer id); }
EmployeeMapper.xml
insert into employee(lastName,email,gendar) values(#{lastName},#{email},#{gendar}) update employee set lastName=#{lastName},email=#{email},gendar=#{gendar} where id=#{id} delete from employee where id=#{id}
3:将sql映射文件注册在全局配置文件中
4:写代码: 1)、根据全局配置文件得到SqlSessionFactory; 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查一个 sqlSession就是代表和数据库的一次会话,用完关闭 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。 MyBatisTest.javapublic class MyBatisTest { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test03() throws IOException{ SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交数据 SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //测试添加 Employee employee = new Employee(null, "jerry4","22@qq.com", "1"); mapper.addEmp(employee); System.out.println(employee.getId()); //测试修改 Employee employee = new Employee(2, "Tom", "jerry@qq.com", "0"); boolean updateEmp = mapper.updateEmp(employee); System.out.println(updateEmp); //测试删除 Integer num=mapper.deleteEmpById(3); System.out.println(num); //2、手动提交数据 openSession.commit(); }finally{ openSession.close(); } }
结果:

测试增删改查这一个过程中,我们可以得到如下结论
1、mybatis允许增删改直接定义以下类型返回值 Integer、Long、Boolean、void 2、我们需要手动提交数据 sqlSessionFactory.openSession();——》手动提交 sqlSessionFactory.openSession(true);——》自动提交 3,主键必须 < insert id=“addEmp” parameterType=“com.atstudying.mybatis.bean.Employee” useGeneratedKeys=“true” keyProperty=“id” databaseId=“mysql”> insert into employee(lastName,email,gendar) values(#{lastName},#{email},#{gendar}) < /insert > 获取自增主键的值: useGeneratedKeys=“true”;使用自增主键获取主键值策略 keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
发表评论
最新留言
不错!
[***.144.177.141]2025年04月02日 11时25分15秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
8051单片机(STC89C52)以定时器中断模式实现两倒计时器异步计时
2019-03-05
vue项目通过vue.config.js配置文件进行proxy反向代理跨域
2019-03-05
android:使用audiotrack 类播放wav文件
2019-03-05
聊聊我的五一小假期
2019-03-05
数据库三个级别封锁协议
2019-03-05
ACM/NCPC2016 C Card Hand Sorting(upc 3028)
2019-03-05
ubuntu学习笔记-常用文件、命令以及作用(hosts、vim、ssh)
2019-03-05
SLAM学习笔记-求解视觉SLAM问题
2019-03-05
程序员应该知道的97件事
2019-03-05
create-react-app路由的实现原理
2019-03-05
openstack安装(九)网络服务的安装--控制节点
2019-03-05
shell编程(六)语言编码规范之(变量)
2019-03-05
vimscript学习笔记(二)预备知识
2019-03-05
Android数据库
2019-03-05
HTML基础,块级元素/行内元素/行内块元素辨析【2分钟掌握】
2019-03-05
STM8 GPIO模式
2019-03-05
23种设计模式一:单例模式
2019-03-05
Qt中的析构函数
2019-03-05
三层框架+sql server数据库 实战教学-徐新帅-专题视频课程
2019-03-05