使用MyBatis实现简单的增删改查操作
程序员文章站
2022-06-17 17:20:55
...
1.新建数据库表、新建项目
2.导入jar包
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("删除失败");
}
}
}
上一篇: 剑指offer 54:链表中环的入口节点
下一篇: 剑指offer:链表中环的入口节点
推荐阅读
-
jsp+servlet实现最简单的增删改查代码分享
-
使用封装好的数据库操作类来实现对数据库的增删查改
-
用PHP向数据库中实现简单的增删改查(纯代码,待完善),php增删
-
node.js使用mongoose操作数据库实现购物车的增、删、改、查功能示例
-
Nodejs连接mysql并实现增、删、改、查操作的方法详解
-
在MFC里面使用ADO访问微软的ACCESS数据库 实现增删改查
-
PHP简单操作MongoDB的方法(安装及增删改查),mongodb增删
-
php中操作memcached缓存进行增删改查数据的实现代码,phpmemcached
-
Mybatis Plus 增删改查的实现(小白教程)
-
Neo4j学习笔记(1)——使用Java API实现简单的增删改查