博客
关于我
MyBatis——(5)MyBatis_映射配置文件_增删改查以及获取自增主键值
阅读量:324 次
发布时间:2019-03-04

本文共 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.xml

MyBatisTest.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.java

package 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.java

public 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的哪个属性

转载地址:http://oqqq.baihongyu.com/

你可能感兴趣的文章
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>