mybatis-增删查改

测试块

	@Test
	public void test2() throws Exception{
		SqlSession opSession=SqlSessionFactoryji();
		try {
			EmployeMapper employeMapper=opSession.getMapper(EmployeMapper.class);	
			 //插入一条数据,openSession。commit()手动提交一条数据
			/*
			 * Employe employe=new Employe(null, "John", "1", "123.@qq.com"); boolean
			 * boolean1=employeMapper.addEmp(employe);
			 * opSession.commit();/
			  //删除一条数据
			/* employeMapper.deleteEmpByid(2);
			 * opSession.commit(); */
			  //修改一条数据
			/*
			 * Employe employe=new Employe(25,"joy","1","2222@");
			 * employeMapper.updateEmp(employe); opSession.commit();
			 */
		}finally {
			opSession.close();
		}
	}

接口

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.hello.Employe;

public interface EmployeMapper {
	public Employe getEmployebyid(Integer id);
	public boolean addEmp(Employe employe);
	public void updateEmp(Employe employe);
	public void deleteEmpByid(Integer id);
}

实体

package com.atguigu.mybatis.hello;

public class Employe {
	private Integer id;
	private String lastName;
	private String gender;
	private String email;
	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 getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Employe(Integer id, String lastName, String gender, String email) {
		super();
		this.id = id;
		this.lastName = lastName;
		this.gender = gender;
		this.email = email;
	}
	
	public Employe() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Employe [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]";
	}
	
}

全局配置文件

<?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><settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>  <!-- 驼峰命名的设置属性 -->
</settings>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
	<!-- 将我们写好的的(EmployeMapper.xml)sql映射文件注射到全局配置文件中,及目前的文件-->
	<mappers>
		<mapper resource="EmployeMapper.xml" />
	</mappers>
</configuration>

EmployMapper.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">
<mapper namespace="com.atguigu.mybatis.dao.EmployeMapper">
<!--namespace:名称空间
	id:唯一标识
	resultType:返回值类型
	#<id>:传递过来的参数取出id值
  -->
	<select id="getEmployebyid"
		resultType="com.atguigu.mybatis.hello.Employe"> select id,last_name,gender,email from tbl_employe
		where id= #{id}  </select>		
		 	
	<!-- public void addEmp(Employe employe); -->
	<insert id="addEmp">
	insert into tbl_employe values(#{id},#{lastName},#{gender},#{email})</insert>
	
	<!-- public void deleteEmpByid(Integer id);  -->
	<delete id="deleteEmpByid">
		delete from tbl_employe where id=#{id}
	</delete> 
	
	<!-- public void updateEmp(Employe employe); -->
	<update id="updateEmp">
	update tbl_employe set last_name=#{lastName},gender=#{gender},email=#{email} where id=#{id}</update>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值