【MyBatis】对MySQL数据库进行增删改查

29 篇文章 0 订阅
25 篇文章 0 订阅

利用MyBatis对MySQL进行增删改查工作

(一)创建数据库表
在这里插入图片描述
在这里插入图片描述
(二)创建一个bean组件,内部属性包含数据库表中的全部字段,用于存放数据库中的数据。

在这里插入图片描述

package com.atguigu.bean;

public class Cat {
	private Integer id;
	private String cName;
	private Integer cAge;
	private Integer cGender;
	
	
	
	public Cat() {
		super();
	}
	public Cat(Integer id, String cName, Integer cAge, Integer cGender) {
		super();
		this.id = id;
		this.cName = cName;
		this.cAge = cAge;
		this.cGender = cGender;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getcName() {
		return cName;
	}
	public void setcName(String cName) {
		this.cName = cName;
	}
	public Integer getcAge() {
		return cAge;
	}
	public void setcAge(Integer cAge) {
		this.cAge = cAge;
	}
	public Integer getcGender() {
		return cGender;
	}
	public void setcGender(Integer cGender) {
		this.cGender = cGender;
	}
	@Override
	public String toString() {
		return "Cat [id=" + id + ", cName=" + cName + ", cAge=" + cAge + ", cGender=" + cGender + "]";
	}
	
	
}

(三)创建dao层接口,用于对数据库进行增删改查操作
在这里插入图片描述

package com.atguigu.dao;

import com.atguigu.bean.Cat;

public interface CatDao {
	public Cat getCatById(Integer id);
	public int insertCat(Cat cat);
	public int deleteCat(Integer id);
	public int updateCat(Cat cat);
}

说明:
1.public Cat getCatById(Integer id);——利用id来查询数据库中的数据,因此方法参数传入id,查询的返回值是Cat类型。
2.public int insertCat(Cat cat);——插入方法中传入Cat类型的变量cat,通过SQL语句对Cat中的属性进行绑定,并为属性赋值来达到添加数据的效果。并且MyBatis会自动返回一个0/1的整型数,表示是否添加成功,这里返回值类型如果是Boolean类型,则返回true/false表示是否添加成功。
3.public int deleteCat(Integer id);——参数传入id,通过id来确定删除的数据,并自动返回0/1表示是否产出成功。
4.public int updateCat(Cat cat);——参数传入Cat,通过SQL语句绑定Cat中的属性,通过对属性赋值来达到更新的目的。MyBatis自动返回0/1表示是否更新成功。
(四)创建接口实现的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.dao.CatDao">
  
  <!-- public Cat getCatById(Integer id);
	public int insertCat(Cat cat);
	public int deleteCat(Integer id);
	public int updateCat(Cat cat); -->
  
  
  	<select id="getCatById" resultType="com.atguigu.bean.Cat">
  		select * from t_cat where id = #{id}
  	</select>
  	
  	<insert id="insertCat">
  		INSERT INTO t_cat(cName,cAge,cGender) VALUES(#{cName},#{cAge},#{cGender})
  	</insert>
  	
  	<delete id="deleteCat">
  		DELETE FROM t_cat WHERE id=#{id}
  	</delete>
  	
  	<update id="updateCat">
  		UPDATE t_cat
		SET cName=#{cName},cAge=#{cAge},cGender=#{cGender}
		WHERE id=#{id}
  	</update>
  </mapper>

说明:
1.namespace=“com.atguigu.dao.CatDao”——连接的接口的全路径。
2.selectinsertdeleteupdate中的id是对应操作的方法名。注意:如果是查询操作,必须携带resultType,指明返回数据的类型(一般是Bean的全路径)。标签内部即为SQL语句。
3.特别注意:接口的实现配置文件Mybatis无法识别,一定记得在第五步mapper中进行配置。
(五)创建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_0325?serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
		//扫描包下所有接口配置文件
    <package name="com/atguigu/dao"/>
  </mappers>
</configuration>

说明:该文件是Mybatis的配置文件,用于数据库连接操作。特别注意:一定要在<mappers>标签中配置步骤四中的接口文件。如果接口配置文件在类路径中(直接建在conf文件夹下)<mappers>标签中还可以这样配置:

<mappers>
    <mapper resource="CatDao.xml"/>
  </mappers>

(六)编写测试类:
在这里插入图片描述

package com.atguigu.test;

import java.io.IOException;
import java.io.InputStream;

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.Test;

import com.atguigu.bean.Cat;
import com.atguigu.dao.CatDao;

public class catTest {

//	添加
	@Test
	public void test() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession(true);
		
		CatDao mapper = openSession.getMapper(CatDao.class);
		mapper.insertCat(new Cat(null, "cat02", 5, 0));
	}
	
//	查询
	@Test
	public void test01() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession(true);
		
		CatDao mapper = openSession.getMapper(CatDao.class);
		Cat id = mapper.getCatById(1);
		System.out.println(id);
	}
	
//	删除
	@Test
	public void test02() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession(true);
		
		CatDao mapper = openSession.getMapper(CatDao.class);
		mapper.deleteCat(1);
	}
	
//	修改
	@Test
	public void test03() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession(true);
		
		CatDao mapper = openSession.getMapper(CatDao.class);
		mapper.updateCat(new Cat(null, "cat03", 60, 1));
	}
}

说明:该配置可参照mybatis入门文档。
特别注意:SqlSession openSession = sqlSessionFactory.openSession(true);中的true在插入操作中必须要设置为true,其他操作有没有都无所谓,因为Mybatis不能自主提交数据到数据库,只能手动提交数据。true则表示手动提交数据到数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值