欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用MyBatis实现简单的增删改查操作

程序员文章站 2022-06-17 17:20:55
...

1.新建数据库表、新建项目

2.导入jar包

使用MyBatis实现简单的增删改查操作

3.新建MyBatis配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
	<properties resource="db.properties"></properties>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/software/ssm/bean/CustomerMapper.xml"/>
	</mappers>
</configuration>

4.新建Customer类

import java.io.Serializable;

public class Customer implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int id;
	private String username;
	private String job;
	private int phone;
	public Customer(int id, String username, String job, int phone) {
		super();
		this.id = id;
		this.username = username;
		this.job = job;
		this.phone = phone;
	}
	public Customer() {
		super();
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public int getPhone() {
		return phone;
	}
	public void setPhone(int phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", username=" + username + ", job=" + job + ", phone=" + phone + "]";
	}
}

5.编写CustomerMapper.xml映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.software.ssm.bean.CustomerMapper">
    //查
	<select id="findCustomerById" parameterType="Integer"
		resultType="com.software.ssm.bean.Customer">
		select * from t_customer
	</select>
     //增
	<insert id="insertCustomer"
		parameterType="com.software.ssm.bean.Customer">
		insert into t_customer(username, job, phone)
		values(#{username},#{job},#{phone})
	</insert>
     //改
	<update id="updateCustomer"
		parameterType="com.software.ssm.bean.Customer">
		update t_customer set username=#{username}, job=#{job}, phone=#{phone} where
		id=#{id}
	</update>
     //删
	<delete id="deleteCustomer" parameterType="com.software.ssm.bean.Customer">
		delete from t_customer where id=#{id}
	</delete>
</mapper>

6.编写增删改查测试方法

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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.After;
import org.junit.Before;
import org.junit.Test;
import com.software.ssm.bean.Customer;

public class CustomerTest {

	private InputStream inputStream;
	private SqlSessionFactory sqlSessionFactory;
	private SqlSession sqlSession;

	@Before
	public void getSession() {
		String resource = "mybatis-config.xml";
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			sqlSession = sqlSessionFactory.openSession();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@After
	public void closeSession() {
		try {
			sqlSession.close();
			inputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

    //测试查询方法
	@Test
	public void findCustomerByIdTest() throws IOException {
		List<Customer> list = sqlSession.selectList("com.software.ssm.bean.CustomerMapper.findCustomerById");
		for (Customer customer : list) {
			System.out.println(customer);
		}
	}

    //测试插入方法
	@Test
	public void insertCustomerTest() {
		Customer customer = new Customer(1, "tom", "teacher", 636412666);
		int result = sqlSession.insert("com.software.ssm.bean.CustomerMapper.insertCustomer", customer);
		int expected = 1;
		sqlSession.commit();
		assertEquals(expected, result);
	}

    //测试更新方法
	@Test
	public void updateCustomerTest() {
		Customer customer = new Customer(5, "jack", "java", 991119);
		int rows = sqlSession.update("com.software.ssm.bean.CustomerMapper.updateCustomer", customer);
		if (rows > 0) {
			sqlSession.commit();
		} else {
			System.out.println("修改失败");
		}
	}

    //测试删除方法
	@Test
	public void deleteCustomerTest() {
		int rows = sqlSession.delete("com.software.ssm.bean.CustomerMapper.deleteCustomer", 2);
		if (rows > 0) {
			sqlSession.commit();
		} else {
			System.out.println("删除失败");
		}
	}
}