飞道的博客

Mybatis框架|更新与删除

310人阅读  评论(0)


一、使用Mybatis进行更新操作

1.创建数据库表user

创建数据库表,并添加记录。创建表的sql语句和添加后的数据库记录如下:

CREATE TABLE `user` (
	`id` INT(20)  AUTO_INCREMENT COMMENT '主键',
	`username` VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
	`sex` VARCHAR(1) NULL DEFAULT NULL COMMENT '性别',
	`address` VARCHAR(200) NULL DEFAULT NULL COMMENT '住址',
	`birthday` DATE NULL DEFAULT NULL COMMENT '生日',
	PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1
;

2.JavaBean

package com.gql.pojo;
import java.io.Serializable;
import java.util.Date;
/**
 * 类说明:
 *		JavaBean
 * @guoqianliang1998.
 */
public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private String sex;
	private String address;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

3.Mybatis配置

<?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>
	<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" />
				<property name="username" value="root" />
				<property name="password" value="Hudie" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="User.xml" />
	</mappers>
</configuration>

4.User.xml配置(update标签)

<?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="test">
	<update id="sql4" parameterType="com.gql.pojo.User">
		UPDATE user SET 
		username='${name}',
		sex=#{sex},
		address=#{address},
		birthday=#{birthday}
		WHERE id =#{id};
	</update>
</mapper>

5.使用Mybatis进行更新测试

package com.gql.Demo;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
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.Before;
import org.junit.Test;

import com.gql.pojo.User;
/**
 * 类说明:
 *		测试使用Mybatis在数据库中更新
 * @guoqianliang1998.
 */
public class Demo {
	private SqlSessionFactory sqlSessionFactory;
	@Before
	public void init() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		
	}

	@Test
	public void testUpdate(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user = new User();
			user.setId(6);
			user.setName("张国荣");
			user.setSex("男");
			user.setAddress("香港");
			user.setBirthday(new Date());
			session.update("test.sql4",user);
			session.commit();
		}finally{
			session.close();
		}
	}
}

成功将id=6的数据进行了更新。

二、使用Mybatis进行删除操作

1.User.xml的配置(delete标签)

<?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="test">
	<delete id="sql5" parameterType="int">
		delete from user where id=#{id};
	</delete>
</mapper>

2.使用Mybatis删除操作测试

package com.gql.Demo;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
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.Before;
import org.junit.Test;

import com.gql.pojo.User;
/**
 * 类说明:
 *		使用Mybatis在数据库中删除记录
 * @guoqianliang1998.
 */
public class Demo {
	private SqlSessionFactory sqlSessionFactory;
	@Before
	public void init() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream in = Resources.getResourceAsStream(resource);
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		
	}
	@Test
	public void testDelete(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			session.delete("test.sql5",5);
			session.commit();
		}finally{
			session.close();
		}
	}
}

成功删除id为5的记录:


转载:https://blog.csdn.net/weixin_43691058/article/details/104228253
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场